Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
C#
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)"


Please help me, I am stuck here.
Don't know what to do further.

Thanks in advance :)
Posted
Updated 25-Dec-12 23:46pm
v3
Comments
Member 11512066 15-Mar-15 8:56am    
Repeatedly I am getting this 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

It isn't clear if you want to send via gmail or you want to use another smtp server.

To send to any server you must match the protocol expected by the server. If you don't match it then it will fail.

If you want to use gmail then googling suggests that your code is incorrect (incomplete.)
 
Share this answer
 
Comments
AhmadAhsan 26-Dec-12 5:53am    
Jschell, I have tried to make the question clear. Currently I am trying to send email using gmail.
Can you please mention what piece of code is missing, what should be added to make the code working. Thanks.
Check this one. It worked for me long time ago. My application still working with same code :)


http://www.codeshode.com/2010/06/send-email-using-gmail-in-aspnet.html[^]


Please Mark as Answer if this post helps you!
 
Share this answer
 
v2
Comments
AhmadAhsan 27-Dec-12 0:30am    
The code I posted in the question and the one available on URL you provided is same. However I tried by omitting the port number but the error is same. Can you please provide your portion of application which is sending email. My email id is ahmad_ahsan@ymail.com. Do I need any additional configuration of my application ? Thanks a lot for any help.
[no name] 27-Dec-12 9:03am    
What error are you having? Specify here.
 
Share this answer
 
Comments
AhmadAhsan 26-Dec-12 7:00am    
Thanks. But I didn't see any significant difference between my code and the code posted at:
http://csharpdotnetfreak.blogspot.com/2009/08/send-email-using-gmail-in-aspnet.html

Getting the same error, any more suggestions plz, I am using visual studio 2010 Professional Edition.
C#
var smtp = new System.Net.Mail.SmtpClient();
   {
       smtp.Host = "smtp.gmail.com";
       smtp.Port = 587;
       smtp.EnableSsl = true;
       smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
       smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
       smtp.Timeout = 20000;
   }
   // Passing values to smtp object
   smtp.Send(fromAddress, toAddress, subject, body);
 
Share this answer
 
Comments
AhmadAhsan 27-Dec-12 0:23am    
I have used he overloaded "send()" function of SmtpClient class, but getting the same error.
[no name] 27-Dec-12 0:24am    
this code works fine....
AhmadAhsan 27-Dec-12 0:46am    
Not working for me, do I need any additional configuraion???
[no name] 27-Dec-12 0:49am    
No..
what error you're getting..
AhmadAhsan 29-Dec-12 7:44am    
error is posted in the thread.
Hi,

In case of sending Email from your gmail account You must set SSL

smtp.EnableSsl = true;

I think you are missing above code in your code.


Regards,

Ratnesh Kumar
 
Share this answer
 
Comments
AhmadAhsan 27-Dec-12 0:46am    
The LOC is included as:

SmtpClient mail = new SmtpClient();

mail.EnableSsl = true;

Any more suggestions ...
HI
If you can take a look am sending an email to Anele.Ngqandu@live.nmmu.ac.za
using gmail account. so I can change and send it to any email address
and this works for me well.

C#
MailMessage message = new MailMessage();

           message.From = new MailAddress("Ngqandu.Anele@gmail.com");
           message.To.Add(new MailAddress("Anele.Ngqandu@live.nmmu.ac.za"));
           message.Subject = subject;
           message.Body = body;

           SmtpClient client = new SmtpClient();
           client.Credentials = new System.Net.NetworkCredential("Ngqandu.Anele@gmail.com", "password");

           client.Port = 587;
           client.Host = "smtp.gmail.com";
           client.EnableSsl = true;
           try
           {
               client.Send(message);
           }
           catch (Exception ex)
           {
               System.Windows.Forms.MessageBox.Show(ex.Message);
           }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900