1
votes

I am trying to send email via my web, I research for help no luck. Still receive error: smtpexception was unhandled by user code

 protected void Button1_Click(object sender, EventArgs e)
    {

        MailMessage msg = new MailMessage();
        msg.From = new MailAddress("[email protected]");
        msg.To.Add(TextBox3.Text);
        msg.Subject = TextBox4.Text;
        msg.Body = TextBox5.Text;
        msg.IsBodyHtml = true;

        SmtpClient smtp = new SmtpClient();
        smtp.Host = "smtp.gmail.com";

        System.Net.NetworkCredential netCred = new System.Net.NetworkCredential();
        netCred.UserName = "[email protected]";
        netCred.Password = "11111111";

        smtp.UseDefaultCredentials = true;
        smtp.Credentials = netCred;
        smtp.Port = 465;
        smtp.EnableSsl = true;
        smtp.Send(msg);
        try
        {
            smtp.Send(msg);
            Label1.Text = "Your E-Mail Sent Great Job!!!!";
        }
        catch (Exception ex)
        {
            //Handle your exception here
            Label1.Text = ex.Message + " " + "Oeps, something when wrong when we tried to send the email";
            return;
        }           
    }

when i run it shows me this error highlighting smtp.Send(message);:

SmtpException was unhandled by the user code and Failure sending mail.

System.Net.Mail.SmtpException: Failure sending mail. ---> System.IO.IOException: Unable to read data from the transport connection: net_io_connectionclosed. at System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte[] buffer, Int32 offset, Int32 read, Boolean readLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller, Boolean oneLine) at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader caller) 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) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at IntroAsp.net.EmailMsg.Button1_Click(Object sender, EventArgs e) in c:\Users\Regev\Documents\Visual Studio 2013\Projects\IntroAsp.net\IntroAsp.net\EmailMsg.aspx.cs:line 46

enter image description here

1
there should be much more to the exception detail than that, could you add it?Crowcoder
have you checked this SmtpException and its message?Pawel Gradecki
make sure gmail is set to allow less secure appsMad Myche

1 Answers

1
votes

Possible solutions:

1) Make smtpClient.UseDefaultCredentials = false;

2) The credentials "UserName" and "Password" are valid for sending an email.

3) Check port 465 is not blocked by a firewall.

4) Instead of using port number 465 use port 587.

Hope, this might help you.

Thank you.