Exemplo n.º 1
0
        /// <summary>
        /// Creates Mime message based on UI data.
        /// </summary>
        private Mail_Message CreateMessage()
        {
            Mail_Message msg = new Mail_Message();

            msg.MimeVersion = "1.0";
            msg.MessageID   = MIME_Utils.CreateMessageID();
            msg.Date        = DateTime.Now;
            msg.From        = Mail_h_MailboxList.Parse("From: " + m_pFrom.Text).Addresses;
            msg.To          = new Mail_t_AddressList();
            msg.To.Add(new Mail_t_Mailbox(m_pFolder.User.FullName, m_pFolder.User.FullName + "@localhost"));
            msg.Subject = m_pSubject.Text;

            //--- multipart/mixed -------------------------------------------------------------------------------------------------
            MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);

            contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
            MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);

            msg.Body = multipartMixed;

            //--- multipart/alternative -----------------------------------------------------------------------------------------
            MIME_Entity        entity_multipartAlternative      = 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_multipartAlternative.Body = multipartAlternative;
            multipartMixed.BodyParts.Add(entity_multipartAlternative);

            //--- 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, m_pText.Text);
            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, RtfToHtml());
            multipartAlternative.BodyParts.Add(entity_text_html);

            //--- application/octet-stream -----------------------------------------------------------------------------------------------
            foreach (ListViewItem item in m_pAttachments.Items)
            {
                multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(item.Tag.ToString()));
            }

            return(msg);
        }
Exemplo n.º 2
0
    public void CreateMultiMail(ConfigMail mail)
    {
        CreateMail(mail);
        var contentTypeMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);

        contentTypeMixed.Param_Boundary = Guid.NewGuid().ToString().Replace("-", "_");
        var multipartMixed = new MIME_b_MultipartMixed(contentTypeMixed);

        Mail.Body = multipartMixed;            //Create a entity to hold multipart/alternative body
        var entityAlternative      = new MIME_Entity();
        var contentTypeAlternative = new MIME_h_ContentType(MIME_MediaTypes.Multipart.alternative);

        contentTypeAlternative.Param_Boundary = Guid.NewGuid().ToString().Replace("-", "_");
        var multipartAlternative = new MIME_b_MultipartAlternative(contentTypeAlternative);

        entityAlternative.Body = multipartAlternative;
        multipartMixed.BodyParts.Add(entityAlternative);
        var entityTextPlain = new MIME_Entity();
        var plain           = new MIME_b_Text(MIME_MediaTypes.Text.plain);

        entityTextPlain.Body = plain;
        plain.SetText(MIME_TransferEncodings.Base64, Encoding.UTF8, "If you see this message, it means that your mail client does not support html.");
        multipartAlternative.BodyParts.Add(entityTextPlain);
        var entityTextHtml = new MIME_Entity();
        var html           = new MIME_b_Text(MIME_MediaTypes.Text.html);

        entityTextHtml.Body = html;
        html.SetText(MIME_TransferEncodings.Base64, Encoding.UTF8, mail.Body);
        multipartAlternative.BodyParts.Add(entityTextHtml);
        foreach (string attachment in mail.Attachments)
        {
            multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(attachment));
        }
        foreach (string resource in mail.Resources)
        {
            var entity = new MIME_Entity();
            entity.ContentDisposition = new MIME_h_ContentDisposition(MIME_DispositionTypes.Inline);
            entity.ContentID          = Convert.ToBase64String(Encoding.Default.GetBytes(Path.GetFileName(resource))); //eg.<img src="cid:ContentID"/>
            var image = new MIME_b_Image(MIME_MediaTypes.Image.jpeg);
            entity.Body = image;
            image.SetDataFromFile(resource, MIME_TransferEncodings.Base64);
            multipartMixed.BodyParts.Add(entity);
        }
    }
Exemplo n.º 3
0
        private Mail_Message Create_PlainText_Html_Attachment_Image(Dictionary <string, string> tomails, Dictionary <string, string> ccmails, string mailFrom, string mailFromDisplay,
                                                                    string subject, string body, Dictionary <string, string> attachments, string notifyEmail = "", string plaintTextTips = "", bool checkspmail = false)
        {
            Mail_Message msg = new Mail_Message();

            msg.MimeVersion = "1.0";
            msg.MessageID   = MIME_Utils.CreateMessageID();
            msg.Date        = DateTime.Now;
            msg.Subject     = subject;
            msg.From        = new Mail_t_MailboxList();
            msg.From.Add(new Mail_t_Mailbox(mailFromDisplay, mailFrom));
            msg.To = new Mail_t_AddressList();
            foreach (string address in tomails.Keys)
            {
                string displayName = tomails[address];
                msg.To.Add(new Mail_t_Mailbox(displayName, address));
            }
            msg.Cc = new Mail_t_AddressList();
            foreach (string address in ccmails.Keys)
            {
                string displayName = ccmails[address];
                msg.Cc.Add(new Mail_t_Mailbox(displayName, address));
            }


            if (!string.IsNullOrEmpty(notifyEmail))
            {
                msg.DispositionNotificationTo.Add(new Mail_t_Mailbox(notifyEmail, notifyEmail));
            }

            #region MyRegion


            MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
            contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
            MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
            msg.Body = multipartMixed;


            MIME_Entity        entity_multipartAlternative      = 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_multipartAlternative.Body = multipartAlternative;
            multipartMixed.BodyParts.Add(entity_multipartAlternative);


            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;


            string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
            if (!string.IsNullOrEmpty(plaintTextTips))
            {
                plainTextBody = plaintTextTips;
            }

            text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, plainTextBody);
            multipartAlternative.BodyParts.Add(entity_text_plain);


            string      htmlText         = body;
            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, htmlText);
            multipartAlternative.BodyParts.Add(entity_text_html);

            var i = 1;


            foreach (string attach in attachments.Keys)
            {
                string   filename = string.Empty;
                FileInfo fino     = new FileInfo(attach);
                if (mAttRename)
                {
                    filename = mAttachmentName + i.ToString() + fino.Extension;
                }
                else
                {
                    if (!string.IsNullOrEmpty(attachments[attach]))
                    {
                        filename = attachments[attach];
                    }
                    else
                    {
                        filename = fino.Name;
                    }
                }

                if (filename.Length > 30)
                {
                    FileInfo aainfo = new FileInfo(filename);
                    filename = filename.Substring(0, 15) + "_略" + aainfo.Extension;
                }



                filename = MimeUtils.EncodeWord(filename);


                using (var fs = File.OpenRead(attach))
                {
                    var ate = Mail_Message.CreateAttachment(fs, filename);
                    multipartAlternative.BodyParts.Add(ate);
                    fs.Close();
                }
            }
            #endregion

            return(msg);
        }
Exemplo n.º 4
0
        public static Mail_Message Create_PlainText_Html_Attachment_Image(Dictionary <string, string> tomails, Dictionary <string, string> ccmails, string mailFrom, string mailFromDisplay,
                                                                          string subject, string body, Dictionary <string, string> attachments, string notifyEmail = "", string plaintTextTips = "")
        {
            Mail_Message msg = new Mail_Message();

            msg.MimeVersion = "1.0";
            msg.MessageID   = MIME_Utils.CreateMessageID();
            msg.Date        = DateTime.Now;
            msg.Subject     = subject;
            msg.From        = new Mail_t_MailboxList();
            msg.From.Add(new Mail_t_Mailbox(mailFromDisplay, mailFrom));
            msg.To = new Mail_t_AddressList();
            foreach (string address in tomails.Keys)
            {
                string displayName = tomails[address];
                msg.To.Add(new Mail_t_Mailbox(displayName, address));
            }
            msg.Cc = new Mail_t_AddressList();
            foreach (string address in ccmails.Keys)
            {
                string displayName = ccmails[address];
                msg.Cc.Add(new Mail_t_Mailbox(displayName, address));
            }

            //设置回执通知
            if (!string.IsNullOrEmpty(notifyEmail) && IsEmail(notifyEmail))
            {
                msg.DispositionNotificationTo.Add(new Mail_t_Mailbox(notifyEmail, notifyEmail));
            }

            #region MyRegion

            //--- multipart/mixed -----------------------------------
            MIME_h_ContentType contentType_multipartMixed = new MIME_h_ContentType(MIME_MediaTypes.Multipart.mixed);
            contentType_multipartMixed.Param_Boundary = Guid.NewGuid().ToString().Replace('-', '.');
            MIME_b_MultipartMixed multipartMixed = new MIME_b_MultipartMixed(contentType_multipartMixed);
            msg.Body = multipartMixed;

            //--- multipart/alternative -----------------------------
            MIME_Entity        entity_multipartAlternative      = 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_multipartAlternative.Body = multipartAlternative;
            multipartMixed.BodyParts.Add(entity_multipartAlternative);

            //--- 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;

            //普通文本邮件内容,如果对方的收件客户端不支持HTML,这是必需的
            string plainTextBody = "如果你邮件客户端不支持HTML格式,或者你切换到“普通文本”视图,将看到此内容";
            if (!string.IsNullOrEmpty(plaintTextTips))
            {
                plainTextBody = plaintTextTips;
            }

            text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, plainTextBody);
            multipartAlternative.BodyParts.Add(entity_text_plain);

            //--- text/html -----------------------------------------
            string      htmlText         = body;//"<html>这是一份测试邮件,<img src=\"cid:test.jpg\">来自<font color=red><b>LumiSoft.Net</b></font></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, htmlText);
            multipartAlternative.BodyParts.Add(entity_text_html);

            //--- application/octet-stream -------------------------
            WebClient client = new WebClient();
            foreach (string attach in attachments.Keys)
            {
                try
                {
                    byte[] bytes = client.DownloadData(attachments[attach]);
                    using (MemoryStream stream = new MemoryStream(bytes))
                    {
                        multipartMixed.BodyParts.Add(Mail_Message.CreateAttachment(stream, attach));
                    }
                }
                catch (Exception ex)
                {
                    throw ex;
                }
            }

            #endregion

            return(msg);
        }