/// <summary> /// Converts <see cref="QueuedEmail"/> to <see cref="MailMessage"/>. /// </summary> internal MailMessage ConvertMail(QueuedEmail qe) { // 'internal' for testing purposes var msg = new MailMessage( qe.To, qe.Subject.Replace("\r\n", string.Empty), qe.Body, qe.From); if (qe.ReplyTo.HasValue()) { msg.ReplyTo.Add(new MailAddress(qe.ReplyTo)); } AddMailAddresses(qe.CC, msg.Cc); AddMailAddresses(qe.Bcc, msg.Bcc); if (qe.Attachments != null && qe.Attachments.Count > 0) { foreach (var qea in qe.Attachments) { MailAttachment attachment = null; if (qea.StorageLocation == EmailAttachmentStorageLocation.Blob) { var data = qea.MediaStorage?.Data; if (data != null && data.LongLength > 0) { attachment = new MailAttachment(data.ToStream(), qea.Name, qea.MimeType); } } else if (qea.StorageLocation == EmailAttachmentStorageLocation.Path) { var path = qea.Path; if (path.HasValue()) { // TODO: (mh) (core) Do this right. //if (path[0] == '~' || path[0] == '/') //{ // path = CommonHelper.MapPath(VirtualPathUtility.ToAppRelative(path), false); //} //if (File.Exists(path)) //{ // attachment = new MailAttachment(path, qea.MimeType) { Name = qea.Name }; //} } } else if (qea.StorageLocation == EmailAttachmentStorageLocation.FileReference) { var file = qea.MediaFile; if (file != null) { var mediaFile = _mediaService.ConvertMediaFile(file); var stream = mediaFile.OpenRead(); if (stream != null) { attachment = new MailAttachment(stream, file.Name, file.MimeType); } } } if (attachment != null) { msg.Attachments.Add(attachment); } } } return(msg); }