public static Mime ExtractFromUue(Mime mimeMessage, string tempPath) { if ((mimeMessage.Attachments.Length == 0) && (IsUue(mimeMessage))) { string shortFileName = ExtractFileFromUue(mimeMessage, tempPath); if (!String.IsNullOrEmpty(shortFileName)) { MimeEntity uueAttach = new MimeEntity(); uueAttach.ContentType = MediaType_enum.Application_octet_stream; uueAttach.ContentDisposition = ContentDisposition_enum.Attachment; uueAttach.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; uueAttach.ContentDisposition_FileName = shortFileName; uueAttach.ContentType_Name = shortFileName; uueAttach.DataFromFile(tempPath + shortFileName); if (mimeMessage.MainEntity.ContentType != MediaType_enum.Multipart_mixed) { mimeMessage.MainEntity.Data = null; mimeMessage.MainEntity.ContentType = MediaType_enum.Multipart_mixed; } mimeMessage.MainEntity.ChildEntities.Add(uueAttach); } } return(mimeMessage); }
/// <summary> /// 调用lumisoft发送邮件 /// </summary> /// <param name="fromEmailAddr">发送者的邮件地址</param> /// <param name="toEmailAddr">给谁发的邮件地址</param> /// <param name="subjectText">主题</param> /// <param name="bodyText">正文</param> /// <param name="filePath">附件路径</param> /// <returns>成功与否</returns> public static bool SendMailByLumisoft(string fromEmailAddr, string toEmailAddr, string subjectText, string bodyText, string filePath) { Mime m = new Mime(); MimeEntity mainEntity = m.MainEntity; // Force to create From: header field mainEntity.From = new AddressList(); mainEntity.From.Add(new MailboxAddress(fromEmailAddr, fromEmailAddr)); // Force to create To: header field mainEntity.To = new AddressList(); mainEntity.To.Add(new MailboxAddress(toEmailAddr, toEmailAddr)); mainEntity.Subject = subjectText; //添加正文 mainEntity.ContentType = MediaType_enum.Multipart_mixed; MimeEntity textEntity = mainEntity.ChildEntities.Add(); textEntity.ContentType = MediaType_enum.Text_html; textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; textEntity.DataText = bodyText; //发送附件 if (!string.IsNullOrEmpty(filePath)) { MimeEntity attachmentEntity = new MimeEntity(); attachmentEntity.ContentType = MediaType_enum.Application_octet_stream; attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment; attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; attachmentEntity.ContentDisposition_FileName = filePath; attachmentEntity.DataFromFile(filePath); mainEntity.ChildEntities.Add(attachmentEntity); } try { SMTP_Client.QuickSend(m); return(true); } catch (Exception e) { //Console.Write(e.StackTrace); return(false); } }
/// <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); }
/// <summary> /// 创建Mime类型格式邮件 /// </summary> /// <param name="tomails"></param> /// <param name="ccmails"></param> /// <param name="mailFrom"></param> /// <param name="mailFromDisplay"></param> /// <param name="subject"></param> /// <param name="body"></param> /// <param name="attachments"></param> /// <param name="embedImages"></param> /// <param name="notifyEmail"></param> /// <param name="plaintTextTips"></param> /// <param name="replyEmail"></param> /// <returns></returns> public static Mime Create_HtmlBody_Attachment_Image(Dictionary <string, string> tomails, Dictionary <string, string> ccmails, string mailFrom, string mailFromDisplay, string subject, string body, List <string> attachments, Dictionary <string, string> embedImages, string notifyEmail = "", string plaintTextTips = "", string replyEmail = "") { Mime m = new Mime(); MimeEntity mainEntity = m.MainEntity; #region 设置收件箱 if (tomails != null) { mainEntity.To = new AddressList(); foreach (string address in tomails.Keys) { string displayName = tomails[address]; mainEntity.To.Add(new MailboxAddress(displayName, address)); } } #endregion #region 设置抄送箱 if (ccmails != null) { mainEntity.Cc = new AddressList(); foreach (string address in ccmails.Keys) { string displayName = ccmails[address]; mainEntity.Cc.Add(new MailboxAddress(displayName, address)); } } #endregion #region 设置发件箱 if (!string.IsNullOrWhiteSpace(mailFrom)) { mainEntity.From = new AddressList(); mainEntity.From.Add(new MailboxAddress(mailFromDisplay, mailFrom)); } #endregion mainEntity.Subject = subject; mainEntity.ContentType = MediaType_enum.Multipart_mixed; #region 设置回执通知 if (!string.IsNullOrEmpty(notifyEmail) && IsEmail(notifyEmail)) { mainEntity.DSN = notifyEmail; } #endregion MimeEntity textEntity = mainEntity.ChildEntities.Add(); textEntity.ContentType = MediaType_enum.Text_html; textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.QuotedPrintable; textEntity.DataText = body; //附件 foreach (string attach in attachments) { MimeEntity attachmentEntity = mainEntity.ChildEntities.Add(); attachmentEntity.ContentType = MediaType_enum.Application_octet_stream; attachmentEntity.ContentDisposition = ContentDisposition_enum.Attachment; attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; FileInfo file = new FileInfo(attach); attachmentEntity.ContentDisposition_FileName = file.Name; attachmentEntity.DataFromFile(attach); } //嵌入图片 foreach (string key in embedImages.Keys) { MimeEntity attachmentEntity = mainEntity.ChildEntities.Add(); attachmentEntity.ContentType = MediaType_enum.Application_octet_stream; attachmentEntity.ContentDisposition = ContentDisposition_enum.Inline; attachmentEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; string imageFile = embedImages[key]; FileInfo file = new FileInfo(imageFile); attachmentEntity.ContentDisposition_FileName = file.Name; //string displayName = Path.GetFileNameWithoutExtension(fileName); attachmentEntity.ContentID = key;//BytesTools.BytesToHex(Encoding.Default.GetBytes(fileName)); attachmentEntity.DataFromFile(imageFile); } return(m); }
/// <summary> /// 构造Mime(邮件消息体) /// </summary> /// <returns>消息对象</returns> public Mime MakeMime() { #region 构造邮件消息体 Mime m = new Mime(); MimeEntity mainEntity = m.MainEntity; // Force to create From: header field mainEntity.ContentType = MediaType_enum.Multipart_mixed; mainEntity.From = new AddressList(); mainEntity.From.Add(new MailboxAddress(this.Name, this.CurrentUserMail)); // Force to create To: header field mainEntity.To = new AddressList(); string[] _ArrayTo = this.To.Split(';'); foreach (string _str_each_to in _ArrayTo) { if (_str_each_to.Trim() != "") { string[] _array_each_to = _str_each_to.Split('@'); mainEntity.To.Add(new MailboxAddress(_array_each_to[0], _str_each_to)); } } // Force to create CC: header field mainEntity.Cc = new AddressList(); string[] _ArrayCC = this.CC.Split(';'); foreach (string _str_each_cc in _ArrayCC) { if (_str_each_cc.Trim() != "") { string[] _array_each_cc = _str_each_cc.Split('@'); mainEntity.Cc.Add(new MailboxAddress(_array_each_cc[0], _str_each_cc)); } } // Force to create BCC: header field mainEntity.Bcc = new AddressList(); string[] _ArrayBCC = this.BCC.Split(';'); foreach (string _str_each_bcc in _ArrayBCC) { if (_str_each_bcc.Trim() != "") { string[] _array_each_bcc = _str_each_bcc.Split('@'); mainEntity.Bcc.Add(new MailboxAddress(_array_each_bcc[0], _str_each_bcc)); } } mainEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; mainEntity.Subject = this.Subject; //开始建立邮件的内容文本(默认是html邮件) MimeEntity textEntity = mainEntity.ChildEntities.Add(); textEntity.ContentType = MediaType_enum.Text_html;//MediaType_enum.Text_plain; textEntity.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; if (this.ReturnReceipt) { mainEntity.Header.Add(new HeaderField("Disposition-Notification-To", this.CurrentUserMail)); } //添加附件(普通) foreach (string _str_each_attach_ in this.Mail_Attech_File_Path) { FileInfo fi = new FileInfo(_str_each_attach_); 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 = fi.Name; attachmentEntity.DataFromFile(fi.FullName); } //添加内嵌附件 foreach (string _str_each_inLine_Pic in this.Mail_Contact_Pic_Path) { //附件路径要处理 try { string attpath = _str_each_inLine_Pic.Replace("%20", " "); attpath = attpath.Replace("file:///", ""); attpath = attpath.Replace("file:", ""); attpath = attpath.Replace("//", @"\\"); attpath = attpath.Replace("/", @"\"); FileInfo fi = new FileInfo(attpath); MimeEntity attachmentEntityInLine = mainEntity.ChildEntities.Add(); attachmentEntityInLine.ContentType = MediaType_enum.Image; attachmentEntityInLine.ContentDisposition = ContentDisposition_enum.Inline; attachmentEntityInLine.ContentTransferEncoding = ContentTransferEncoding_enum.Base64; attachmentEntityInLine.ContentDisposition_FileName = fi.Name; attachmentEntityInLine.DataFromFile(fi.FullName); string Cid = attachmentEntityInLine.MessageID; if (!string.IsNullOrEmpty(Cid)) { this.Body = this.Body.Replace(attpath, string.Format("cid:{0}", Cid.Replace("<", "").Replace(">", ""))); } } catch { } } textEntity.DataText = this.Body; return(m); #endregion }