利用SmtpClient類發送郵件

前臺:

  1. <html xmlns="http://www.w3.org/1999/xhtml">
  2. <head runat="server">
  3.     <title></title>
  4. </head>
  5. <body>
  6.     <form runat="server" action="">
  7.         <asp:Button ID="Button1" runat="server" Text="發送" OnClick="Button1_Click" />
  8.     </form>
  9. </body>
  10. </html>

 

後臺:

  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11. using AjaxControlToolkit;
  12. using System.Web.Services;
  13. using System.Data.SqlClient;
  14. using System.Net;
  15. using System.Net.Mail;
  16. using System.Text;
  17. public partial class nameSpace08108_Default : System.Web.UI.Page
  18. {
  19.     protected void Page_Load(object sender, EventArgs e)
  20.     {
  21.         
  22.     }
  23.     protected void Button1_Click(object sender, EventArgs e)
  24.     {
  25.         try
  26.             {
  27.                 MailAddress mailFrom = new MailAddress("****@126.com");
  28.                 MailAddress mailTo = new MailAddress("****@126.com");
  29.                 MailMessage objMail = new MailMessage(mailFrom, mailTo);
  30.                 objMail.IsBodyHtml = false;
  31.                 objMail.Priority = MailPriority.High;
  32.                 objMail.Subject = "測試";
  33.                 objMail.BodyEncoding = Encoding.UTF8;
  34.                 objMail.Body = "你好!這是一個關於發送郵件的測試...";
  35.                 SmtpClient client = new SmtpClient("smtp.126.com");
  36.                 client.Credentials = new System.Net.NetworkCredential("用戶名""密碼");
  37.                 client.Send(objMail);
  38.             }
  39.             catch (Exception ex)
  40.             {
  41.                 throw new Exception(ex.Message);
  42.             }
  43.         
  44.     }
  45. }
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值