.net System.Net.Mail 之用SmtpClient发送邮件 Demo
private static bool sendMail()
{
try
{
//接收人邮箱
string SendTo = "XXXXX@163.com";
//抄送人邮箱
string SendCC = "YYYYY@163.com";
MailMessage message = new MailMessage();
//发送人邮箱
message.From = new MailAddress(ConfigurationSettings.AppSettings["RMSMailFrom"]);
//分别获取接收人和抄送人的邮箱 可多个,需以;分割
if (!String.IsNullOrEmpty(SendTo))
{
string[] stList = SendTo.Split(new char[1] { ';' });
foreach (string s in stList)
{
if (!string.IsNullOrEmpty(s))
message.To.Add(s.Replace("\r", "").Replace("\n", "").Trim());
}
}
if (!String.IsNullOrEmpty(SendCC))
{
string[] ccList = SendCC.Split(new char[1] { ';' });
foreach (string s in ccList)
{
if (!string.IsNullOrEmpty(s))
message.CC.Add(s.Replace("\r", "").Replace("\n", "").Trim());
}
}
//邮件主题
message.Body = "亲,有个程序出错了哦!";
//邮件标题
message.Subject = "有程序报错啦";
//邮件正文的编码 设置为中文
message.BodyEncoding = Encoding.UTF8;
SmtpClient client = new SmtpClient();
//邮件发送人的账号,密码
client.Credentials = new NetworkCredential(ConfigurationSettings.AppSettings["MailAccount"], ConfigurationSettings.AppSettings["RMSMailAccountPWD"]);
//[SMTP服务器域名或IP地址]:例如:smtp.qq.com
client.Host = ConfigurationSettings.AppSettings["MailServer"];
client.Send(message);
return true;
}
catch (Exception ex)
{
Console.WriteLine("发送失败:" + ex.Message);
return false;
}
}