/// <summary> /// Converts ContentTransferEncoding_enum to string. NOTE: Returns null for ContentTransferEncoding_enum.NotSpecified. /// </summary> /// <param name="encoding">ContentTransferEncoding_enum value to convert.</param> /// <returns></returns> public static string ContentTransferEncodingToString(ContentTransferEncoding_enum encoding) { if (encoding == ContentTransferEncoding_enum._7bit) { return("7bit"); } else if (encoding == ContentTransferEncoding_enum.QuotedPrintable) { return("quoted-printable"); } else if (encoding == ContentTransferEncoding_enum.Base64) { return("base64"); } else if (encoding == ContentTransferEncoding_enum._8bit) { return("8bit"); } else if (encoding == ContentTransferEncoding_enum.Binary) { return("binary"); } else if (encoding == ContentTransferEncoding_enum.Unknown) { return("unknown"); } else { return(null); } }
/// <summary> /// Converts ContentTransferEncoding_enum to string. NOTE: Returns null for ContentTransferEncoding_enum.NotSpecified. /// </summary> /// <param name="encoding">ContentTransferEncoding_enum value to convert.</param> /// <returns></returns> public static string ContentTransferEncodingToString(ContentTransferEncoding_enum encoding) { if(encoding == ContentTransferEncoding_enum._7bit){ return "7bit"; } else if(encoding == ContentTransferEncoding_enum.QuotedPrintable){ return "quoted-printable"; } else if(encoding == ContentTransferEncoding_enum.Base64){ return "base64"; } else if(encoding == ContentTransferEncoding_enum._8bit){ return "8bit"; } else if(encoding == ContentTransferEncoding_enum.Binary){ return "binary"; } else if(encoding == ContentTransferEncoding_enum.Unknown){ return "unknown"; } else{ return null; } }
/// <summary> /// Encodes data with specified content transfer encoding. /// </summary> /// <param name="data">Data to encode.</param> /// <param name="encoding">Encoding with what to encode data.</param> private byte[] EncodeData(byte[] data, ContentTransferEncoding_enum encoding) { // Allow only known Content-Transfer-Encoding (ContentTransferEncoding_enum value), // otherwise we don't know how to encode data. if (encoding == ContentTransferEncoding_enum.NotSpecified) { throw new Exception("Please specify Content-Transfer-Encoding first !"); } if (encoding == ContentTransferEncoding_enum.Unknown) { throw new Exception( "Not supported Content-Transfer-Encoding. If it's your custom encoding, encode data yourself and set it with DataEncoded property !"); } if (encoding == ContentTransferEncoding_enum.Base64) { return Core.Base64Encode(data); } else if (encoding == ContentTransferEncoding_enum.QuotedPrintable) { return Core.QuotedPrintableEncode(data); } else { return data; } }
/// <summary> /// Parses entity and it's child entities. /// </summary> internal void Parse(string text) { StringReader r = new StringReader(text); r.ReadToFirstChar(); // If starts with ( then multipart/xxx, otherwise normal single part entity if (r.StartsWith("(")) { // Entities are (entity1)(entity2)(...) <SP> ContentTypeSubType while (r.StartsWith("(")) { IMAP_BODY_Entity entity = new IMAP_BODY_Entity(); entity.Parse(r.ReadParenthesized()); entity.m_pParentEntity = this; m_pChildEntities.Add(entity); r.ReadToFirstChar(); } // Read multipart values. (nestedMimeEntries) contentTypeSubMediaType string contentTypeSubMediaType = r.ReadWord(); m_pContentType = MimeUtils.ParseMediaType("multipart/" + contentTypeSubMediaType); } else { // Basic fields for non-multipart // contentTypeMainMediaType contentTypeSubMediaType (conentTypeParameters) contentID contentDescription contentEncoding contentSize [envelope] [contentLine] // Content-Type string contentTypeMainMediaType = r.ReadWord(); string contentTypeSubMediaType = r.ReadWord(); if (contentTypeMainMediaType.ToUpper() != "NIL" && contentTypeSubMediaType.ToUpper() != "NIL") { m_pContentType = MimeUtils.ParseMediaType(contentTypeMainMediaType + "/" + contentTypeSubMediaType); } // Content-Type header field parameters // Parameters syntax: "name" <SP> "value" <SP> "name" <SP> "value" <SP> ... . r.ReadToFirstChar(); string conentTypeParameters = "NIL"; if (r.StartsWith("(")) { conentTypeParameters = r.ReadParenthesized(); StringReader contentTypeParamReader = new StringReader(MimeUtils.DecodeWords(conentTypeParameters)); List <HeaderFieldParameter> parameters = new List <HeaderFieldParameter>(); while (contentTypeParamReader.Available > 0) { string parameterName = contentTypeParamReader.ReadWord(); string parameterValue = contentTypeParamReader.ReadWord(); parameters.Add(new HeaderFieldParameter(parameterName, parameterValue)); } m_pContentTypeParams = parameters.ToArray(); } else { // Skip NIL r.ReadWord(); } // Content-ID: string contentID = r.ReadWord(); if (contentID.ToUpper() != "NIL") { m_ContentID = contentID; } // Content-Description: string contentDescription = r.ReadWord(); if (contentDescription.ToUpper() != "NIL") { m_ContentDescription = contentDescription; } // Content-Transfer-Encoding: string contentEncoding = r.ReadWord(); if (contentEncoding.ToUpper() != "NIL") { m_ContentEncoding = MimeUtils.ParseContentTransferEncoding(contentEncoding); } // Content Encoded data size in bytes string contentSize = r.ReadWord(); if (contentSize.ToUpper() != "NIL") { m_ContentSize = Convert.ToInt32(contentSize); } // Only for ContentType message/rfc822 if (ContentType == MediaType_enum.Message_rfc822) { r.ReadToFirstChar(); // envelope if (r.StartsWith("(")) { m_pEnvelope = new IMAP_Envelope(); m_pEnvelope.Parse(r.ReadParenthesized()); } else { // Skip NIL, ENVELOPE wasn't included r.ReadWord(); } // TODO: // BODYSTRUCTURE // contentLine } // Only for ContentType text/xxx if (contentTypeMainMediaType.ToLower() == "text") { // contentLine string contentLines = r.ReadWord(); if (contentLines.ToUpper() != "NIL") { m_ContentLines = Convert.ToInt32(contentLines); } } } }
/// <summary> /// Parses entity and it's child entities. /// </summary> internal void Parse(string text) { StringReader r = new StringReader(text); r.ReadToFirstChar(); // If starts with ( then multipart/xxx, otherwise normal single part entity if (r.StartsWith("(")) { // Entities are (entity1)(entity2)(...) <SP> ContentTypeSubType while (r.StartsWith("(")) { IMAP_BODY_Entity entity = new IMAP_BODY_Entity(); entity.Parse(r.ReadParenthesized()); entity.m_pParentEntity = this; m_pChildEntities.Add(entity); r.ReadToFirstChar(); } // Read multipart values. (nestedMimeEntries) contentTypeSubMediaType string contentTypeSubMediaType = r.ReadWord(); m_pContentType = MimeUtils.ParseMediaType("multipart/" + contentTypeSubMediaType); } else { // Basic fields for non-multipart // contentTypeMainMediaType contentTypeSubMediaType (conentTypeParameters) contentID contentDescription contentEncoding contentSize [envelope] [contentLine] // Content-Type string contentTypeMainMediaType = r.ReadWord(); string contentTypeSubMediaType = r.ReadWord(); if (contentTypeMainMediaType.ToUpper() != "NIL" && contentTypeSubMediaType.ToUpper() != "NIL") { m_pContentType = MimeUtils.ParseMediaType(contentTypeMainMediaType + "/" + contentTypeSubMediaType); } // Content-Type header field parameters // Parameters syntax: "name" <SP> "value" <SP> "name" <SP> "value" <SP> ... . r.ReadToFirstChar(); string conentTypeParameters = "NIL"; if (r.StartsWith("(")) { conentTypeParameters = r.ReadParenthesized(); StringReader contentTypeParamReader = new StringReader(MimeUtils.DecodeWords(conentTypeParameters)); List<HeaderFieldParameter> parameters = new List<HeaderFieldParameter>(); while (contentTypeParamReader.Available > 0) { string parameterName = contentTypeParamReader.ReadWord(); string parameterValue = contentTypeParamReader.ReadWord(); parameters.Add(new HeaderFieldParameter(parameterName, parameterValue)); } m_pContentTypeParams = parameters.ToArray(); } else { // Skip NIL r.ReadWord(); } // Content-ID: string contentID = r.ReadWord(); if (contentID.ToUpper() != "NIL") { m_ContentID = contentID; } // Content-Description: string contentDescription = r.ReadWord(); if (contentDescription.ToUpper() != "NIL") { m_ContentDescription = contentDescription; } // Content-Transfer-Encoding: string contentEncoding = r.ReadWord(); if (contentEncoding.ToUpper() != "NIL") { m_ContentEncoding = MimeUtils.ParseContentTransferEncoding(contentEncoding); } // Content Encoded data size in bytes string contentSize = r.ReadWord(); if (contentSize.ToUpper() != "NIL") { m_ContentSize = Convert.ToInt32(contentSize); } // Only for ContentType message/rfc822 if (ContentType == MediaType_enum.Message_rfc822) { r.ReadToFirstChar(); // envelope if (r.StartsWith("(")) { m_pEnvelope = new IMAP_Envelope(); m_pEnvelope.Parse(r.ReadParenthesized()); } else { // Skip NIL, ENVELOPE wasn't included r.ReadWord(); } // TODO: // BODYSTRUCTURE // contentLine } // Only for ContentType text/xxx if (contentTypeMainMediaType.ToLower() == "text") { // contentLine string contentLines = r.ReadWord(); if (contentLines.ToUpper() != "NIL") { m_ContentLines = Convert.ToInt32(contentLines); } } } }
/// <summary> /// Converts ContentTransferEncoding_enum to string. /// </summary> /// <param name="encoding">Encoding value to convert.</param> /// <returns></returns> private string ContentTransferEncoding_ToString(ContentTransferEncoding_enum encoding) { string retVal = ""; if(encoding == ContentTransferEncoding_enum._7bit){ retVal = "7bit"; } else if(encoding == ContentTransferEncoding_enum.QuotedPrintable){ retVal = "quoted-printable"; } else if(encoding == ContentTransferEncoding_enum.Base64){ retVal = "base64"; } else if(encoding == ContentTransferEncoding_enum._8bit){ retVal = "8bit"; } else if(encoding == ContentTransferEncoding_enum.Binary){ retVal = "binary"; } return retVal; }