天天看點

使用System.Net.Mail類實作郵件發送

     如果提供用于基本身份驗證的憑據,則憑據将以明文形式發送到伺服器。這可能會産生安全問題,因為憑據可以被他人看見進而加以利用。但是向163的郵箱發送郵件是必須要帶上發送者的憑據資訊,否則郵件是發送不出去的。

     SmtpClient也可以這樣建立:SmtpClient client=new SmtpClient(smtp_server),因為郵件發送伺服器的預設端口是25。

     1、QQ郵箱向163郵箱發送郵件

使用System.Net.Mail類實作郵件發送
使用System.Net.Mail類實作郵件發送

Code

private static void TimeEvent(object source, ElapsedEventArgs e)

{

    string smtp_server="smtp.qq.com";

    int port = 25;

    string mail_from = "***from_mail_username***@qq.com";

    string sender = "***from_mail_username***";

    string mail_to = "***to_mail_username***@163.com";

    string receiver = "***to_mail_username***@";

    string subject = "Title Test"

    string body = "Content Test";

    try

    {

    SendEmail(smtp_server, port, mail_from, sender, mail_to, receiver, subject, body);

    }

    catch(Exception ex)

    MessageBox.Show(ex.Message);

}

public static void SendEmail(string smtp_server, int port, string mail_from, string sender, string mail_to, string receiver, string subject, string body)

    MailAddress from = new MailAddress(mail_from, sender);

    MailAddress to = new MailAddress(mail_to, receiver);

    MailMessage message = new MailMessage(from, to);

    message.BodyEncoding = Encoding.UTF8;

    message.IsBodyHtml = true;

    message.Subject = subject;

    message.Body = body;

    SmtpClient client = new SmtpClient(smtp_server, port);   

    client.Credentials = new NetworkCredential("***from_mail_username***@qq.com", "***pwd***");

    client.Send(message);

     2、163郵箱向163郵箱發送郵件

使用System.Net.Mail類實作郵件發送
使用System.Net.Mail類實作郵件發送

    string smtp_server="smtp.163.com";

    string mail_from = "***from_mail_username***@163.com";

    string receiver = "***to_mail_username***";

    SmtpClient client = new SmtpClient(smtp_server);   

    client.Credentials = new NetworkCredential("***from_mail_username***@163.com", "***pwd***");

     3、163郵箱向QQ郵箱發送郵件

使用System.Net.Mail類實作郵件發送
使用System.Net.Mail類實作郵件發送

    string mail_to = "***to_mail_username***@qq.com";

     3、QQ郵箱向QQ郵箱發送郵件

使用System.Net.Mail類實作郵件發送
使用System.Net.Mail類實作郵件發送

<a target="_blank" href="http://www.cnblogs.com/lhb25/p/must-read-links-for-web-designers-and-developers-volume-12.html">Web 前端工程師和設計師必讀精華文章推薦</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/07/28/html5-awesome-single-page-sites-inspiration.html" target="_blank">酷!15個精美的 HTML5 單頁網站作品欣賞</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/11/22/best-awesome-css3-animation-demos.html">炫!35個讓人驚訝的 CSS3 動畫效果示範</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/03/02/30-mind-blowing-parallax-scrolling-effect-websites.html" target="_blank">贊!30個與衆不同的優秀視差滾動效果網站</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2012/01/13/25-outstanding-single-page-website-designs.html">靓å!25個優秀的國外單頁網站設計作品欣賞</a>

<a target="_blank" href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/09/awesome-html5-and-javascript-effects.html">帥!8個驚豔的 HTML5 和 JavaScript 特效</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/06/27/35-exclusive-rainbow-colored-flash-websites.html" target="_blank">頂!35個很漂亮的國外 Flash 網站作品欣賞</a>

<a href="http://www.cnblogs.com/lhb25//lhb25/archive/2011/08/24/outstanding-admin-panels-part-one.html" target="_blank">哇!34個漂亮網站和應用程式背景管理界面</a>

使用System.Net.Mail類實作郵件發送

<a href="http://www.yyyweb.com/go/web" target="_blank">本部落格新站點 ◆ 前端裡 ◆ 歡迎圍觀:)</a>

歡迎任何形式的轉載,但請務必注明出處。

繼續閱讀