Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C#ASP.NETemail
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:
        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 Smile | :)
Posted 24-Dec-12 7:53am
Edited 25-Dec-12 23:46pm
v3
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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.)
  Permalink  
Comments
AhmadAhsan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

Check this one. It worked for me long time ago. My application still working with same code Smile | :)
 

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

Please Mark as Answer if this post helps you!
  Permalink  
v2
Comments
AhmadAhsan at 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.
Sheikh Muhammad Haris at 27-Dec-12 9:03am
   
What error are you having? Specify here.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

  Permalink  
Comments
AhmadAhsan at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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);
  Permalink  
Comments
AhmadAhsan at 27-Dec-12 0:23am
   
I have used he overloaded "send()" function of SmtpClient class, but getting the same error.
Krunal Rohit at 27-Dec-12 0:24am
   
this code works fine....
AhmadAhsan at 27-Dec-12 0:46am
   
Not working for me, do I need any additional configuraion???
Krunal Rohit at 27-Dec-12 0:49am
   
No.. what error you're getting..
AhmadAhsan at 29-Dec-12 7:44am
   
error is posted in the thread.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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
  Permalink  
Comments
AhmadAhsan at 27-Dec-12 0:46am
   
The LOC is included as: SmtpClient mail = new SmtpClient(); mail.EnableSsl = true; Any more suggestions ...
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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.
 
 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);
            }
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 534
1 OriginalGriff 312
2 Damith Weerasinghe 300
3 thatraja 220
4 ravikhoda 210
0 Sergey Alexandrovich Kryukov 10,974
1 OriginalGriff 6,667
2 Peter Leow 4,760
3 Abhinav S 3,813
4 Maciej Los 3,565


Advertise | Privacy | Mobile
Web03 | 2.8.140421.2 | Last Updated 27 Dec 2012
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Use
Layout: fixed | fluid