private MailMessage ConstructEmail(IdentityMessage message) { var mailMessage = new MailMessage { Subject = message.Subject }; mailMessage.IsBodyHtml = IsHtml; if (AlternateViews.Any()) { foreach (var view in AlternateViews) { mailMessage.AlternateViews.Add(view); } } else { mailMessage.Body = message.Body; } if (!string.IsNullOrEmpty(From)) { mailMessage.From = new MailAddress(From); } mailMessage.To.Add(new MailAddress(message.Destination)); foreach (var attachedFileInformation in Attachments) { var mimeType = DocumentExtensions.GetMimeTypeForFile(attachedFileInformation.Key); mailMessage.Attachments.Add(new Attachment(attachedFileInformation.Value, mimeType) { Name = attachedFileInformation.Key }); } mailMessage.IsBodyHtml = IsHtml; return(mailMessage); }
public virtual void Save(System.IO.TextWriter txt) { txt.WriteLine("Date: {0}", (Date == DateTime.MinValue ? LocalTime.Now : Date).GetRFC2060Date()); txt.WriteLine("To: {0}", string.Join("; ", To.Select(x => x.ToString()))); txt.WriteLine("Cc: {0}", string.Join("; ", Cc.Select(x => x.ToString()))); txt.WriteLine("Reply-To: {0}", string.Join("; ", ReplyTo.Select(x => x.ToString()))); txt.WriteLine("Bcc: {0}", string.Join("; ", Bcc.Select(x => x.ToString()))); if (Sender != null) { txt.WriteLine("Sender: {0}", Sender); } if (From != null) { txt.WriteLine("From: {0}", From); } if (!string.IsNullOrEmpty(MessageID)) { txt.WriteLine("Message-ID: {0}", MessageID); } var otherHeaders = Headers.Where(x => !SpecialHeaders.Contains(x.Key, StringComparer.InvariantCultureIgnoreCase)); foreach (var header in otherHeaders) { txt.WriteLine("{0}: {1}", header.Key, header.Value); } if (Importance != MailPriority.Normal) { txt.WriteLine("Importance: {0}", (int)Importance); } txt.WriteLine("Subject: {0}", Subject); string boundary = null; if (Attachments.Any() || AlternateViews.Any()) { boundary = $"--boundary_{Guid.NewGuid()}"; txt.WriteLine("Content-Type: multipart/mixed; boundary={0}", boundary); } // signal end of headers txt.WriteLine(); if (boundary != null) { txt.WriteLine("--" + boundary); txt.WriteLine(); } txt.WriteLine(Body); AlternateViews.Union(Attachments).ToList().ForEach(att => { txt.WriteLine("--" + boundary); txt.WriteLine(string.Join("\n", att.Headers.Select(h => $"{h.Key}: {h.Value}"))); txt.WriteLine(); txt.WriteLine(att.Body); }); if (boundary != null) { txt.WriteLine("--" + boundary + "--"); } }
public virtual void Save(TextWriter txt) { txt.WriteLine("Date: {0}", Date.GetRFC2060Date()); txt.WriteLine("To: {0}", string.Join("; ", To.Select(x => x.ToString()))); txt.WriteLine("Cc: {0}", string.Join("; ", Cc.Select(x => x.ToString()))); txt.WriteLine("Reply-To: {0}", string.Join("; ", ReplyTo.Select(x => x.ToString()))); txt.WriteLine("Bcc: {0}", string.Join("; ", Bcc.Select(x => x.ToString()))); if (Sender != null) { txt.WriteLine("Sender: {0}", Sender); } if (From != null) { txt.WriteLine("From: {0}", From); } if (!string.IsNullOrEmpty(MessageID)) { txt.WriteLine("Message-ID: {0}", MessageID); } var otherHeaders = Headers.Where(x => !SpecialHeaders.Contains(x.Key, StringComparer.InvariantCultureIgnoreCase)); foreach (var header in otherHeaders) { txt.WriteLine("{0}: {1}", header.Key, header.Value); } if (Importance != MailPriority.Normal) { txt.WriteLine("Importance: {0}", (int)Importance); } txt.WriteLine("Subject: {0}", Subject); string boundary = null; if (Attachments.Any() || AlternateViews.Any()) { boundary = string.Format("--boundary_{0}", Guid.NewGuid()); txt.WriteLine("Content-Type: multipart/mixed; boundary={0}", boundary); } // signal end of headers txt.WriteLine(); if (!string.IsNullOrWhiteSpace(Body)) { if (boundary != null) { txt.WriteLine("--" + boundary); txt.WriteLine(); } txt.Write(Body); } AlternateViews.ToList().ForEach(view => { txt.WriteLine(); txt.WriteLine("--" + boundary); txt.WriteLine(string.Join("\r\n", view.Headers.Select(h => string.Format("{0}: {1}", h.Key, h.Value)))); txt.WriteLine(); if (view.Scope >= Scope.HeadersAndBodySnyppit) { txt.WriteLine(view.Body); } }); this.Attachments.ToList().ForEach(att => { txt.WriteLine(); txt.WriteLine("--" + boundary); txt.WriteLine(string.Join("\r\n", att.Headers.Select(h => string.Format("{0}: {1}", h.Key, h.Value)))); txt.WriteLine(); if (att.Scope >= Scope.HeadersAndBodySnyppit) { txt.WriteLine(att.Body); } }); if (boundary != null) { txt.WriteLine("--" + boundary + "--"); } }