I am trying to send email through an application in asp.net using c#. I searched a lot and mostly found the following code to send email using c# in asp.net:
MailMessage objEmail = new MailMessage();
objEmail.From = new MailAddress(txtFrom.Text);
objEmail.To.Add(txtTo.Text);
objEmail.CC.Add(txtCC.Text);
objEmail.Bcc.Add(txtBCC.Text);
objEmail.Subject = txtSubject.Text;
try
{
SmtpClient mail = new SmtpClient();
mail.EnableSsl = true;
mail.DeliveryMethod = SmtpDeliveryMethod.Network;
mail.Credentials = new NetworkCredential(txtFrom.Text, txtPassword.Text);
mail.Timeout = 20000;
mail.UseDefaultCredentials = false;
mail.Host = "smtp.gmail.com";
mail.Port = 587;
mail.Send(objEmail);
Response.Write("Your Email has been sent sucessfully - Thank You");
}
catch (Exception exc)
{
Response.Write("Send failure due to : <br />" + exc.ToString());
}
But constantly I am receiving the following error:
"System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: An existing connection was forcibly closed by the remote host. ---> System.Net.Sockets.SocketException: An existing connection was forcibly closed by the remote host at System.Net.Sockets.Socket.Receive(Byte[] buffer, Int32 offset, Int32 size, SocketFlags socketFlags) at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) --- End of inner exception stack trace --- at System.Net.Sockets.NetworkStream.Read(Byte[] buffer, Int32 offset, Int32 size) at System.Net.DelegatedStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.BufferedReadStream.Read(Byte[] buffer, Int32 offset, Int32 count) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) at System.Net.Mail.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.StartTlsCommand.Send(SmtpConnection conn) at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint) at System.Net.Mail.SmtpClient.GetConnection() at System.Net.Mail.SmtpClient.Send(MailMessage message)"