Created
January 1, 2017 08:22
-
-
Save songzheng45/1d26fae164e852de5d833e1f79b48eed to your computer and use it in GitHub Desktop.
C#: 发送邮件
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System.Net.Mail; | |
using System.Text; | |
public static class EmailUtil | |
{ | |
/// <summary> | |
/// 发送邮件(无附件) | |
/// </summary> | |
/// <remarks> | |
/// 如果抛出异常信息:“服务器响应为:5.7.1 [xxx@abc.com]: Relay access denied” | |
/// 则需要邮件服务器将客户端IP添加到信任。 | |
/// </remarks> | |
/// <param name="title">邮件标题</param> | |
/// <param name="content">邮件内容</param> | |
/// <param name="receiverEmail">接受人邮箱地址</param> | |
public static bool SendEmail(string title, string content, string receiverEmail) | |
{ | |
string strAccount = "xxx@gmail.com"; | |
string strPwd = "your_password"; | |
// 邮件服务器 | |
var smtpServer = "smtp.gmail.com"; | |
var smtpClient = new SmtpClient | |
{ | |
DeliveryMethod = SmtpDeliveryMethod.Network, | |
Host = smtpServer, | |
Credentials = new System.Net.NetworkCredential(strAccount, strPwd), | |
}; | |
var mailMessage = new MailMessage(strAccount, receiverEmail) | |
{ | |
Subject = title, | |
Body = content, | |
BodyEncoding = Encoding.UTF8, | |
IsBodyHtml = true, | |
Priority = MailPriority.High | |
}; | |
smtpClient.Send(mailMessage); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment