下面用到的郵件帳號和密碼都不是真實的,需要測試就換成自己的郵件帳號。
需要引用:
using System.Net.Mail;
using System.Text;
using System.Net;
程式碼:
MailMessage myMail = new MailMessage(); //建立郵件執行個體對象
myMail.From = new MailAddress("bluesky@sina.com"); //寄件者,要和郵件伺服器的驗證資訊對應,不能隨便更改
myMail.To.Add(new MailAddress("848594@qq.com")); //接收者
myMail.Subject = "C#發送Email"; //郵件標題
myMail.SubjectEncoding = Encoding.UTF8; //標題編碼
myMail.Body = "this is a test email!"; //郵件內容
myMail.BodyEncoding = Encoding.UTF8; //郵件內容編碼
myMail.IsBodyHtml = true; //郵件內容是否支援html
SmtpClient smtp = new SmtpClient(); //建立smtp執行個體對象
smtp.Host = "mail.sina.com"; //郵件伺服器SMTP
smtp.Port = 25; //郵件伺服器連接埠
smtp.Credentials = new NetworkCredential("bluesky@sina.com", "123456"); //郵件伺服器驗證資訊
smtp.Send(myMail); //發送郵件
使用Gmail郵箱發送郵件樣本
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress("bluesky@gmail.com");
myMail.To.Add(new MailAddress("444354@qq.com"));
myMail.Subject = "C#發送Email";
myMail.SubjectEncoding = Encoding.UTF8;
myMail.Body = "this is a test email from gmail!<a href='http://www.sina.com.cn'>sina</a>";
myMail.BodyEncoding = Encoding.UTF8;
myMail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.Port = 587; //Gmail的smtp連接埠
smtp.Credentials = new NetworkCredential("bluesky@gmail.com", "123456");
smtp.EnableSsl = true; //Gmail要求SSL串連
smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //Gmail的發送方式是通過網路的方式,需要指定
smtp.Send(myMail);
使用QQ郵箱發送郵件樣本
MailMessage myMail = new MailMessage();
myMail.From = new MailAddress("4443233@qq.com");
myMail.To.Add(new MailAddress("bluesky@gmail.com"));
myMail.Subject = "C#發送Email";
myMail.SubjectEncoding = Encoding.UTF8;
myMail.Body = "this is a test email from QQ!";
myMail.BodyEncoding = Encoding.UTF8;
myMail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.qq.com";
smtp.Credentials = new NetworkCredential("4443233@qq.com", "123456");
smtp.Send(myMail);
引用自:http://hi.baidu.com/%CA%D8%CD%FB%C0%B6%CC%EC/blog/item/6e5e4d60c9d6776c0d33fa3d.html