C# 用qq邮箱发邮件

 

一、在企业的QQ邮箱中开启POP3/SMTP服务

开启服务时,授权密码保存好。

 二、示例

public static string UserName = ""; // 企业邮箱
public static string UserPass = ""; //邮箱开启POP3/SMTP服务,开启后的授权码密码

/// <summary>
/// 向用户发送邮件
/// </summary>
/// <param name="ReceiveUser">接收邮件的用户</param>
/// <param name="MailTitle">发送标题</param>
/// <param name="valiCode">发送的验证码</param>
/// <returns>bool</returns>
public static bool sendMail(string ReceiveUser, string MailTitle, string valiCode)
{
//保存验证码
var authCode = valiCode;
UAP.Session.Set("authCode", valiCode);

MailAddress toMail = new MailAddress(ReceiveUser);
MailAddress fromMail = new MailAddress(UserName, "51流量神器");
MailMessage mail = new MailMessage(fromMail, toMail);
mail.Subject = MailTitle;
mail.IsBodyHtml = true;
mail.Body = "【" + MailTitle + "】您好,您的验证码是:" + valiCode + " ."; ;
SmtpClient client = new SmtpClient();
client.Host = "smtp.exmail.qq.com";
client.Port = 587; //25 端口已被阿里云全面封杀
client.EnableSsl = true; //使用安全套接字层(SSL)加密连接
client.UseDefaultCredentials = false;
client.Credentials = new NetworkCredential(UserName, UserPass);
client.DeliveryMethod = SmtpDeliveryMethod.Network;  //邮件通过网络的方式传输
try
{
client.Send(mail);
return true;
}
catch(Exception ex)
{
Extend.WriteTextLog("SendEmail", ex.Message, DateTime.Now, "Email_Error"); //打印日志
throw;
}
}

先设置EnableSsl 为true;

在设置UseDefaultCredentials 为false。

三、在Global.asax文件中设置账户密码,方便以后修改企业邮箱。

 

posted @ 2018-10-10 16:06  阿刁_silver  阅读(154)  评论(0编辑  收藏  举报