/// <summary> /// Deletes an email account /// </summary> /// <param name="emailAccount">Email account</param> public virtual void DeleteEmailAccount(EmailAccount emailAccount) { if (emailAccount == null) throw new ArgumentNullException("emailAccount"); if (GetAllEmailAccounts().Count == 1) throw new CmsException("You cannot delete this email account. At least one account is required."); _emailAccountRepository.Delete(emailAccount); //event notification _eventPublisher.EntityDeleted(emailAccount); }
/// <summary> /// Sends an email /// </summary> /// <param name="emailAccount">Email account to use</param> /// <param name="subject">Subject</param> /// <param name="body">Body</param> /// <param name="fromAddress">From address</param> /// <param name="fromName">From display name</param> /// <param name="toAddress">To address</param> /// <param name="toName">To display name</param> /// <param name="replyTo">ReplyTo address</param> /// <param name="replyToName">ReplyTo display name</param> /// <param name="bcc">BCC addresses list</param> /// <param name="cc">CC addresses list</param> /// <param name="attachmentFilePath">Attachment file path</param> /// <param name="attachmentFileName">Attachment file name. If specified, then this file name will be sent to a recipient. Otherwise, "AttachmentFilePath" name will be used.</param> /// <param name="attachedDownloadId">Attachment download ID (another attachedment)</param> public virtual void SendEmail(EmailAccount emailAccount, string subject, string body, string fromAddress, string fromName, string toAddress, string toName, string replyTo = null, string replyToName = null, IEnumerable<string> bcc = null, IEnumerable<string> cc = null, string attachmentFilePath = null, string attachmentFileName = null, int attachedDownloadId = 0) { var message = new MailMessage(); //from, to, reply to message.From = new MailAddress(fromAddress, fromName); message.To.Add(new MailAddress(toAddress, toName)); if (!String.IsNullOrEmpty(replyTo)) { message.ReplyToList.Add(new MailAddress(replyTo, replyToName)); } //BCC if (bcc != null) { foreach (var address in bcc.Where(bccValue => !String.IsNullOrWhiteSpace(bccValue))) { message.Bcc.Add(address.Trim()); } } //CC if (cc != null) { foreach (var address in cc.Where(ccValue => !String.IsNullOrWhiteSpace(ccValue))) { message.CC.Add(address.Trim()); } } //content message.Subject = subject; message.Body = body; message.IsBodyHtml = true; //create the file attachment for this e-mail message if (!String.IsNullOrEmpty(attachmentFilePath) && File.Exists(attachmentFilePath)) { var attachment = new Attachment(attachmentFilePath); attachment.ContentDisposition.CreationDate = File.GetCreationTime(attachmentFilePath); attachment.ContentDisposition.ModificationDate = File.GetLastWriteTime(attachmentFilePath); attachment.ContentDisposition.ReadDate = File.GetLastAccessTime(attachmentFilePath); if (!String.IsNullOrEmpty(attachmentFileName)) { attachment.Name = attachmentFileName; } message.Attachments.Add(attachment); } //another attachment? if (attachedDownloadId > 0) { var download = _downloadService.GetDownloadById(attachedDownloadId); if (download != null) { //we do not support URLs as attachments if (!download.UseDownloadUrl) { string fileName = !String.IsNullOrWhiteSpace(download.Filename) ? download.Filename : download.Id.ToString(); fileName += download.Extension; var ms = new MemoryStream(download.DownloadBinary); var attachment = new Attachment(ms, fileName); //string contentType = !String.IsNullOrWhiteSpace(download.ContentType) ? download.ContentType : "application/octet-stream"; //var attachment = new Attachment(ms, fileName, contentType); attachment.ContentDisposition.CreationDate = DateTime.UtcNow; attachment.ContentDisposition.ModificationDate = DateTime.UtcNow; attachment.ContentDisposition.ReadDate = DateTime.UtcNow; message.Attachments.Add(attachment); } } } //send email using (var smtpClient = new SmtpClient()) { smtpClient.UseDefaultCredentials = emailAccount.UseDefaultCredentials; smtpClient.Host = emailAccount.Host; smtpClient.Port = emailAccount.Port; smtpClient.EnableSsl = emailAccount.EnableSsl; if (emailAccount.UseDefaultCredentials) smtpClient.Credentials = CredentialCache.DefaultNetworkCredentials; else smtpClient.Credentials = new NetworkCredential(emailAccount.Username, emailAccount.Password); smtpClient.Send(message); } }
/// <summary> /// Inserts an email account /// </summary> /// <param name="emailAccount">Email account</param> public virtual void InsertEmailAccount(EmailAccount emailAccount) { if (emailAccount == null) throw new ArgumentNullException("emailAccount"); emailAccount.Email = CommonHelper.EnsureNotNull(emailAccount.Email); emailAccount.DisplayName = CommonHelper.EnsureNotNull(emailAccount.DisplayName); emailAccount.Host = CommonHelper.EnsureNotNull(emailAccount.Host); emailAccount.Username = CommonHelper.EnsureNotNull(emailAccount.Username); emailAccount.Password = CommonHelper.EnsureNotNull(emailAccount.Password); emailAccount.Email = emailAccount.Email.Trim(); emailAccount.DisplayName = emailAccount.DisplayName.Trim(); emailAccount.Host = emailAccount.Host.Trim(); emailAccount.Username = emailAccount.Username.Trim(); emailAccount.Password = emailAccount.Password.Trim(); emailAccount.Email = CommonHelper.EnsureMaximumLength(emailAccount.Email, 255); emailAccount.DisplayName = CommonHelper.EnsureMaximumLength(emailAccount.DisplayName, 255); emailAccount.Host = CommonHelper.EnsureMaximumLength(emailAccount.Host, 255); emailAccount.Username = CommonHelper.EnsureMaximumLength(emailAccount.Username, 255); emailAccount.Password = CommonHelper.EnsureMaximumLength(emailAccount.Password, 255); _emailAccountRepository.Insert(emailAccount); //event notification _eventPublisher.EntityInserted(emailAccount); }