C# QQ邮箱授权码发送邮件

using System.Net;
using System.Web.Mail;

    public class SendMail
{
/// <summary>
/// 发送Email
/// </summary>
public static void Send(string strTo, string Subject, string Body)
{
string smtpserver = "smtp.qq.com";
string userName = "";
string pwd = "";
string nickName = "服务器在线预警系统";
string strFrom = "";//收件人
sendMail(smtpserver, userName, pwd, nickName, strFrom, strTo, Subject, Body);
} /// <summary>
/// 发送电子邮件
/// </summary>
/// <param name="smtpserver">SMTP服务器</param>
/// <param name="userName">登录帐号</param>
/// <param name="pwd">登录密码</param>
/// <param name="nickName">发件人昵称</param>
/// <param name="strFrom">发件人</param>
/// <param name="strTo">收件人</param>
/// <param name="Subject">主题</param>
/// <param name="Body">内容</param>
public static bool sendMail(string smtpserver, string userName, string pwd, string nickName, string strFrom, string strTo, string Subject, string Body)
{
System.Web.Mail.MailMessage mail = new System.Web.Mail.MailMessage();
try
{
pwd = DataEncrypt.Decrypt(pwd, "KYCMS"); mail.To = strTo;
mail.From = strFrom;
mail.Subject = Subject;
mail.BodyFormat = System.Web.Mail.MailFormat.Html;
mail.Body = Body; mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate", "1"); //身份验证
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendusername", mail.From); //邮箱登录账号,这里跟前面的发送账号一样就行
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/sendpassword", pwd); //这个密码要注意:如果是一般账号,要用授权码;企业账号用登录密码
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpserverport", 465);//端口
mail.Fields.Add("http://schemas.microsoft.com/cdo/configuration/smtpusessl", "true");//SSL加密
SmtpMail.SmtpServer = smtpserver; //企业账号用smtp.exmail.qq.com
SmtpMail.Send(mail);
return true; //邮件发送成功
}
catch (Exception ex)
{
//失败,错误信息:ex.Message;
}
return false;
}
}

  

上一篇:HDFS中NameNode启动过程


下一篇:新霸哥带你进入java的世界