/// <summary> /// The MIME string. /// </summary> /// <returns></returns> public string ToMimeString() { var content = new StringBuilder(); if (this.ContentType.Type.Equals("multipart")) //|| this.ContentType.Type.Equals("image") //|| this.ContentType.Type.Equals("application")) { string boundary = string.Empty; // If there is no boundary, create one. if (this.ContentType.Parameters["boundary"] == null || this.ContentType.Parameters["boundary"].Length < 1) { string unique = Codec.GetUniqueString(); boundary = string.Format("---AU_MimePart_{0}", unique); this.ContentType.Parameters.Add("boundary", boundary); } else { boundary = this.ContentType.Parameters["boundary"]; } // Add the header. content.Append(this.GetHeaderString()); // Add the subparts. foreach (MimePart subpart in this.SubParts) { content.AppendFormat("\r\n\r\n--{0}\r\n", boundary); content.Append(subpart.ToMimeString()); } //content += this.TextContentTransferEncoded; // Close the packet. content.AppendFormat("\r\n\r\n--{0}--\r\n", boundary); return(content.ToString()); } // Modified by PMENGAL // All textplain is encoded with quoted-printable even if it is set to UTF7 or whatever. /*if (this.ContentTransferEncoding == ActiveUp.Net.Mail.ContentTransferEncoding.SevenBits) * { * content = this.TextContent; * } * else * { * if (this.ContentType.MimeType.ToLower().IndexOf("text/") != -1) content = Codec.ToQuotedPrintable(this.TextContent, (this.Charset != null) ? this.Charset : "us-ascii"); * else if (this.ContentType.MimeType.ToLower().IndexOf("message/") != -1) content = this.TextContent; * else content = Codec.Wrap(System.Convert.ToBase64String(this.BinaryContent), 77); * }*/ content .Append(this.GetHeaderString()) .Append("\r\n") .Append(this.TextContentTransferEncoded); return(content.ToString()); }
private void InitializeBoundaryIfNotProvided() { if (!string.IsNullOrEmpty(ContentType.Parameters["boundary"])) { return; } // TODO: Factorize the boundary name var newBoundary = "---AU_MimePart_" + Codec.GetUniqueString(); ContentType.Parameters.Add("boundary", newBoundary); }
/// <summary> /// The MIME representation of the header. /// </summary> /// <param name="removeBlindCopies">if set to <c>true</c> remove blind copies (BCC).</param> /// <returns></returns> public string ToHeaderString(bool removeBlindCopies) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); /*foreach (TraceInfo trace in this.Trace) sb.AppendLine("Received: " + trace.ToString()); * if (!this.From.Email.Equals(string.Empty)) sb.AppendLine("From: " + this.From.Merged); * if (!this.Sender.Email.Equals(string.Empty)) sb.AppendLine("Sender: " + this.Sender.Merged); * if (this.To.Count > 0) sb.AppendLine("To: " + this.To.Merged); * if (this.Cc.Count > 0) sb.AppendLine("Cc: " + this.Cc.Merged); * if (this.Bcc.Count > 0) sb.AppendLine("Bcc: " + this.Bcc.Merged); * if (!this.ReplyTo.Email.Equals(string.Empty)) sb.AppendLine("Reply-to: " + this.ReplyTo.Merged);*/ foreach (TraceInfo trace in this.Trace) { this.AddHeaderField("Received", trace.ToString()); } if (!this.From.Email.Equals(string.Empty)) { this.AddHeaderField("From", this.From.Merged); } if (!this.Sender.Email.Equals(string.Empty)) { this.AddHeaderField("Sender", this.Sender.Merged); } if (this.To.Count > 0) { this.AddHeaderField("To", this.To.Merged); } if (this.Cc.Count > 0) { this.AddHeaderField("Cc", this.Cc.Merged); } if (this.Bcc.Count > 0 && !removeBlindCopies) { this.AddHeaderField("Bcc", this.Bcc.Merged); } if (!this.ReplyTo.Email.Equals(string.Empty)) { this.AddHeaderField("Reply-To", this.ReplyTo.Merged); } if (this.Date.Equals(DateTime.MinValue)) { this.Date = DateTime.Now; } if (this.MessageId == null || this.MessageId == string.Empty) { this.MessageId = "<AU" + Codec.GetUniqueString() + "@" + System.Net.Dns.GetHostName() + ">"; } if (this.ContentType.MimeType.Length > 0) { string contentType = this.ContentType.ToString(); contentType = contentType.Substring(contentType.IndexOf(":") + 1).TrimStart(' '); this.AddHeaderField("Content-Type", contentType); } if (this.ContentDisposition.Disposition.Length > 0) { string contentDisposition = this.ContentDisposition.ToString(); contentDisposition = contentDisposition.Substring(contentDisposition.IndexOf(":") + 1).TrimStart(' '); this.AddHeaderField("Content-Disposition", contentDisposition); } if (this.ContentType.Type.Equals("text")) { string contentTransferEncoding = this.ContentTransferEncoding.ToString(); contentTransferEncoding = contentTransferEncoding.Substring(contentTransferEncoding.IndexOf(":") + 1).TrimStart(' '); this.AddHeaderField("Content-Transfer-Encoding", "quoted-printable"); } //sb.Append(this.ContentType.ToString() + "\r\n"); //if (this.ContentDisposition.Disposition == "attachment") sb.AppendLine(this.ContentDisposition.ToString()); System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); System.Version v = asm.GetName().Version; this.AddHeaderField("X-Mailer", "ActiveUp.MailSystem " + v.Major + "." + v.Minor + "." + v.Build + " www.activeup.com"); foreach (string key in this.HeaderFields.AllKeys) { for (int i = 0; i < this.HeaderFields.GetValues(key).Length; i++) { sb.Append(this.HeaderFieldNames.GetValues(key)[i] + ": " + this.HeaderFields.GetValues(key)[i] + "\r\n"); } } /*string header = sb.ToString().TrimEnd('\r', '\n'); * string foldedHeader = string.Empty; * * StringReader sr = new StringReader(header); * while(sr.Peek() != -1) * { * foldedHeader += Parser.Fold(sr.ReadLine()) + "\r\n"; * }*/ return(sb.ToString().TrimEnd('\r', '\n')); }
/// <summary> /// Generates a new Content-ID for the part. /// </summary> public void SetContentId() { ContentId = "AMLv2" + Codec.GetUniqueString() + "@" + System.Net.Dns.GetHostName(); }
/// <summary> /// The MIME representation of the header. /// </summary> /// <param name="removeBlindCopies">if set to <c>true</c> remove blind copies (BCC).</param> /// <param name="forceBase64Encoding">if set to <c>true</c> forces inner elements to be base64 encoded</param> /// <returns></returns> public string ToHeaderString(bool removeBlindCopies, bool forceBase64Encoding = false) { System.Text.StringBuilder sb = new System.Text.StringBuilder(); /*foreach (TraceInfo trace in this.Trace) sb.AppendLine("Received: " + trace.ToString()); * if (!this.From.Email.Equals(string.Empty)) sb.AppendLine("From: " + this.From.Merged); * if (!this.Sender.Email.Equals(string.Empty)) sb.AppendLine("Sender: " + this.Sender.Merged); * if (this.To.Count > 0) sb.AppendLine("To: " + this.To.Merged); * if (this.Cc.Count > 0) sb.AppendLine("Cc: " + this.Cc.Merged); * if (this.Bcc.Count > 0) sb.AppendLine("Bcc: " + this.Bcc.Merged); * if (!this.ReplyTo.Email.Equals(string.Empty)) sb.AppendLine("Reply-to: " + this.ReplyTo.Merged);*/ foreach (TraceInfo trace in Trace) { AddHeaderField("Received", trace.ToString()); } if (!From.Email.Equals(string.Empty)) { AddHeaderField("From", From.Merged); } if (!Sender.Email.Equals(string.Empty)) { AddHeaderField("Sender", Sender.Merged); } if (To.Count > 0) { AddHeaderField("To", To.Merged); } if (Cc.Count > 0) { AddHeaderField("Cc", Cc.Merged); } if (Bcc.Count > 0 && !removeBlindCopies) { AddHeaderField("Bcc", Bcc.Merged); } if (!ReplyTo.Email.Equals(string.Empty)) { AddHeaderField("Reply-To", ReplyTo.Merged); } if (Date.Equals(DateTime.MinValue)) { Date = DateTime.Now; } if (string.IsNullOrEmpty(MessageId)) { MessageId = "<AU" + Codec.GetUniqueString() + "@" + System.Net.Dns.GetHostName() + ">"; } if (ContentType.MimeType.Length > 0) { string contentType = ContentType.ToString(); contentType = contentType.Substring(contentType.IndexOf(":") + 1).TrimStart(' '); AddHeaderField("Content-Type", contentType); } if (ContentDisposition.Disposition.Length > 0) { string contentDisposition = ContentDisposition.ToString(); contentDisposition = contentDisposition.Substring(contentDisposition.IndexOf(":") + 1).TrimStart(' '); AddHeaderField("Content-Disposition", contentDisposition); } if (forceBase64Encoding) { AddHeaderField("Content-Transfer-Encoding", "base64"); } else if (ContentType.Type.Equals("text")) { AddHeaderField("Content-Transfer-Encoding", "quoted-printable"); } System.Reflection.Assembly asm = System.Reflection.Assembly.GetExecutingAssembly(); Version v = asm.GetName().Version; AddHeaderField("X-Mailer", "ActiveUp.MailSystem " + v.Major + "." + v.Minor + "." + v.Build + " www.activeup.com"); foreach (string key in HeaderFields.AllKeys) { for (int i = 0; i < HeaderFields.GetValues(key).Length; i++) { sb.Append(HeaderFieldNames.GetValues(key)[i] + ": " + HeaderFields.GetValues(key)[i] + "\r\n"); } } return(sb.ToString().TrimEnd('\r', '\n')); }