【轉】C#發送Email郵件

來源:互聯網
上載者:User

標籤:style   blog   http   color   os   使用   sp   strong   on   

轉自:http://hi.baidu.com/bluesky_cn/item/8bb060ace834c53f020a4df2

下面用到的郵件帳號和密碼都不是真實的,需要測試就換成自己的郵件帳號。

需要引用:

1 using System.Net.Mail;2 using System.Text;3 using System.Net;

程式碼:

 1         MailMessage myMail = new MailMessage(); //建立郵件執行個體對象 2         myMail.From = new MailAddress(""); //寄件者,要和郵件伺服器的驗證資訊對應,不能隨便更改 3          4         myMail.To.Add(new MailAddress("")); //接收者 5         myMail.Subject = "C#發送Email"; //郵件標題 6         myMail.SubjectEncoding = Encoding.UTF8; //標題編碼 7         myMail.Body = "this is a test email!"; //郵件內容 8         myMail.BodyEncoding = Encoding.UTF8; //郵件內容編碼 9         myMail.IsBodyHtml = true;     //郵件內容是否支援html10         SmtpClient smtp = new SmtpClient(); //建立smtp執行個體對象11         smtp.Host = "mail.sina.com"; //郵件伺服器SMTP12         smtp.Port = 25;                  //郵件伺服器連接埠13         smtp.Credentials = new NetworkCredential("", "123456"); //郵件伺服器驗證資訊14         smtp.Send(myMail); //發送郵件

使用Gmail郵箱發送郵件樣本

 1         MailMessage myMail = new MailMessage(); 2         myMail.From = new MailAddress(""); 3         myMail.To.Add(new MailAddress("")); 4         myMail.Subject = "C#發送Email"; 5         myMail.SubjectEncoding = Encoding.UTF8; 6         myMail.Body = "this is a test email from gmail!<a href=‘http://www.sina.com.cn‘>sina</a>"; 7         myMail.BodyEncoding = Encoding.UTF8; 8         myMail.IsBodyHtml = true; 9         SmtpClient smtp = new SmtpClient();10         smtp.Host = "smtp.gmail.com";11         smtp.Port = 587; //Gmail的smtp連接埠12         smtp.Credentials = new NetworkCredential("", "123456");13         smtp.EnableSsl = true; //Gmail要求SSL串連14         smtp.DeliveryMethod = SmtpDeliveryMethod.Network; //Gmail的發送方式是通過網路的方式,需要指定15         smtp.Send(myMail);

使用QQ郵箱發送郵件樣本

 1         MailMessage myMail = new MailMessage(); 2         myMail.From = new MailAddress(""); 3         myMail.To.Add(new MailAddress("")); 4         myMail.Subject = "C#發送Email"; 5         myMail.SubjectEncoding = Encoding.UTF8; 6         myMail.Body = "this is a test email from QQ!"; 7         myMail.BodyEncoding = Encoding.UTF8; 8         myMail.IsBodyHtml = true; 9         SmtpClient smtp = new SmtpClient();10         smtp.Host = "smtp.qq.com";11         smtp.Credentials = new NetworkCredential("", "123456");12         smtp.Send(myMail);

 

【轉】C#發送Email郵件

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.