123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Net.Mail;
- using System.Configuration;
- using Bowin.Common.Utility;
- using LumiSoft.Net.POP3.Client;
- using LumiSoft.Net.Mail;
- using LumiSoft.Net.SMTP.Client;
- using LumiSoft.Net.AUTH;
- using LumiSoft.Net.MIME;
- using System.IO;
- namespace Bowin.Common.Mail
- {
- public class MailHelper
- {
- private static string smtpHost = ConfigurationManager.AppSettings["Mail_SMTPServer"];
- private static int? smtpPort = ConfigurationManager.AppSettings["Mail_SMTPPort"].ParseStrTo<int>();
- private static bool? smtpIsSSL = ConfigurationManager.AppSettings["Mail_SMTPIsSSL"].ParseStrTo<bool>();
- private static string popHost = ConfigurationManager.AppSettings["Mail_Pop3Server"];
- private static int? popPort = ConfigurationManager.AppSettings["Mail_Pop3Port"].ParseStrTo<int>();
- private static bool? popIsSSL = ConfigurationManager.AppSettings["Mail_Pop3IsSSL"].ParseStrTo<bool>();
- private static string userName = ConfigurationManager.AppSettings["Mail_UserName"];
- private static string password = ConfigurationManager.AppSettings["Mail_Password"];
- private const int _DEFAULT_SMTP_PORT = 25;
- private const int _DEFAULT_POP3_PORT = 110;
- private static string GetSenderEmail()
- {
- string hostName = smtpHost.Substring(smtpHost.IndexOf('.') + 1);
- return userName + "@" + hostName;
- }
- public static void SendMail(string receiver, string title, string content)
- {
- if (string.IsNullOrEmpty(smtpHost))
- {
- throw new SendMailException("请指定发送邮件的STMP服务器地址。");
- }
- if (!smtpPort.HasValue)
- {
- smtpPort = _DEFAULT_SMTP_PORT;
- }
- if (!smtpIsSSL.HasValue)
- {
- smtpIsSSL = false;
- }
- if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
- {
- throw new SendMailException("请指定发送邮件的账号和密码。");
- }
- //try
- //{
- // string sender = GetSenderEmail();
- // using (SMTP_Client client = new SMTP_Client())
- // {
- // var authInfo = new AUTH_SASL_Client_Plain(userName, password);
- // client.Connect(smtpHost, smtpPort.Value, smtpIsSSL.Value);
- // client.Auth(authInfo);
- // Mail_Message message = new Mail_Message();
- // message.From = new Mail_t_MailboxList();
- // message.From.Add(new Mail_t_Mailbox(sender, sender));
- // message.To = new Mail_t_AddressList();
- // message.To.Add(new Mail_t_Mailbox(receiver, receiver));
- // message.Subject = title;
- // //--- 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);
- // message.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格式,或者你切换到“普通文本”视图,将看到此内容";
- // text_plain.SetText(MIME_TransferEncodings.QuotedPrintable, Encoding.UTF8, plainTextBody);
- // multipartAlternative.BodyParts.Add(entity_text_plain);
- // //--- text/html -----------------------------------------
- // string htmlText = content;//"<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);
- // using (MemoryStream stream = new MemoryStream())
- // {
- // message.ToStream(stream, new MIME_Encoding_EncodedWord(MIME_EncodedWordEncoding.Q, Encoding.UTF8), Encoding.UTF8);
- // stream.Position = 0;
- // client.SendMessage(stream);
- // }
- // if (message != null)
- // {
- // message.Dispose();
- // }
- // client.Disconnect();
- // }
- //}
- //catch (Exception ex)
- //{
- // throw new SendMailException("邮件发送失败,详情请参考InnerException", ex);
- //}
- MailMessage message = new MailMessage();
- message.From = new MailAddress(GetSenderEmail());
- message.To.Add(receiver);
- message.Subject = title;
- message.IsBodyHtml = true;
- message.Body = content;
- SmtpClient client = new SmtpClient(smtpHost, smtpPort.Value);
- client.EnableSsl = smtpIsSSL.Value;
- client.Credentials = new System.Net.NetworkCredential(userName, password);
- try
- {
- client.Send(message);
- }
- catch (Exception ex)
- {
- throw new SendMailException("邮件发送失败,详情请参考InnerException", ex);
- }
- }
- public static List<Mail_Message> ReceiveMail(string mailAddress)
- {
- if (string.IsNullOrEmpty(popHost))
- {
- throw new SendMailException("请指定发送邮件的POP3服务器地址。");
- }
- if (!popPort.HasValue)
- {
- popPort = _DEFAULT_POP3_PORT;
- }
- if (!popIsSSL.HasValue)
- {
- popIsSSL = false;
- }
- if (string.IsNullOrEmpty(userName) || string.IsNullOrEmpty(password))
- {
- throw new SendMailException("请指定接收邮件的账号和密码。");
- }
- List<Mail_Message> mailList = new List<Mail_Message>();
- using (POP3_Client client = new POP3_Client())
- {
- client.Connect(popHost, popPort.Value, popIsSSL.Value);
- client.Login(userName, password);
- POP3_ClientMessageCollection coll = client.Messages;
- for (int i = 0; i < coll.Count; i++)
- {
- POP3_ClientMessage message = coll[i];
- Mail_Message mm = Mail_Message.ParseFromByte(coll[i].MessageToByte());
- mailList.Add(mm);
- }
- }
- return mailList;
- }
- }
- }
|