在上一篇C# 郵件發送方法【webMail方式】中介紹了Webmail的郵件 發送方式,現在介紹下C#中使用NetMail發送郵件的方式,
測試代碼如下:
/// <summary>
/// NetMail方式測試通過
/// </summary>
private void TestSend()
{
System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
//收件人地址
mm.To.Add(new System.Net.Mail.MailAddress("[email protected]", "Name"));
//發件人地址
mm.From = new System.Net.Mail.MailAddress("[email protected]");
//這個可以不指定
//mm.Sender = new System.Net.Mail.MailAddress("[email protected]", "SenderName");、
mm.Subject = "This is Test Email";
mm.Body = "<h3>This is Testing SMTP Mail Send By Me</h3>";
mm.IsBodyHtml = true;
mm.Priority = System.Net.Mail.MailPriority.High; // 設置發送郵件的優先級
System.Net.Mail.SmtpClient smtCliend = new System.Net.Mail.SmtpClient();
//指定郵件服務器
smtCliend.Host = "smtp.sina.com";
//smtp郵件服務器的端口號
smtCliend.Port = 25;
//設置發件人郵箱的用戶名和地址,使用公共郵件服務器一般需要提供,不然發送不會成功
smtCliend.Credentials = new NetworkCredential("xxxxxxx", "xxxxxxx");
//指定郵件的發送方式
smtCliend.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
try
{
smtCliend.Send(mm);
}
catch (System.Net.Mail.SmtpException ex)
{
Response.Write(ex.Message);
}
catch (Exception ex)
{
Response.Write(ex.Message);
}
}