Пример #1
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);
        }