我是新的ASP.Net并尝试修复邮件问题。我的网站托管在godaddy服务器上,并在ASP.Net中开发。尝试使用以下脚本发送邮件,但抛出错误为“发送邮件失败”
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);
}
当打印异常时,在ex:
中得到以下内容System.Net.Mail.SmtpException:发送邮件失败。 ---> System.IO.IOException:无法从传输中读取数据 连接:net_io_connectionclosed。在 System.Net.Mail.SmtpReplyReaderFactory.ProcessRead(Byte []缓冲区, Int32 offset,Int32 read,Boolean readLine)at System.Net.Mail.SmtpReplyReaderFactory.ReadLines(SmtpReplyReader caller,Boolean oneLine)at System.Net.Mail.SmtpReplyReaderFactory.ReadLine(SmtpReplyReader 调用者)在System.Net.Mail.CheckCommand.Send(SmtpConnection conn, 字符串和放大器; System.Net.Mail.MailCommand.Send(SmtpConnection。) conn,Byte []命令,MailAddress from,Boolean allowUnicode)at System.Net.Mail.SmtpTransport.SendMail(MailAddress sender, MailAddressCollection收件人,String deliveryNotify,布尔值 allowUnicode,SmtpFailedRecipientException&例外) System.Net.Mail.SmtpClient.Send(MailMessage消息)---内部结束 异常堆栈跟踪--- at System.Net.Mail.SmtpClient.Send(MailMessage消息)at _Default.SendEmail(Object sender,EventArgs e)
答案 0 :(得分:1)
如果您想使用端口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();