/// <summary> /// SendMail 發送郵件 /// </summary> /// <param name="mailToArray">對方郵件地址</param> /// <param name="subject">主旨</param> /// <param name="body">內容</param> /// <returns></returns> public static bool SendMail(string[] mailToArray, string subject, string body) { SmtpClient s = ConfigSmtp.CreationSmtpClient(); NetworkCredential nc = (NetworkCredential)s.Credentials; MailMessage smail = ConfigSmtp.CreationMailMessage(); bool resilt = false; try { if (CheckMailTo(mailToArray)) { throw MailToAddressException(); } smail.Subject = subject.Trim(); smail.Body = body.Trim(); foreach (string address in mailToArray) { smail.To.Add(new MailAddress(address.Trim())); } s.Send(smail); resilt = true; } catch (System.Net.Mail.SmtpException ex) { throw ex; } finally { smail.Dispose(); } return(resilt); }
/// <summary> /// SendMail 發送郵件 /// </summary> /// <param name="mailToArray">對方郵件地址</param> /// <param name="fromMail">來源地址</param> /// <param name="fromDisplayName">來源地址別名</param> /// <param name="subject">主旨</param> /// <param name="body">內容</param> /// <returns></returns> public static bool SendMail(string[] mailToArray, string fromMail, string fromDisplayName, string subject, string body) { SmtpClient s = ConfigSmtp.CreationSmtpClient(); NetworkCredential nc = (NetworkCredential)s.Credentials; MailMessage smail = ConfigSmtp.CreationMailMessage(); bool resilt = false; if (string.IsNullOrWhiteSpace(fromDisplayName) && string.IsNullOrWhiteSpace(fromMail)) { smail.From = new MailAddress(nc.UserName, ConfigSmtp.ServiceMailDiaplayName); } else if (fromDisplayName != string.Empty && string.IsNullOrWhiteSpace(fromMail)) { smail.From = new MailAddress(nc.UserName, fromDisplayName); } else if (string.IsNullOrWhiteSpace(fromDisplayName) && fromMail != string.Empty) { smail.From = new MailAddress(fromMail, ConfigSmtp.ServiceMailDiaplayName); } else if (fromDisplayName != string.Empty && fromMail != string.Empty) { smail.From = new MailAddress(fromMail, fromDisplayName); } try { if (CheckMailTo(mailToArray)) { throw MailToAddressException(); } smail.Subject = subject.Trim(); smail.Body = body.Trim(); foreach (string address in mailToArray) { smail.To.Add(new MailAddress(address.Trim())); } s.Send(smail); resilt = true; } catch (System.FormatException ex) { throw ex; } catch (System.Net.Mail.SmtpException ex) { throw ex; } finally { smail.Dispose(); } return(resilt); }
/// <summary> /// 取得 MailMessage 物件 /// </summary> /// <returns>MailMessage</returns> private static MailMessage CreationMailMessage() { SmtpClient s = ConfigSmtp.CreationSmtpClient(); NetworkCredential nc = (NetworkCredential)s.Credentials; //string fromMail = nc.UserName; //system mail address string fromDisplayName = ConfigSmtp.ServiceMailDiaplayName; //mail address display name MailMessage smail = new MailMessage(); //smail.From = new MailAddress(fromMail, fromDisplayName); smail.IsBodyHtml = true; //smail.Attachments.Add(new Attachment("")); smail.BodyEncoding = System.Text.Encoding.UTF8; //smail.CC.Add(new MailAddress("")); //smail.DeliveryNotificationOptions = DeliveryNotificationOptions.Delay; //smail.From = new MailAddress(fromMail, fromDisplayName); smail.SubjectEncoding = System.Text.Encoding.UTF8; return(smail); }
/// <summary> /// SendMail 發送郵件 /// </summary> /// <param name="mailToArray">對方郵件地址</param> /// <param name="mailToDisplayNameArray">對方地址別名</param> /// <param name="subject">主旨</param> /// <param name="body">內容</param> /// <returns></returns> public static bool SendMail(string[] mailToArray, string[] mailToDisplayNameArray, string subject, string body) { SmtpClient s = ConfigSmtp.CreationSmtpClient(); NetworkCredential nc = (NetworkCredential)s.Credentials; MailMessage smail = ConfigSmtp.CreationMailMessage(); bool resilt = false; try { if (CheckMailTo(mailToArray)) { throw MailToAddressException(); } smail.Subject = subject.Trim(); smail.Body = body.Trim(); for (int i = 0; i < mailToArray.Length; i++) { if (mailToDisplayNameArray.Length == mailToArray.Length) { smail.To.Add(new MailAddress(mailToArray[i].Trim(), mailToDisplayNameArray[i].Trim())); } else { smail.To.Add(new MailAddress(mailToArray[i].Trim())); } } s.Send(smail); resilt = true; } catch (System.FormatException ex) { throw ex; } catch (System.Net.Mail.SmtpException ex) { throw ex; } finally { smail.Dispose(); } return(resilt); }