电子邮件无法使用 ASP.net

Email not working on ASP.net

我是新手 ASP.Net,正在尝试解决邮件问题。我的网站托管在 Godaddy 服务器上,其开发时间为 ASP.Net。尝试使用以下脚本发送邮件但抛出错误 "Failure sending mail"

MailMessage mail = new MailMessage("firstmail@mail.com", "sfirstmail@mail.com");
    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("myloginid@mail.com", "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);

    }

当 Print exception 在 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)

如果要使用465等端口需要设置client.EnableSsl = true

这个配置适合我:

    MailMessage message = new MailMessage
    {
      IsBodyHtml = true,
      Body = "text",
      Subject = "subject",
      To = { "firstmail@mail.com", "sfirstmail@mail.com" }
   };

   message.From = new MailAddress("sender@email.com");

   SmtpClient client = new SmtpClient("myhosting.secureserver.net", 465)
   {
      Credentials = new NetworkCredential("myloginid@mail.com", "mypassword"),
      EnableSsl = true
   };

   client.Send();