1. 程式人生 > >C#SMTP發郵件

C#SMTP發郵件

method 用戶 gpo cli str sin client user host

public static bool SendMailUse()
{
string host = "smtp.163.com";// 郵件服務器smtp.163.com表示網易郵箱服務器
string userName = "[email protected]";// 發送端賬號
string password = "a96606";// 發送端密碼(這個客戶端重置後的密碼)

SmtpClient client = new SmtpClient();
client.DeliveryMethod = SmtpDeliveryMethod.Network;//指定電子郵件發送方式
client.Host = host;//郵件服務器
client.UseDefaultCredentials = true;
client.Credentials = new System.Net.NetworkCredential(userName, password);//用戶名、密碼

string strfrom = userName;
string strto = "[email protected]";
//string strcc = "[email protected]";//抄送


string subject = "訂單標題";//郵件的主題
string body = "訂單標題";//發送的郵件正文

System.Net.Mail.MailMessage msg = new System.Net.Mail.MailMessage();
msg.From = new MailAddress(strfrom, "訂單");
msg.To.Add(strto);
//msg.CC.Add(strcc);

msg.Subject = subject;//郵件標題
msg.Body = body;//郵件內容
msg.BodyEncoding = System.Text.Encoding.UTF8;//郵件內容編碼
msg.IsBodyHtml = true;//是否是HTML郵件
msg.Priority = MailPriority.High;//郵件優先級

try
{
client.Send(msg);
return true;
}
catch (System.Net.Mail.SmtpException ex)
{
return false;
}
}

C#SMTP發郵件