/// <summary> /// Returns a <see cref="System.String" /> that represents this instance. /// </summary> /// <returns>A <see cref="System.String" /> that represents this instance.</returns> public override string ToString() { return(string.Format("To: {0} Cc: {1} Bcc: {2}", string.Join(";", To.Select(s => s.Address)), string.Join(";", Cc.Select(s => s.Address)), string.Join(";", Bcc.Select(s => s.Address)))); }
public void Save(System.IO.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); txt.WriteLine(); //todo: attachments txt.Write(Body); }
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 override string ToString() { StringBuilder sb = new StringBuilder(); string boundary = Guid.NewGuid().ToString(); var date = DateTime.Now.ToString("ddd, d MMM yyyy H:m:s zz00"); var subject = !Equals(SubjectEncoding, Encoding.ASCII) ? EncodeQuotedPrintableHeader(Subject, SubjectEncoding) : Subject; AddHeader(sb, "From", FormatMailAddress(From, HeaderEncoding)); if (To.Any()) { AddHeader(sb, "To", String.Join(", ", To.Select(t => FormatMailAddress(t, HeaderEncoding)))); } if (Cc.Any()) { AddHeader(sb, "Cc", String.Join(", ", Cc.Select(t => FormatMailAddress(t, HeaderEncoding)))); } AddHeader(sb, "Subject", subject); AddHeader(sb, "MIME-Version", "1.0"); AddHeader(sb, "Date", date); AddHeader(sb, "Message-ID", MessageId); foreach (var additionalHeader in AdditionalHeaders) { AddHeader(additionalHeader.Name, additionalHeader.Value); } if (Attachments.Any()) { AddHeader(sb, "Content-Type", "multipart/mixed; boundary=" + boundary); AddLine(sb, ""); AddLine(sb, "--" + boundary); } string boundary2 = Guid.NewGuid().ToString(); AddHeader(sb, "Content-Type", "multipart/alternative; boundary=" + boundary2); AddLine(sb, ""); AddLine(sb, "--" + boundary2); AddHeader(sb, "Content-Type", "text/plain; charset=" + BodyEncoding.HeaderName); AddHeader(sb, "Content-Transfer-Encoding", "quoted-printable"); AddHeader(sb, "Content-Disposition", "inline"); AddLine(sb, ""); AddLine(sb, encodeQuotedPrintable(Text, BodyEncoding)); AddLine(sb, "--" + boundary2); AddHeader(sb, "Content-Type", "text/html; charset=" + BodyEncoding.HeaderName); AddHeader(sb, "Content-Transfer-Encoding", "quoted-printable"); AddHeader(sb, "Content-Disposition", "inline"); AddLine(sb, ""); AddLine(sb, encodeQuotedPrintable(Html, BodyEncoding)); AddLine(sb, "--" + boundary2 + "--"); if (Attachments.Any()) { AddLine(sb, ""); foreach (var attachment in Attachments) { AddLine(sb, "--" + boundary); if (attachment.Encoding != null) { AddHeader(sb, "Content-Type", attachment.Type + "; charset=" + attachment.Encoding.HeaderName); } else { AddHeader(sb, "Content-Type", attachment.Type); } AddHeader(sb, "Content-Transfer-Encoding", "base64"); AddHeader(sb, "Content-Disposition", "attachment; filename=" + attachment.Name); AddLine(sb, ""); AddLine(sb, Convert.ToBase64String(attachment.Data, Base64FormattingOptions.InsertLineBreaks)); } AddLine(sb, "--" + boundary + "--"); } return(sb.ToString()); }
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 + "--"); } }