.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()
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()
...全文
相关推荐
.Net Core WebApi部署到Windows服务器上的步骤
主要介绍了.Net Core WebApi部署到Windows服务器上的步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
.Net Core WebApi部署在Linux服务器上的方法
鸽了好久,终于有个时间继续写了,继上一篇之后,又写(水)了一篇,有什么不足之处请大家指出,多谢各位了。 下面有两个需要用到的软件,putty和pscp,我已经上传到博客园了,下载请点击这里。 一、准备服务器 首先和之前一样,先去腾讯云整了个云服务器,选择CentOS的镜像。 然后跟之前一样完成购买,得到公网的IP地址。 接着我们复制下来公网的IP地址,我这里使用了putty进行远程登录。 输入root用户名和你之前设置的密码进行登录。 然后,我们就可以安装.Net Core的运行环境了。 二、安装.Net Core环境 首先去.Net微软官网找到对应的版本。
发邮件系统 ASP.NET
一个简单的 发邮件系统,还在完善中 现在 只能 用smtp.163.com
asp.net/aspx本地测试工具一键本地测试asp.net程序
asp.net/aspx本地测试工具|一键本地测试asp.net程序,可以在本地测试任何asp.net程序!
把ASP.NET MVC项目部署到本地IIS上的完整步骤
最近会经常修改一些网站前端的内容,为了方便跟UI和产品交流,需要将自己修改过的页面及时发布到测试机或者是本地的IIS上。下面这篇文章主要给大家介绍了关于如何把ASP.NET MVC项目部署到本地IIS上的相关资料,需要的朋友可以参考下


