Reference:http://www.dotblogs.com.tw/phoenix7765/archive/2008/11/14/5987.aspx

using System; 
using System.Net; 
using System.Net.Mail; 
using System.Text; 
using System.Web.UI;     

namespace WebApplication 

  public partial class PhoenixMail : BasePage 
  { 
    protected void Page_Load(object sender, EventArgs e) 
    {     
    } 
   
    protected void btnSend_Click(object sender, ImageClickEventArgs e) 
    { 
      string[] emaillist = txtTo.Text.Split(',');                                 //將郵件字串分離
      AsyncSendMail delt = new AsyncSendMail(SendMailAsync);                      //非同步執行
      AsyncCallback acb = new AsyncCallback(SendMailCallBack);                    //非同步執行
      IAsyncResult isr = delt.BeginInvoke(txtFrom.Text, emaillist, acb, delt);    //非同步執行
      ReturnLiteral.Text = "郵件已加入工作排程,完成後會寄送一封通知信給您,謝謝"; 
    } 
    
    public string SendMailAsync(string From, string[] To)                       //非同步執行方法主體
    { 
      StringBuilder sb = new StringBuilder(); 
      System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();       //宣告一個計時器
      sb.AppendFormat("<p>起始時間 - {0} - </p>", DateTime.Now);                 
      sb.AppendFormat("<p>總共{0}封郵件<p>", To.Length); 

      int Success = 0; 
      int Failure = 0; 

      SmtpClient emailClient = new SmtpClient("YourMail.com");                        //設定SMTP位址
      emailClient.Credentials = new NetworkCredential("YourAccount", "YourPassword"); //設定帳號密碼
      sw.Reset();    //開始計時器
      sw.Start();    //開始計時器
      for (int i = 0; i < To.Length; i++)              //一一寄出郵件
      { 
        try
        { 
          MailMessage message = new MailMessage() 
          { 
            Body = txtBody.Text, 
            BodyEncoding = Encoding.UTF8, 
            From = new MailAddress(From), 
            IsBodyHtml = true, 
            Subject = txtSubject.Text, 
            SubjectEncoding = Encoding.UTF8 
          }; 
          message.To.Add(To[i]); 
          emailClient.Send(message); 
          Success++; 
          message.Dispose(); 
        } 
        catch
        { 
          sb.AppendFormat("<p>寄送失敗的郵件 - {0}</p>", To[i]); 
          Failure++; 
          continue; 
        } 
      } 
        sw.Stop();  //計時器停止
        emailClient = null; 
        sb.AppendFormat("<p>成功郵件數量:{0}</p><p>失敗郵件數量:{1}<p>", Success, Failure); 
        sb.AppendFormat("<p>結束時間 - {0} - </p>", DateTime.Now); 
        sb.AppendFormat("<p>總共時數 - {0}小時{1}分{2}秒{3}毫秒</p>", sw.Elapsed.Hours, sw.Elapsed.Minutes, sw.Elapsed.Seconds, sw.Elapsed.Milliseconds); 
        return sb.ToString();     //回傳訊息
    } 
    public delegate string AsyncSendMail(string From, string[] To);    //宣告一個非同步委派的方法(與非同步主體簽名碼相同)

    public void SendMailCallBack(IAsyncResult isr)    //非同步回傳訊息的方法
    { 
      AsyncSendMail asm = (AsyncSendMail)isr.AsyncState; 
      string result = asm.EndInvoke(isr); 
      SmtpClient emailClient = new SmtpClient("YourMail.com"); 
      emailClient.Credentials = new NetworkCredential("YourAccount", "YourPassword"); 
      MailMessage ResultMail = new MailMessage("
YourAccount@YourMail.com", "YourAccount@YourMail.com", "郵件寄送完成通知信", result); 
      ResultMail.IsBodyHtml = true; 
      emailClient.Send(ResultMail); 
      emailClient = null; 
      ResultMail.Dispose(); 
    } 
  } 
}

請自行修改"YourMail.com" "YourAccount" "YourPassword" 等等

以上 Phoenix 2008/11/14

arrow
arrow
    全站熱搜
    創作者介紹
    創作者 就是要紅 的頭像
    就是要紅

    就是要紅

    就是要紅 發表在 痞客邦 留言(0) 人氣()