示例#1
0
文件: Mime.cs 项目: nbhopson/QMail
        /// <summary>
        /// Parses mime message from stream.
        /// </summary>
        /// <param name="stream">Mime message stream.</param>
        /// <returns></returns>
        public static Mime Parse(Stream stream)
        {
            Mime mime = new Mime();
            mime.MainEntity.Parse(new SmartStream(stream,false),null);

            return mime;
        }
示例#2
0
文件: Mime.cs 项目: nbhopson/QMail
        /// <summary>
        /// Creates simple mime message with attachments.
        /// </summary>
        /// <param name="from">Header field From: value.</param>
        /// <param name="to">Header field To: value.</param>
        /// <param name="subject">Header field Subject: value.</param>
        /// <param name="bodyText">Body text of message. NOTE: Pass null is body text isn't wanted.</param>
        /// <param name="bodyHtml">Body HTML text of message. NOTE: Pass null is body HTML text isn't wanted.</param>
        /// <param name="attachmentFileNames">Attachment file names. Pass null if no attachments. NOTE: File name must contain full path to file, for example: c:\test.pdf.</param>
        /// <returns></returns>
        public static Mime CreateSimple(AddressList from,AddressList to,string subject,string bodyText,string bodyHtml,string[] attachmentFileNames)
        {
            Mime m = new Mime();

            MimeEntity mainEntity = m.MainEntity;
            mainEntity.From = from;
            mainEntity.To = to;
            mainEntity.Subject = subject;

            // There are no atachments
            if(attachmentFileNames == null || attachmentFileNames.Length == 0){
                // If bodyText and bodyHtml both specified
                if(bodyText != null && bodyHtml != null){
                    mainEntity.ContentType = MediaType_enum.Multipart_alternative;

                    MimeEntity textEntity = mainEntity.ChildEntities.Add();
                    textEntity.ContentType = MediaType_enum.Text_plain;
                    textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textEntity.DataText = bodyText;

                    MimeEntity textHtmlEntity = mainEntity.ChildEntities.Add();
                    textHtmlEntity.ContentType = MediaType_enum.Text_html;
                    textHtmlEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textHtmlEntity.DataText = bodyHtml;
                }
                // There is only body text
                else if(bodyText != null){
                    MimeEntity textEntity = mainEntity;
                    textEntity.ContentType = MediaType_enum.Text_plain;
                    textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textEntity.DataText = bodyText;
                }
                // There is only body html text
                else if(bodyHtml != null){
                    MimeEntity textHtmlEntity = mainEntity;
                    textHtmlEntity.ContentType = MediaType_enum.Text_html;
                    textHtmlEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textHtmlEntity.DataText = bodyHtml;
                }
            }
            // There are attachments
            else{
                mainEntity.ContentType = MediaType_enum.Multipart_mixed;

                // If bodyText and bodyHtml both specified
                if(bodyText != null && bodyHtml != null){
                    MimeEntity multiPartAlternativeEntity = mainEntity.ChildEntities.Add();
                    multiPartAlternativeEntity.ContentType = MediaType_enum.Multipart_alternative;

                    MimeEntity textEntity = multiPartAlternativeEntity.ChildEntities.Add();
                    textEntity.ContentType = MediaType_enum.Text_plain;
                    textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textEntity.DataText = bodyText;

                    MimeEntity textHtmlEntity = multiPartAlternativeEntity.ChildEntities.Add();
                    textHtmlEntity.ContentType = MediaType_enum.Text_html;
                    textHtmlEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textHtmlEntity.DataText = bodyHtml;
                }
                // There is only body text
                else if(bodyText != null){
                    MimeEntity textEntity = mainEntity.ChildEntities.Add();
                    textEntity.ContentType = MediaType_enum.Text_plain;
                    textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textEntity.DataText = bodyText;
                }
                // There is only body html text
                else if(bodyHtml != null){
                    MimeEntity textHtmlEntity = mainEntity.ChildEntities.Add();
                    textHtmlEntity.ContentType = MediaType_enum.Text_html;
                    textHtmlEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable;
                    textHtmlEntity.DataText = bodyHtml;
                }

                foreach(string fileName in attachmentFileNames){
                    MimeEntity attachmentEntity = mainEntity.ChildEntities.Add();
                    attachmentEntity.ContentType = MediaType_enum.Application_octet_stream;
                    attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment;
                    attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64;
                    attachmentEntity.ContentDisposition_FileName = Core.GetFileNameFromPath(fileName);
                    attachmentEntity.DataFromFile(fileName);
                }
            }

            return m;
        }