.net 本地发邮件好用,部署到服务器上就不好用了
代码
public static bool SendEmail(string mailTo, string mailSubject, string mailContent)
{
//设置发送方的邮件信息,例如使用网易的smtp
string smtpServer = "smtp.qq.com"; //SMTP服务器
string mailFrom = "*********@qq.com"; //登陆用户名
string userPassword = "dfsfsdfsdf";//登陆密码
// 邮件服务设置
SmtpClient smtpClient = new SmtpClient();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
smtpClient.EnableSsl = true;
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
// 发送邮件设置
MailAddress from = new MailAddress(mailFrom);
MailAddress to = new MailAddress(mailTo);
MailMessage mailMessage = new MailMessage(from, to); // 发送人和收件人
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailContent;//内容
mailMessage.BodyEncoding = Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = MailPriority.Low;//优先级
try
{
Log.LogResult("信息 v " + "--" + mailMessage + "/r/n", "log", "111.txt");
smtpClient.Send(mailMessage); // 发送邮件
return true;
}
catch (SmtpException ex)
{
Log.LogResult("发送出现错误 v " + "--" + ex + "/r/n", "log", "111.txt");
return false;
}
}
#endregion
日志
ystem.Net.Mail.SmtpException: Failure sending mail. ---> System.Net.WebException: Unable to connect to the remote server ---> System.Net.Sockets.SocketException: 由于连接方在一段时间后没有正确答复或连接的主机没有反应,连接尝试失败。 14.17.57.241:25
at System.Net.Sockets.Socket.DoConnect(EndPoint endPointSnapshot, SocketAddress socketAddress)
at System.Net.ServicePoint.ConnectSocketInternal(Boolean connectFailure, Socket s4, Socket s6, Socket& socket, IPAddress& address, ConnectSocketState state, IAsyncResult asyncResult, Exception& exception)
--- End of inner exception stack trace ---
at System.Net.ServicePoint.GetConnection(PooledStream PooledStream, Object owner, Boolean async, IPAddress& address, Socket& abortSocket, Socket& abortSocket6)
at System.Net.PooledStream.Activate(Object owningObject, Boolean async, GeneralAsyncDelegate asyncCallback)
at System.Net.ConnectionPool.GetConnection(Object owningObject, GeneralAsyncDelegate asyncCallback, Int32 creationTimeout)
at System.Net.Mail.SmtpConnection.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpTransport.GetConnection(ServicePoint servicePoint)
at System.Net.Mail.SmtpClient.GetConnection()