前一段时间,由于网站需要做找回密码的功能,需要给往用户的邮箱中发送邮件,在网上找了段代码,写上去之后,发现邮件老是跑到垃圾箱中,我在网上试图找到原因,网上说的好多都是什么词语过滤问题,一直没有找到满意答案,后来晚上回家之后又找了段代码两个代码对比,发现我之前写的少了一句,在百度上一搜原来那句是设置邮件的优先级别,试了试加进代码中,果然问题解决了,现在晒晒代码。
public static bool SendMail(string ToEmail, string pwd)
{
//你的邮箱号码(163邮箱)
string Email = "邮箱号";
//邮箱密码
string password = "密码";
//编码方式
Encoding EnCode = Encoding.UTF8;
//实例化一个邮件信息
System.Net.Mail.MailMessage Message = new System.Net.Mail.MailMessage();
Message.From = new MailAddress(Email, "中国出境旅游领队网", EnCode);
Message.To.Add(new MailAddress(ToEmail, "尊敬的用户", EnCode));
Message.Subject = "";
//将邮件的优先级别设置到最高,防止邮件丢入到垃圾箱
Message.Priority = System.Net.Mail.MailPriority.High;
Message.SubjectEncoding = EnCode;
StringBuilder MailContent = new StringBuilder();
MailContent.Append("尊敬的用户:<br/>");
MailContent.Append(" 您好!你于");
MailContent.Append(DateTime.Now.ToLongTimeString());
MailContent.Append("通过<a href='http://localhost:4932/default.aspx'>中国出境旅游领队网</a>审请找回密码。");
MailContent.Append("<br/> 您的密码为" + pwd + ",请牢记您的密码。为了安全起见,请用户点登录后进入用户中心修改密码!");
string url = "http://localhost:4932/default.aspx";
MailContent.Append("<br/> <a href='" + url + "'>" + url + "</a>");
//MailContent.Append(pwd);
Message.Body = MailContent.ToString();
Message.BodyEncoding = EnCode;
Message.IsBodyHtml = true;
try
{
SmtpClient smtp = new SmtpClient("smtp.163.com", 25);
smtp.Credentials = new NetworkCredential(Email, password);
smtp.DeliveryMethod = SmtpDeliveryMethod.Network;
smtp.Send(Message);
}
catch (Exception)
{
return false;
}
finally
{
Message.Dispose();
}
return true;
}