You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
13 KiB

  1. using OT.COM.LogisticsUtil;
  2. using OT.COM.SignalerMessage;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Linq;
  6. using System.Net;
  7. using System.Net.Mail;
  8. using System.Text;
  9. using System.Text.RegularExpressions;
  10. namespace CounsellorBL
  11. {
  12. public class MailService : MessageBase
  13. {
  14. Util u = new Util();
  15. public CResponseMessage SendMail(CRequestMessage i_crm)
  16. {
  17. CResponseMessage crm = null;
  18. string sMsg = null;
  19. try
  20. {
  21. do
  22. {
  23. string sSubject = _fetchString(i_crm, BLWording.SUBJECT);
  24. string sBody = _fetchString(i_crm, BLWording.BODY);
  25. string sSender = _fetchString(i_crm, BLWording.SENDERNAME);
  26. string sReceiver = _fetchString(i_crm, BLWording.RECEIVERS);
  27. string sUseSSL = u.GetSettingString("USESSL");
  28. Mailer m = new Mailer();
  29. m.SMTP = u.GetSettingString("SMTPURL"); // "smtp.gmail.com";
  30. m.FromAddress = u.GetSettingString("MAILOWNERADDRESS"); //"ot.syssender@gmail.com";
  31. m.Account = u.GetSettingString("MAILACCOUNT"); //"ot.syssender@gmail.com";
  32. m.Password = u.GetSettingString("MAILPASS"); // "XXXXXX";
  33. m.Name = sSender;
  34. m.Port = Convert.ToInt32(u.GetSettingString("MAILPORT")); // "587"
  35. m.SSL = sUseSSL != null ? sUseSSL.ToLower().StartsWith("y") : false;
  36. m.MyEncoding = Encoding.GetEncoding("UTF-8");
  37. string [] saMailAddress = sReceiver.Split(",;".ToCharArray());
  38. List<Mailer.MailAccountInfo> l = new List<Mailer.MailAccountInfo>();
  39. foreach (string sMailAddress in saMailAddress)
  40. {
  41. l.Add(new Mailer.MailAccountInfo() { EMail = sMailAddress });
  42. }
  43. sMsg = m.Send(sSubject, sBody, l);
  44. if (null != sMsg)
  45. {
  46. break;
  47. }
  48. crm = new CSuccessResponseMessage(null, i_crm) { MSG = "SEND SUCCESS" };
  49. }
  50. while (false);
  51. }
  52. catch (Exception e)
  53. {
  54. sMsg = u.GetLastExceptionMsg(e);
  55. }
  56. if (null != sMsg)
  57. {
  58. crm = new CResponseMessage(i_crm) { RESULT = EResponseResult.RR_FALSE, MSG = sMsg };
  59. }
  60. return crm;
  61. }
  62. /// <summary>
  63. /// 同步發送電子郵件
  64. /// </summary>
  65. /// <param name="o_myMessage">電子郵件</param>
  66. /// <returns></returns>
  67. public bool SendMailNET(System.Net.Mail.MailMessage o_myMessage)
  68. {
  69. try
  70. {
  71. //mo_Log.Debug("發送郵件開始");
  72. //重構在 GetEmailConfig()
  73. System.Net.Mail.SmtpClient o_myMailServer = GetEmailConfig(); //new SmtpClient(sServer);//寄件服務器地址,端口
  74. System.Net.Mail.MailAddress o_MailFrom = new System.Net.Mail.MailAddress("service@origtek-mail.com", o_myMessage.From.DisplayName);
  75. //Add by Alina 需要把發件者的信息添加到抄送人中
  76. Regex reg = new Regex(@"(\w*)(\()*");
  77. string strFromName = o_myMessage.From.DisplayName;//人員名稱
  78. Match m = reg.Match(strFromName);
  79. strFromName = m.Groups[1].Value;
  80. string strFromMail = o_myMessage.From.Address;//人員Mail
  81. System.Net.Mail.MailAddress o_MailTo = new System.Net.Mail.MailAddress(strFromMail, strFromName);
  82. o_myMessage.CC.Add(o_myMessage.From);//把發件者的信息添加到抄送人中
  83. o_myMessage.From = o_MailTo;//發件者的信息
  84. //發送Mail
  85. //mo_Log.Debug("FormName:" + o_myMessage.From.DisplayName);
  86. //mo_Log.Debug("FormMail:" + o_myMessage.From.Address);
  87. o_myMailServer.Send(o_myMessage);
  88. return true;
  89. }
  90. catch (System.Net.Mail.SmtpException ex)
  91. {
  92. //ErrorMessages = ex.Message;
  93. //mo_Log.Error(ex);
  94. return false;
  95. }
  96. }
  97. #region SendMailNET_NoSelf
  98. /// <summary>
  99. /// 同步發送電子郵件
  100. /// </summary>
  101. /// <param name="o_myMessage">電子郵件</param>
  102. /// <returns></returns>
  103. public bool SendMailNET_NoSelf(System.Net.Mail.MailMessage o_myMessage)
  104. {
  105. try
  106. {
  107. //mo_Log.Debug("發送郵件開始");
  108. System.Net.Mail.SmtpClient o_myMailServer = GetEmailConfig(); //new SmtpClient(sServer);//寄件服務器地址,端口
  109. System.Net.Mail.MailAddress o_MailFrom = new System.Net.Mail.MailAddress("service@origtek-mail.com", o_myMessage.From.DisplayName);
  110. //發送Mail
  111. //mo_Log.Debug("FormName:" + o_myMessage.From.DisplayName + ";FormMail:" + o_myMessage.From.Address);
  112. o_myMailServer.Send(o_myMessage);
  113. return true;
  114. }
  115. catch (System.Net.Mail.SmtpException ex)
  116. {
  117. //ErrorMessages = ex.Message;
  118. //mo_Log.Error(ex);
  119. return false;
  120. }
  121. }
  122. #endregion
  123. #region "GetEmailConfig"
  124. /// <summary>
  125. /// 提供本類中其它公有郵件功能,取得基本smtp設定.
  126. /// </summary>
  127. /// <returns></returns>
  128. private SmtpClient GetEmailConfig()
  129. {
  130. string sMailEncoding = u.GetSettingString("MailEncoding"); //設置字集防止亂碼
  131. string sFromName = u.GetSettingString("FromName").Trim(); //顯示發件人的名字代替Mail地址
  132. string sFromEmail = u.GetSettingString("FromEmail").Trim(); //發件人地址
  133. string sFromUserid = u.GetSettingString("FromUserId").Trim(); //發件人的帳號
  134. string sFromPassword = u.GetSettingString("FromPassword").Trim(); //發件人的密碼
  135. string sServer = u.GetSettingString("Server").Trim(); //發送郵件服務器的IP地址
  136. int i32ServerPort = 0;
  137. int.TryParse(u.GetSettingString("ServerPort"), out i32ServerPort); //發送郵件服務器的端口
  138. int iTimeout = 0;
  139. int.TryParse(u.GetSettingString("Timeout"), out iTimeout); //發送郵件超時時間
  140. bool blSSLYES = u.GetSettingString("SSL").Trim().Equals("true") ? true : false;//是否開啟SSL驗證
  141. System.Net.Mail.SmtpClient o_myMailServer = new SmtpClient(sServer);//寄件服務器地址,端口
  142. o_myMailServer.Host = sServer;
  143. if (i32ServerPort != 0)
  144. {
  145. o_myMailServer.Port = i32ServerPort;
  146. }
  147. o_myMailServer.EnableSsl = blSSLYES;//是否開啟SSL驗證
  148. if (iTimeout != 0)
  149. {
  150. o_myMailServer.Timeout = iTimeout * 1000; //若郵件過大,請手動加長時間, 1000約等於1秒
  151. }
  152. if (!sFromPassword.Equals(""))
  153. {
  154. o_myMailServer.UseDefaultCredentials = false;
  155. o_myMailServer.Credentials = new System.Net.NetworkCredential(sFromUserid, sFromPassword);//登錄會員名和密碼
  156. }
  157. return o_myMailServer;
  158. }
  159. #endregion
  160. private class Mailer
  161. {
  162. public class MailAccountInfo
  163. {
  164. public string EMail { get; set; }
  165. public string Name { get; set; }
  166. }
  167. public string SMTP { get; set; }
  168. public string FromAddress { get; set; }
  169. public string Account { get; set; }
  170. public string Password { get; set; }
  171. public string Name { get; set; }
  172. public int Port { get; set; }
  173. public string ErrorMessage { get; set; }
  174. public bool SSL { get; set; }
  175. public Encoding MyEncoding { get; set; }
  176. /// <summary>
  177. /// 建構子
  178. /// </summary>
  179. public Mailer()
  180. {
  181. MyEncoding = Encoding.GetEncoding("UTF-8");
  182. }
  183. /// <summary>
  184. /// 寄信動作
  185. /// </summary>
  186. /// <param name="strSubject">主旨</param>
  187. /// <param name="strBody">信件內容</param>
  188. /// <param name="arrayStrToAddressAndName">收件者</param>
  189. /// <param name="bHTML"></param>
  190. /// <param name="arrayStrAttached">附件</param>
  191. /// <param name="arrayStrCCAddressAndName">CC</param>
  192. /// <param name="arrayStrBCCAddressAndName">BCC</param>
  193. /// <returns></returns>
  194. public string Send(string strSubject, string strBody, List<MailAccountInfo> arrayStrToAddressAndName,
  195. bool bHTML = true,
  196. List<MailAccountInfo> arrayStrCCAddressAndName = null,
  197. List<MailAccountInfo> arrayStrBCCAddressAndName = null)
  198. {
  199. string sMsg = null;
  200. try
  201. {
  202. System.Net.Mail.MailMessage mm = new System.Net.Mail.MailMessage();
  203. if (string.IsNullOrWhiteSpace(this.FromAddress) == false)
  204. {
  205. mm.Sender = new System.Net.Mail.MailAddress(string.Format("{1}<{0}>", this.FromAddress, this.Name));
  206. mm.From = new System.Net.Mail.MailAddress(string.Format("{1}<{0}>", this.FromAddress, this.Name));
  207. }
  208. mm.Subject = strSubject;
  209. mm.IsBodyHtml = bHTML;
  210. mm.Body = strBody;
  211. mm.BodyEncoding = this.MyEncoding;
  212. //加入收件者
  213. if (arrayStrToAddressAndName != null)
  214. {
  215. for (int i = 0; i < arrayStrToAddressAndName.Count; i++)
  216. {
  217. MailAccountInfo mai = arrayStrToAddressAndName[i];
  218. if (null != mai.EMail)
  219. {
  220. mm.To.Add(mai.EMail);
  221. }
  222. }
  223. }
  224. //加入CC
  225. //string strCC = "";
  226. if (arrayStrCCAddressAndName != null)
  227. {
  228. for (int i = 0; i < arrayStrCCAddressAndName.Count; i++)
  229. {
  230. //strCC += (i == 0 ? "" : ",") + string.Format("{1}<{0}>", arrayStrCCAddressAndName[i, 0], arrayStrCCAddressAndName[i, 1]);
  231. MailAccountInfo mai = arrayStrCCAddressAndName[i];
  232. if (null != mai.EMail)
  233. {
  234. mm.CC.Add(mai.EMail);
  235. }
  236. }
  237. }
  238. //if (strCC != "")
  239. // mm.CC.Add(strCC);
  240. //string strBCC = "";
  241. //加入BCC
  242. if (arrayStrBCCAddressAndName != null)
  243. {
  244. for (int i = 0; i < arrayStrBCCAddressAndName.Count; i++)
  245. {
  246. //strBCC += (i == 0 ? "" : ",") + string.Format("{1}<{0}>", arrayStrBCCAddressAndName[i, 0], arrayStrBCCAddressAndName[i, 1]);
  247. MailAccountInfo mai = arrayStrBCCAddressAndName[i];
  248. if (null != mai.EMail)
  249. {
  250. mm.Bcc.Add(mai.EMail);
  251. }
  252. }
  253. }
  254. //if (strBCC != "")
  255. // mm.Bcc.Add(strBCC);
  256. System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient(this.SMTP, this.Port);
  257. if (!string.IsNullOrWhiteSpace(this.Account) && !string.IsNullOrWhiteSpace(this.Password))
  258. {
  259. smtp.Credentials = new NetworkCredential(this.Account, this.Password);
  260. }
  261. smtp.EnableSsl = this.SSL;
  262. mm.Body = strBody;
  263. smtp.Send(mm);
  264. mm.Dispose();
  265. }
  266. catch (Exception ex)
  267. {
  268. sMsg = new Util().GetLastExceptionMsg(ex);
  269. }
  270. return sMsg;
  271. }
  272. }
  273. }
  274. }