/// <summary> /// 初始化MailMessage /// </summary> /// <param name="obj"></param> private void InitMailMessage(EmailEntity obj) { try { _mail = new MailMessage() { From = new MailAddress(obj.From), Subject = obj.Subject, Body = obj.Content, IsBodyHtml = obj.IsHTMLFormat, BodyEncoding = obj.BodyEncoding, Priority = obj.Priority, }; if (obj.CcList != null) { foreach (string c in obj.CcList) { _mail.CC.Add(c); } } if (obj.BccList != null) { foreach (var mailBcc in obj.BccList) { _mail.Bcc.Add(mailBcc); } } if (_mail != null) { foreach (var item in obj.ToList) { if (!_mail.To.Contains(new MailAddress(item))) { _mail.To.Add(item); } } } } catch (Exception ex) { //throw new DomainServiceException("Fail to InitMailMessage.", ex); } }
/// <summary> /// 发邮件 /// </summary> /// <param name="obj"></param> /// <returns></returns> public string SendEmail(EmailEntity obj) { try { if (obj != null) { if (ValidateAllFileSize(obj.Attachments)) { obj.From = m_EmailConfig.SystemEmail; obj.Smtp = GetSmtp(); obj.Port = GetPort(); if (!string.IsNullOrEmpty(obj.Smtp) && obj.Port > 0 && !string.IsNullOrEmpty(obj.From) && (obj.ToList.Count > 0)) { InitSmtp(obj); InitMailMessage(obj); AddAttchement(obj); _smtp.SendAsync(_mail, null); _smtp.SendCompleted += (sender, evt) => { AttachmentsDispose(); //DeleteFiles(obj); }; return(string.Empty); } //return Culture.CnEn(obj.CultureName, "发送邮件失败.", "Fail to send the Email."); }//end if (ValidateAllFileSize(obj.Attachments)) //return Culture.CnEn(obj.CultureName, "所有的附件大小不能大于7M", "The size of all attachments can't be bigger than 7m"); }//end if (obj != null) //return Culture.CnEn(obj.CultureName, "发送邮件失败.", "Fail to send the Email."); } catch (DomainServiceException ex) { // return Culture.CnEn(obj.CultureName, "发送邮件失败.", "Fail to send the Email."); ; } return(string.Empty); }
/// <summary> /// 添加配置文件 /// </summary> /// <param name="obj"></param> private void AddAttchement(EmailEntity obj) { if (obj.Attachments != null && obj.Attachments.Count > 0) { foreach (String fileName in obj.Attachments) { if (File.Exists(fileName)) { var att = new Attachment(fileName) { NameEncoding = obj.BodyEncoding, Name = fileName.Substring(fileName.LastIndexOf('_') + 1) }; _mail.Attachments.Add(att); } } } }
/// <summary> /// 初始化Smtp /// </summary> /// <param name="obj"></param> private void InitSmtp(EmailEntity obj) { try { string domainUser = m_EmailConfig.UserName; string domainPws = DataEncrypt.DecryptFromDB(m_EmailConfig.Password); _smtp = new SmtpClient { Host = obj.Smtp, Port = obj.Port, EnableSsl = obj.Ssl, Credentials = new System.Net.NetworkCredential(domainUser, domainPws) }; } catch (Exception ex) { //throw new DomainServiceException("InitSmtp Failed.", ex); } }
/// <summary> /// 删除邮件附件 /// </summary> /// <param name="obj"></param> /// <returns></returns> public bool DeleteFiles(EmailEntity obj) { try { if (obj.Attachments != null && obj.Attachments.Count > 0) { foreach (String FilePath in obj.Attachments) { if (File.Exists(Path + FilePath)) { File.Delete(Path + FilePath); } } return(true); } return(false); } catch (Exception ex) { //throw new DomainServiceException("Fail to delete files.", ex); } return(false); }