.NET進階學習之使用ASP.NET兩種發送郵件的方法

來源:互聯網
上載者:User

如果你的ASP.NET程式學得不錯了,想用ASP.NET來發送郵件,在網上有好多文章,本站不管網上說是如何?的,下面整理兩種.NET發送郵件的方法給你參考一下,很簡單的哦。

第一種:

1、設定檔(Web.config) + 少量C#代碼

在 configuration 節點下添加

<system.net>
<mailSettings>
<smtp from="baidu@163.com">
<network host="smtp.163.com" password="baidu" port="25" userName="baidu" defaultCredentials="false"/>
</smtp>
</mailSettings>
</system.net>
/// <summary>
/// 發送郵件
/// </summary>
private void SendMail()
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress("baidu@163.com");//發送人郵箱地址,與smtp節點中的from值一致
message.To.Add(new System.Net.Mail.MailAddress("taobao@163.com"));//接收人郵箱地址
message.Subject = "hello";
message.Body = "<b>taobao</b>";
message.IsBodyHtml = true;

System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient();
smtpclient.Send(message);
}

好,介紹完第一種發送郵件的方法,下面看第二種。

/// <summary>
/// 發送郵件
/// </summary>
private void SendMail()
{
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.From = new System.Net.Mail.MailAddress("baidu@163.com");//發送人郵箱地址,與smtp節點中的from值一致
message.To.Add(new System.Net.Mail.MailAddress("taobao@163.com"));//接收人郵箱地址
message.Subject = "hello";
message.Body = "<b>taobao</b>";
message.IsBodyHtml = true;

System.Net.Mail.SmtpClient smtpclient = new System.Net.Mail.SmtpClient("smtp.163.com", 25);
smtpclient.Credentials = new System.Net.NetworkCredential("baidu", "baidu");//參數分別是信箱使用者名和密碼
smtpclient.Send(message);
}

 

怎樣,上面兩種發送郵件的方法都很簡單吧。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.