使用Gmail发送邮件心得

 /// <summary>
/// 发送邮件
/// </summary>
/// <returns></returns>
public static bool SendEmail(string subject, string body, string to)
{
MailMessage myEmail = new MailMessage();
Encoding eEncod = Encoding.GetEncoding("utf-8");
myEmail.From = new System.Net.Mail.MailAddress(SmtpUserName, SmtpUserName, eEncod);
myEmail.To.Add(to);
myEmail.Subject = subject;
myEmail.Body = body;
myEmail.BodyEncoding = Encoding.UTF8;
myEmail.Priority = System.Net.Mail.MailPriority.Normal;
myEmail.IsBodyHtml = true;

SmtpClient smtp = new SmtpClient(SmtpServer, 587);
smtp.EnableSsl = true;
string UserName = SmtpUserName;
string Password = SmtpPassword;
smtp.Credentials = new System.Net.NetworkCredential(UserName, Password);
try
{
smtp.Send(myEmail);
return true;
}
catch (Exception ex)
{
return false;
}
return true;
}

注意:

使用gmail发送邮件写端口,但是当把端口设为465时,会报超时,所以只能设为587

上一篇:Vim快捷输出查找寄存器的内容(去除\<,\>和\V)


下一篇:nodejs开发微信1——微信路由设置a(access_token和tickets)