/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset) { if (IsModified) { StringBuilder retVal = new StringBuilder(); retVal.Append("Received: "); retVal.Append("FROM " + m_From); if (m_pFrom_TcpInfo != null) { retVal.Append(" (" + m_pFrom_TcpInfo + ")"); } retVal.Append(" BY " + m_By); if (m_pBy_TcpInfo != null) { retVal.Append(" (" + m_pBy_TcpInfo + ")"); } if (!string.IsNullOrEmpty(m_Via)) { retVal.Append(" VIA " + m_Via); } if (!string.IsNullOrEmpty(m_With)) { retVal.Append(" WITH " + m_With); } if (!string.IsNullOrEmpty(m_ID)) { retVal.Append(" ID " + m_ID); } if (!string.IsNullOrEmpty(m_For)) { retVal.Append(" FOR " + m_For); } retVal.Append("; " + MIME_Utils.DateTimeToRfc2822(m_Time)); retVal.Append("\r\n"); return(retVal.ToString()); } else { return(m_ParseValue); } }
/// <summary> /// Construct secified mime entity ENVELOPE string. /// </summary> /// <param name="entity">Mail message.</param> /// <returns></returns> public static string ConstructEnvelope(Mail_Message entity) { /* RFC 3501 7.4.2 * ENVELOPE * A parenthesized list that describes the envelope structure of a * message. This is computed by the server by parsing the * [RFC-2822] header into the component parts, defaulting various * fields as necessary. * * The fields of the envelope structure are in the following * order: date, subject, from, sender, reply-to, to, cc, bcc, * in-reply-to, and message-id. The date, subject, in-reply-to, * and message-id fields are strings. The from, sender, reply-to, * to, cc, and bcc fields are parenthesized lists of address * structures. * * An address structure is a parenthesized list that describes an * electronic mail address. The fields of an address structure * are in the following order: personal name, [SMTP] * at-domain-list (source route), mailbox name, and host name. * * [RFC-2822] group syntax is indicated by a special form of * address structure in which the host name field is NIL. If the * mailbox name field is also NIL, this is an end of group marker * (semi-colon in RFC 822 syntax). If the mailbox name field is * non-NIL, this is a start of group marker, and the mailbox name * field holds the group name phrase. * * If the Date, Subject, In-Reply-To, and Message-ID header lines * are absent in the [RFC-2822] header, the corresponding member * of the envelope is NIL; if these header lines are present but * empty the corresponding member of the envelope is the empty * string. * * Note: some servers may return a NIL envelope member in the * "present but empty" case. Clients SHOULD treat NIL and * empty string as identical. * * Note: [RFC-2822] requires that all messages have a valid * Date header. Therefore, the date member in the envelope can * not be NIL or the empty string. * * Note: [RFC-2822] requires that the In-Reply-To and * Message-ID headers, if present, have non-empty content. * Therefore, the in-reply-to and message-id members in the * envelope can not be the empty string. * * If the From, To, cc, and bcc header lines are absent in the * [RFC-2822] header, or are present but empty, the corresponding * member of the envelope is NIL. * * If the Sender or Reply-To lines are absent in the [RFC-2822] * header, or are present but empty, the server sets the * corresponding member of the envelope to be the same value as * the from member (the client is not expected to know to do * this). * * Note: [RFC-2822] requires that all messages have a valid * From header. Therefore, the from, sender, and reply-to * members in the envelope can not be NIL. * * ENVELOPE ("date" "subject" from sender reply-to to cc bcc "in-reply-to" "messageID") */ // NOTE: all header fields and parameters must in ENCODED form !!! MIME_Encoding_EncodedWord wordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B, Encoding.UTF8); wordEncoder.Split = false; StringBuilder retVal = new StringBuilder(); retVal.Append("ENVELOPE ("); // date try{ if (entity.Date != DateTime.MinValue) { retVal.Append(TextUtils.QuoteString(MIME_Utils.DateTimeToRfc2822(entity.Date))); } else { retVal.Append("NIL"); } } catch { retVal.Append("NIL"); } // subject if (entity.Subject != null) { //retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(entity.Subject))); string val = wordEncoder.Encode(entity.Subject); retVal.Append(" {" + val.Length + "}\r\n" + val); } else { retVal.Append(" NIL"); } // from if (entity.From != null && entity.From.Count > 0) { retVal.Append(" " + ConstructAddresses(entity.From.ToArray(), wordEncoder)); } else { retVal.Append(" NIL"); } // sender // NOTE: There is confusing part, according rfc 2822 Sender: is MailboxAddress and not AddressList. if (entity.Sender != null) { retVal.Append(" ("); retVal.Append(ConstructAddress(entity.Sender, wordEncoder)); retVal.Append(")"); } else { retVal.Append(" NIL"); } // reply-to if (entity.ReplyTo != null) { retVal.Append(" " + ConstructAddresses(entity.ReplyTo.Mailboxes, wordEncoder)); } else { retVal.Append(" NIL"); } // to if (entity.To != null && entity.To.Count > 0) { retVal.Append(" " + ConstructAddresses(entity.To.Mailboxes, wordEncoder)); } else { retVal.Append(" NIL"); } // cc if (entity.Cc != null && entity.Cc.Count > 0) { retVal.Append(" " + ConstructAddresses(entity.Cc.Mailboxes, wordEncoder)); } else { retVal.Append(" NIL"); } // bcc if (entity.Bcc != null && entity.Bcc.Count > 0) { retVal.Append(" " + ConstructAddresses(entity.Bcc.Mailboxes, wordEncoder)); } else { retVal.Append(" NIL"); } // in-reply-to if (entity.InReplyTo != null) { retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(entity.InReplyTo))); } else { retVal.Append(" NIL"); } // message-id if (entity.MessageID != null) { retVal.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(entity.MessageID))); } else { retVal.Append(" NIL"); } retVal.Append(")"); return(retVal.ToString()); }
/// <summary> /// Returns header field as string. /// </summary> /// <param name="wordEncoder">8-bit words ecnoder. Value null means that words are not encoded.</param> /// <param name="parmetersCharset">Charset to use to encode 8-bit characters. Value null means parameters not encoded.</param> /// <param name="reEncode">If true always specified encoding is used. If false and header field value not modified, original encoding is kept.</param> /// <returns>Returns header field as string.</returns> public override string ToString(MIME_Encoding_EncodedWord wordEncoder, Encoding parmetersCharset, bool reEncode) { if (reEncode || this.IsModified) { StringBuilder retVal = new StringBuilder(); retVal.Append("Received: "); retVal.Append("from " + m_From); if (m_pFrom_TcpInfo != null) { retVal.Append(" (" + m_pFrom_TcpInfo.ToString() + ")"); } retVal.Append(" by " + m_By); if (m_pBy_TcpInfo != null) { retVal.Append(" (" + m_pBy_TcpInfo.ToString() + ")"); } if (!string.IsNullOrEmpty(m_Via)) { retVal.Append(" via " + m_Via); } if (!string.IsNullOrEmpty(m_With)) { retVal.Append(" with " + m_With); } if (!string.IsNullOrEmpty(m_ID)) { retVal.Append(" id " + m_ID); } if (!string.IsNullOrEmpty(m_For)) { retVal.Append(" for " + m_For); } retVal.Append("; " + MIME_Utils.DateTimeToRfc2822(m_Time)); retVal.Append("\r\n"); /* Some servers doesnt like uppercase. * retVal.Append("Received: "); * retVal.Append("FROM " + m_From); * if(m_pFrom_TcpInfo != null){ * retVal.Append(" (" + m_pFrom_TcpInfo.ToString() + ")"); * } * retVal.Append(" BY " + m_By); * if(m_pBy_TcpInfo != null){ * retVal.Append(" (" + m_pBy_TcpInfo.ToString() + ")"); * } * if(!string.IsNullOrEmpty(m_Via)){ * retVal.Append(" VIA " + m_Via); * } * if(!string.IsNullOrEmpty(m_With)){ * retVal.Append(" WITH " + m_With); * } * if(!string.IsNullOrEmpty(m_ID)){ * retVal.Append(" ID " + m_ID); * } * if(!string.IsNullOrEmpty(m_For)){ * retVal.Append(" FOR " + m_For); * } * retVal.Append("; " + MIME_Utils.DateTimeToRfc2822(m_Time)); * retVal.Append("\r\n");*/ return(retVal.ToString()); } else { return(m_ParseValue); } }
public static void ConstructEnvelope(StructureBuilder builder, Mail_Message entity) { // date, subject, from, sender, reply-to, to, cc, bcc, in-reply-to, and message-id var wordEncoder = new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.B, Encoding.UTF8); wordEncoder.Split = false; builder.Append("ENVELOPE").SpaceNBracket(); // date try { if (entity.Date != DateTime.MinValue) { builder.Append(TextUtils.QuoteString(MIME_Utils.DateTimeToRfc2822(entity.Date))); } else { builder.Append("NIL"); } } catch { builder.Append("NIL"); } // subject if (entity.Subject != null) { builder.Append(" " + TextUtils.QuoteString(wordEncoder.Encode(entity.Subject))); //string val = wordEncoder.Encode(entity.Subject); //builder.Append(" {").Append(val.Length).Append("}\r\n").Append(val); } else { builder.AppendNil(); } // from if (entity.From != null && entity.From.Count > 0) { builder.Append(" "); ConstructAddresses(builder, entity.From.ToArray(), wordEncoder); } else { builder.AppendNil(); } // sender // NOTE: There is confusing part, according rfc 2822 Sender: is MailboxAddress and not AddressList. if (entity.Sender != null) { builder.SpaceNBracket(); ConstructAddress(builder, entity.Sender, wordEncoder); builder.EndBracket(); } else { builder.AppendNil(); } // reply-to if (entity.ReplyTo != null) { builder.Append(" "); ConstructAddresses(builder, entity.ReplyTo.Mailboxes, wordEncoder); } else { builder.Append(" NIL"); } // to if (entity.To != null && entity.To.Count > 0) { builder.Append(" "); ConstructAddresses(builder, entity.To.Mailboxes, wordEncoder); } else { builder.Append(" NIL"); } // cc if (entity.Cc != null && entity.Cc.Count > 0) { builder.Append(" "); ConstructAddresses(builder, entity.Cc.Mailboxes, wordEncoder); } else { builder.AppendNil(); } // bcc if (entity.Bcc != null && entity.Bcc.Count > 0) { builder.Append(" "); ConstructAddresses(builder, entity.Bcc.Mailboxes, wordEncoder); } else { builder.AppendNil(); } // in-reply-to if (entity.InReplyTo != null) { builder.Append(" ").Append(TextUtils.QuoteString(wordEncoder.Encode(entity.InReplyTo))); } else { builder.AppendNil(); } // message-id if (entity.MessageID != null) { builder.Append(" ").Append(TextUtils.QuoteString(wordEncoder.Encode(entity.MessageID))); } else { builder.AppendNil(); } builder.EndBracket(); }
/// <summary> /// Creates delivery status notifications(DSN) message. /// </summary> /// <param name="to">DSN message To.</param> /// <param name="subject">DSN message subject.</param> /// <param name="rtfText">DSN message RTF body text.</param> /// <param name="envelopeID">Envelope ID(MAIL FROM: ENVID).</param> /// <param name="arrivalDate">Message arrival date.</param> /// <param name="receivedFromMTA">The remote host EHLo name from where messages was received.</param> /// <param name="reportingMTA">Reporting MTA name.</param> /// <param name="originalRecipient">Original recipient(RCPT TO: ORCTP).</param> /// <param name="finalRecipient">Final recipient.</param> /// <param name="action">DSN action.</param> /// <param name="statusCode_text">Remote SMTP status code with text.</param> /// <param name="remoteMTA">Remote MTA what returned <b>statusCode_text</b>.</param> /// <param name="lastAttempt">Last delivery attempt.</param> /// <param name="retryUntil">Date time how long server will attempt to deliver message.</param> /// <param name="ret">Specifies what original message part are renturned.</param> /// <param name="message">Original message.</param> /// <returns>Returns created DSN message.</returns> public static Mail_Message CreateDsnMessage(string to, string subject, string rtfText, string envelopeID, DateTime arrivalDate, string receivedFromMTA, string reportingMTA, string originalRecipient, string finalRecipient, string action, string statusCode_text, string remoteMTA, DateTime lastAttempt, DateTime retryUntil, SMTP_DSN_Ret ret, Mail_Message message) { // For more info, see RFC 3464. // Ensure that all line-feeds are CRLF. rtfText = rtfText.Replace("\r\n", "\n").Replace("\n", "\r\n"); Mail_Message msg = new Mail_Message(); msg.MimeVersion = "1.0"; msg.Date = DateTime.Now; msg.From = new Mail_t_MailboxList(); msg.From.Add(new Mail_t_Mailbox("Mail Delivery Subsystem", "postmaster@local")); msg.To = new Mail_t_AddressList(); msg.To.Add(new Mail_t_Mailbox(null, to)); msg.Subject = subject; //--- multipart/report ------------------------------------------------------------------------------------------------- MIME_h_ContentType contentType_multipartReport = new MIME_h_ContentType(MIME_MediaTypes.Multipart.report); contentType_multipartReport.Parameters["report-type"] = "delivery-status"; contentType_multipartReport.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartReport multipartReport = new MIME_b_MultipartReport(contentType_multipartReport); msg.Body = multipartReport; //--- multipart/alternative ----------------------------------------------------------------------------------------- MIME_Entity entity_multipart_alternative = new MIME_Entity(); MIME_h_ContentType contentType_multipartAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative); contentType_multipartAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartAlternative multipartAlternative = new MIME_b_MultipartAlternative(contentType_multipartAlternative); entity_multipart_alternative.Body = multipartAlternative; multipartReport.BodyParts.Add(entity_multipart_alternative); //--- text/plain --------------------------------------------------------------------------------------------------- MIME_Entity entity_text_plain = new MIME_Entity(); MIME_b_Text text_plain = new MIME_b_Text(MIME_MediaTypes.Text.plain); entity_text_plain.Body = text_plain; text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, SCore.RtfToText(rtfText)); multipartAlternative.BodyParts.Add(entity_text_plain); //--- text/html ----------------------------------------------------------------------------------------------------- MIME_Entity entity_text_html = new MIME_Entity(); MIME_b_Text text_html = new MIME_b_Text(MIME_MediaTypes.Text.html); entity_text_html.Body = text_html; text_html.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, SCore.RtfToHtml(rtfText)); multipartAlternative.BodyParts.Add(entity_text_html); //--- message/delivery-status MIME_Entity entity_message_deliveryStatus = new MIME_Entity(); MIME_b_Message body_message_deliveryStatus = new MIME_b_Message(MIME_MediaTypes.Message.delivery_status); entity_message_deliveryStatus.Body = body_message_deliveryStatus; StringBuilder dsnText = new StringBuilder(); if (!string.IsNullOrEmpty(envelopeID)) { dsnText.Append("Original-Envelope-Id: " + envelopeID + "\r\n"); } dsnText.Append("Arrival-Date: " + MIME_Utils.DateTimeToRfc2822(arrivalDate) + "\r\n"); if (!string.IsNullOrEmpty(receivedFromMTA)) { dsnText.Append("Received-From-MTA: dns; " + receivedFromMTA + "\r\n"); } dsnText.Append("Reporting-MTA: dns; " + reportingMTA + "\r\n"); dsnText.Append("\r\n"); if (!string.IsNullOrEmpty(originalRecipient)) { dsnText.Append("Original-Recipient: " + originalRecipient + "\r\n"); } dsnText.Append("Final-Recipient: rfc822;" + finalRecipient + "" + "\r\n"); dsnText.Append("Action: " + action + "\r\n"); dsnText.Append("Status: " + statusCode_text.Substring(0, 1) + ".0.0" + "\r\n"); if (!string.IsNullOrEmpty(statusCode_text)) { dsnText.Append("Diagnostic-Code: smtp; " + statusCode_text + "\r\n"); } if (!string.IsNullOrEmpty(remoteMTA)) { dsnText.Append("Remote-MTA: dns; " + remoteMTA + "\r\n"); } if (lastAttempt != DateTime.MinValue) { dsnText.Append("Last-Attempt-Date: " + MIME_Utils.DateTimeToRfc2822(lastAttempt) + "\r\n"); } if (retryUntil != DateTime.MinValue) { dsnText.Append("Will-Retry-Until: " + MIME_Utils.DateTimeToRfc2822(retryUntil) + "\r\n"); } dsnText.Append("\r\n"); body_message_deliveryStatus.SetData(new MemoryStream(Encoding.UTF8.GetBytes(dsnText.ToString())), MIME_TransferEncodings.EightBit); multipartReport.BodyParts.Add(entity_message_deliveryStatus); //--- message/rfc822 if (message != null) { MIME_Entity entity_message_rfc822 = new MIME_Entity(); MIME_b_MessageRfc822 body_message_rfc822 = new MIME_b_MessageRfc822(); entity_message_rfc822.Body = body_message_rfc822; if (ret == SMTP_DSN_Ret.FullMessage) { body_message_rfc822.Message = message; } else { MemoryStream ms = new MemoryStream(); message.Header.ToStream(ms, null, null); ms.Position = 0; body_message_rfc822.Message = Mail_Message.ParseFromStream(ms); } multipartReport.BodyParts.Add(entity_message_rfc822); } return(msg); }
public static Mail_Message CreateDsnMessage(string to, string subject, string rtfText, string envelopeID, DateTime arrivalDate, string receivedFromMTA, string reportingMTA, string originalRecipient, string finalRecipient, string action, string statusCode_text, string remoteMTA, DateTime lastAttempt, DateTime retryUntil, SMTP_DSN_Ret ret, Mail_Message message) { rtfText = rtfText.Replace("\r\n", "\n").Replace("\n", "\r\n"); Mail_Message mail_Message = new Mail_Message(); mail_Message.MimeVersion = "1.0"; mail_Message.Date = DateTime.Now; mail_Message.From = new Mail_t_MailboxList(); mail_Message.From.Add(new Mail_t_Mailbox("Mail Delivery Subsystem", "postmaster@local")); mail_Message.To = new Mail_t_AddressList(); mail_Message.To.Add(new Mail_t_Mailbox(null, to)); mail_Message.Subject = subject; MIME_h_ContentType mIME_h_ContentType = new MIME_h_ContentType(MIME_MediaTypes.Multipart.report); mIME_h_ContentType.Parameters["report-type"] = "delivery-status"; mIME_h_ContentType.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.'); MIME_b_MultipartReport mIME_b_MultipartReport = new MIME_b_MultipartReport(mIME_h_ContentType); mail_Message.Body = mIME_b_MultipartReport; MIME_Entity mIME_Entity = new MIME_Entity(); MIME_b_MultipartAlternative mIME_b_MultipartAlternative = new MIME_b_MultipartAlternative(new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative) { Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.') }); mIME_Entity.Body = mIME_b_MultipartAlternative; mIME_b_MultipartReport.BodyParts.Add(mIME_Entity); MIME_Entity mIME_Entity2 = new MIME_Entity(); MIME_b_Text mIME_b_Text = new MIME_b_Text(MIME_MediaTypes.Text.plain); mIME_Entity2.Body = mIME_b_Text; mIME_b_Text.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, SCore.RtfToText(rtfText)); mIME_b_MultipartAlternative.BodyParts.Add(mIME_Entity2); MIME_Entity mIME_Entity3 = new MIME_Entity(); MIME_b_Text mIME_b_Text2 = new MIME_b_Text(MIME_MediaTypes.Text.html); mIME_Entity3.Body = mIME_b_Text2; mIME_b_Text2.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, SCore.RtfToHtml(rtfText)); mIME_b_MultipartAlternative.BodyParts.Add(mIME_Entity3); MIME_Entity mIME_Entity4 = new MIME_Entity(); MIME_b_Message mIME_b_Message = new MIME_b_Message(MIME_MediaTypes.Message.delivery_status); mIME_Entity4.Body = mIME_b_Message; StringBuilder stringBuilder = new StringBuilder(); if (!string.IsNullOrEmpty(envelopeID)) { stringBuilder.Append("Original-Envelope-Id: " + envelopeID + "\r\n"); } stringBuilder.Append("Arrival-Date: " + MIME_Utils.DateTimeToRfc2822(arrivalDate) + "\r\n"); if (!string.IsNullOrEmpty(receivedFromMTA)) { stringBuilder.Append("Received-From-MTA: dns; " + receivedFromMTA + "\r\n"); } stringBuilder.Append("Reporting-MTA: dns; " + reportingMTA + "\r\n"); stringBuilder.Append("\r\n"); if (!string.IsNullOrEmpty(originalRecipient)) { stringBuilder.Append("Original-Recipient: " + originalRecipient + "\r\n"); } stringBuilder.Append("Final-Recipient: rfc822;" + finalRecipient + "\r\n"); stringBuilder.Append("Action: " + action + "\r\n"); stringBuilder.Append("Status: " + statusCode_text.Substring(0, 1) + ".0.0\r\n"); if (!string.IsNullOrEmpty(statusCode_text)) { stringBuilder.Append("Diagnostic-Code: smtp; " + statusCode_text + "\r\n"); } if (!string.IsNullOrEmpty(remoteMTA)) { stringBuilder.Append("Remote-MTA: dns; " + remoteMTA + "\r\n"); } if (lastAttempt != DateTime.MinValue) { stringBuilder.Append("Last-Attempt-Date: " + MIME_Utils.DateTimeToRfc2822(lastAttempt) + "\r\n"); } if (retryUntil != DateTime.MinValue) { stringBuilder.Append("Will-Retry-Until: " + MIME_Utils.DateTimeToRfc2822(retryUntil) + "\r\n"); } stringBuilder.Append("\r\n"); mIME_b_Message.SetData(new MemoryStream(Encoding.UTF8.GetBytes(stringBuilder.ToString())), MIME_TransferEncodings.EightBit); mIME_b_MultipartReport.BodyParts.Add(mIME_Entity4); if (message != null) { MIME_Entity mIME_Entity5 = new MIME_Entity(); MIME_b_MessageRfc822 mIME_b_MessageRfc = new MIME_b_MessageRfc822(); mIME_Entity5.Body = mIME_b_MessageRfc; if (ret == SMTP_DSN_Ret.FullMessage) { mIME_b_MessageRfc.Message = message; } else { MemoryStream memoryStream = new MemoryStream(); message.Header.ToStream(memoryStream, null, null); memoryStream.Position = 0L; mIME_b_MessageRfc.Message = Mail_Message.ParseFromStream(memoryStream); } mIME_b_MultipartReport.BodyParts.Add(mIME_Entity5); } return(mail_Message); }