摘要:[C#]使用SMTP寄Mail
using System.Net.Mail;
public void SendMail()
{
//設定smtp主機
SmtpClient mySmtp = new SmtpClient("smtp.hinet.net");
//設定smtp帳密
mySmtp.Credentials = new System.Net.NetworkCredential("user", "password");
//信件內容
string pcontect = "string or html";
//設定mail內容
MailMessage msgMail = new MailMessage();
//寄件者
msgMail.From = new MailAddress("sys@hinet.net");
//收件者
msgMail.To.Add("user@hinet.net");
//主旨
msgMail.Subject = "信件主旨";
//信件內容(含HTML時)
AlternateView alt = AlternateView.CreateAlternateViewFromString(pcontect, null, "text/html");
msgMail.AlternateViews.Add(alt);
//寄mail
mySmtp.Send(msgMail);
}
註:寄HTML信時,在VS2005可設 msgMail.BodyFormat=MailFormat.Html,但在VS2008需使用AlternateView物件
參考資料
http://edu.uuu.com.tw/article/010724.htm
http://www.aspheute.com/english/20000918.asp
http://www.dotnetspider.com/resources/19750-HTML-email-with-images.aspx
http://www.example-code.com/csharp/email.asp
http://msdn.microsoft.com/zh-tw/library/system.net.mail.mailmessage(VS.90).aspx
http://msdn.microsoft.com/zh-tw/library/system.net.mail.alternateview(VS.90).aspx