各位大神好,我最近遇到一个问题,搞了我差不多一个月,今天算是解决了,但是却不知道其中原理,请各位大神帮忙解惑。
我遇到的问题是:购买的阿里云windows服务器部署网站后,网站发送邮件总是失败,本地没有问题。
一开始的代码如下,使用的是System.Net.Mail
string smtpServer = "smtp.163.com"; //SMTP服务器
string mailFrom = ConfigHelper.GetString("from163Email"); //登陆用户名
string userPassword = ConfigHelper.GetString("pwd163Email");//登陆密码
// 邮件服务设置
SmtpClient smtpClient = new SmtpClient();//();
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;//指定电子邮件发送方式
smtpClient.Host = smtpServer; //指定SMTP服务器
if (isBySsl)
{
smtpClient.EnableSsl = true;
smtpClient.Port = int.Parse(ConfigHelper.GetString("sslPort"));
}
smtpClient.UseDefaultCredentials = false;
smtpClient.Credentials = new System.Net.NetworkCredential(mailFrom, userPassword);//用户名和密码
// 发送邮件设置
MailMessage mailMessage = new MailMessage(mailFrom, mailTo); // 发送人和收件人
mailMessage.Subject = mailSubject;//主题
mailMessage.Body = mailContent;//内容
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;//正文编码
mailMessage.IsBodyHtml = true;//设置为HTML格式
mailMessage.Priority = MailPriority.Low;//优先级
try
{
smtpClient.Send(mailMessage); // 发送邮件
25端口被禁了,但是ssl加密的465端口也一直发送不出去,网上说改成587也还是不行。
后面改用了另一个类库System.Web.Mail,代码如下
System.Web.Mail.MailMessage mmsg = new System.Web.Mail.MailMessage();
//邮件主题
mmsg.Subject = subject;
mmsg.BodyFormat = System.Web.Mail.MailFormat.Html;
//邮件正文
mmsg.Body = body;
//正文编码
mmsg.BodyEncoding = Encoding.UTF8;
//优先级
mmsg.Priority = System.Web.Mail.MailPriority.Normal;
//发件者邮箱地址
mmsg.From = ConfigHelper.GetString("from163Email");
//收件人收箱地址
mmsg.To = Tomail;
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1");
//用户名
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", ConfigHelper.GetString("from163Email"));
//密码
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", ConfigHelper.GetString("pwd163Email"));
//端口
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", isBySsl ? "465" : "25");
//是否ssl
mmsg.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", isBySsl ? "true" : "false");
//Smtp服务器
System.Web.Mail.SmtpMail.SmtpServer = "smtp.163.com";
try
{
System.Web.Mail.SmtpMail.Send(mmsg);
这次竟然发送成功了。按理说,net2.0以后的版本用的应该是net类库的,web类库已经过时了。但实际是net类库的发送不了,web类库的可以,有大神指点一下吗?谢谢了
这是我的目标框架: