異世界


2012年5月24日 星期四

利用 GMail 傳送 EMAIL

首先要擁有一個 GMail 帳號,並了解其主機及Port 號。

我們為gMail 建立一個 Class,命名為ClassGMail:

   1: using System;
   2: using System.Text;
   3: using System.Net.Mail;
   4: using System.ComponentModel;
   5:  
   6: namespace Cht_MobileNms
   7: {
   8:     class ClassGMail
   9:     {
  10:             /* gMail的 SMTP 主機及 Port*/
  11:             private System.Net.Mail.SmtpClient MySmtp;
  12:  
  13:             private MailMessage Mail;
  14:            
  15:             public string eMailAccount { private get; set; }
  16:             public string eMailPassword { private get; set; }
  17:  
  18:             public string mListDestAddr { private get; set; }
  19:             public string mASubject { private get; set; }
  20:             public string mABody { private get; set; }
  21:  
  22:             /* BackgroundWorker */
  23:             private System.ComponentModel.BackgroundWorker bwSendMail;
  24:             
  25:  
  26:             /// <summary>
  27:             /// 建構式 1
  28:             /// </summary>
  29:             /// <param name="eMailAccount"></param>
  30:             /// <param name="Password"></param>
  31:             public ClassGMail(string Account, string Password)
  32:             {
  33:                 /*建立 SmtpClient 物件 並設定 Gmail的smtp主機及Port */
  34:                 MySmtp = new System.Net.Mail.SmtpClient("smtp.gmail.com", 587);
  35:  
  36:                 /*設定你的帳號密碼*/
  37:                 eMailAccount = Account;
  38:                 eMailPassword = Password;
  39:  
  40:                 /*Gmial 的 smtp 使用 SSL*/
  41:                 MySmtp.Credentials = new System.Net.NetworkCredential(eMailAccount, eMailPassword);
  42:                 MySmtp.EnableSsl = true;
  43:  
  44:                 /* 背景執行物建初始化 */                
  45:                 this.bwSendMail = new System.ComponentModel.BackgroundWorker();
  46:                 this.bwSendMail.DoWork += new System.ComponentModel.DoWorkEventHandler(this.bwSendMail_DoWork);
  47:             }
  48:  
  49:             /// <summary>
  50:             /// 傳送 EMAIL
  51:             /// </summary>
  52:             /// <param name="ListDestAddr"></param>
  53:             /// <param name="ASubject"></param>
  54:             /// <param name="ABody"></param>
  55:             /// <returns></returns>
  56:             public void sendMailTo(String ListDestAddr, String ASubject, String ABody)
  57:             {
  58:                 mListDestAddr=ListDestAddr;
  59:                 mASubject=ASubject;
  60:                 mABody = ABody;
  61:  
  62:                 /* 背景執行 eMail 傳送 */
  63:                 try
  64:                 {
  65:                     bwSendMail.RunWorkerAsync(); 
  66:                 }
  67:                 catch 
  68:                 { 
  69:                 }
  70:             }
  71:  
  72:             private void bwSendMail_DoWork(object sender, DoWorkEventArgs e)
  73:             {
  74:                 PrivateSendMailTo(mListDestAddr, mASubject, mABody);
  75:             }
  76:  
  77:  
  78:             /// <summary>
  79:             /// 傳送 EMAIL
  80:             /// </summary>
  81:             /// <param name="ListDestAddr"></param>
  82:             /// <param name="ASubject"></param>
  83:             /// <param name="ABody"></param>
  84:             /// <returns></returns>
  85:             private void PrivateSendMailTo(String ListDestAddr, String ASubject, String ABody)
  86:             {
  87:                 try
  88:                 {
  89:                     //============================================================
  90:                     Mail = new MailMessage();
  91:                     Mail.From = new MailAddress(eMailAccount);
  92:                     Mail.Subject = ASubject.Trim().ToString();
  93:                     Mail.Body = ABody.Trim().ToString();
  94:  
  95:                     /* 分解多個 目的位址 */
  96:                     string[] AList = ListDestAddr.Split(';');
  97:                     int Cnt = AList.GetLength(0);
  98:  
  99:                     foreach (string Addr in AList)
 100:                     {
 101:                         if (Addr != "") { Mail.To.Add(Addr.Trim().ToString()); }
 102:                     }
 103:                     //============================================================
 104:  
 105:                     /*發送Email*/ 
 106:                     MySmtp.Send(Mail);
 107:                 }
 108:                 catch (Exception err)
 109:                 {                    
 110:                     // MessageBox.Show(err.Message);
 111:                     // throw (err);
 112:                 }
 113:             }
 114:  
 115:  
 116:     }
 117:  
 118:  
 119: }


建構一個GMail實體:


   1: /// <summary>
   2: /// 電子郵件
   3: /// </summary>
   4: ClassGMail infoMail = new ClassGMail("WXYZ@gmail.com", "1234567");   // 帳號及密碼作為參數


傳送eMmail:


   1: //.. 傳送 eMail 給相關人員
   2: if (MailAlarmBody.Length > 0)
   3: {
   4:     try
   5:     {
   6:         infoMail.sendMailTo(getEMailAddress(), "CHT_NMS", MailAlarmBody.ToString());
   7:     }
   8:     catch { }
   9: }


可以一次傳送給多人,可從資料庫中取得資料串接收件人。


   1: /*從 DB 取得 eMailAddress*/
   2:        private string getEMailAddress()
   3:        {
   4:            linq = new EbNoClassDataContext(cnString);
   5:  
   6:            var result = from info in linq.User
   7:                         select new
   8:                         {
   9:                             info.eMail
  10:                         };
  11:  
  12:            StringBuilder sb = new StringBuilder();
  13:            foreach (var addr in result)
  14:            {
  15:                sb.Append(addr.eMail.ToString() + ';');
  16:            }
  17:  
  18:            return (sb.ToString());
  19:        }

沒有留言:

張貼留言