Search code examples
asp.netsmtpclient

Email not working on ASP.net


I am new ASP.Net and trying fix an mail issue. My website is hosted on godaddy server and its developed in ASP.Net. Trying to send an mail using following script but its throwing error as "Failure sending mail"

MailMessage mail = new MailMessage("[email protected]", "[email protected]");
    SmtpClient client = new SmtpClient();
    client.Host = "myhosting.secureserver.net";

    // Tried "relay-hosting.secureserver.net" -Errors-Unable to connect
    // Tried "smtpout.secureserver.net" -Errors-Unable to read data from
    // the transport connection: net_io_connectionclosed

    client.Port = 3535;  //Tried 80, 3535, 25, 465 (SSL)
    client.UseDefaultCredentials = false;
    client.DeliveryMethod = SmtpDeliveryMethod.Network;
    client.EnableSsl = false;
    client.ServicePoint.MaxIdleTime = 1;
    client.Timeout = 10000;
    client.Credentials = new NetworkCredential("[email protected]", "mypassword");
    mail.IsBodyHtml = true;
    mail.Subject = "New Job";

    mail.IsBodyHtml = true;
    try
    {

        client.Send(mail);
        mail.Dispose();
        Response.Redirect("thankyou.html");
    }
    catch (Exception ex)
    {
        Response.Write(ex.Message);

    }

When Print the exception got the following in ex:

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.CheckCommand.Send(SmtpConnection conn, String& response) at System.Net.Mail.MailCommand.Send(SmtpConnection conn, Byte[] command, MailAddress from, Boolean allowUnicode) at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection recipients, String deliveryNotify, Boolean allowUnicode, SmtpFailedRecipientException& exception) at System.Net.Mail.SmtpClient.Send(MailMessage message) --- End of inner exception stack trace --- at System.Net.Mail.SmtpClient.Send(MailMessage message) at _Default.SendEmail(Object sender, EventArgs e)


Solution

  • If you want to use port 465, etc. you should set up client.EnableSsl = true

    This configuration works for me:

        MailMessage message = new MailMessage
        {
          IsBodyHtml = true,
          Body = "text",
          Subject = "subject",
          To = { "[email protected]", "[email protected]" }
       };
    
       message.From = new MailAddress("[email protected]");
    
       SmtpClient client = new SmtpClient("myhosting.secureserver.net", 465)
       {
          Credentials = new NetworkCredential("[email protected]", "mypassword"),
          EnableSsl = true
       };
    
       client.Send();