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("请指定发送邮件的账号和密码。");
- }
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- 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;
- }
- }
- }
|