123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129 |
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using MailKit.Net.Smtp;
- using System.Configuration;
- using Bowin.Common.Utility;
- using System.IO;
- using Microsoft.Extensions.DependencyInjection;
- using Microsoft.Extensions.Configuration;
- using MimeKit;
- using MailKit.Net.Pop3;
- namespace Bowin.Common.Mail
- {
- public static class MailHelper
- {
- private const int _DEFAULT_SMTP_PORT = 25;
- private const int _DEFAULT_POP3_PORT = 110;
- private static MailSettings Settings { get; set; }
- static MailHelper()
- {
- var configuration = new ConfigurationBuilder()
- .SetBasePath(Directory.GetCurrentDirectory())
- .AddJsonFile("appsettings.json")
- .Build();
- Settings = configuration.GetSection("BowinMail").Get<MailSettings>();
- }
- private static string GetSenderEmail()
- {
- string hostName = Settings.SMTPServer.Substring(Settings.SMTPServer.IndexOf('.') + 1);
- return Settings.UserName + "@" + hostName;
- }
- public static void SendMail(string receiver, string title, string content)
- {
- if (string.IsNullOrEmpty(Settings.SMTPServer))
- {
- throw new SendMailException("请指定发送邮件的STMP服务器地址。");
- }
- if (!Settings.SMTPPort.HasValue)
- {
- Settings.SMTPPort = _DEFAULT_SMTP_PORT;
- }
- if (!Settings.SMTPIsSSL.HasValue)
- {
- Settings.SMTPIsSSL = false;
- }
- if (string.IsNullOrEmpty(Settings.UserName) || string.IsNullOrEmpty(Settings.Password))
- {
- throw new SendMailException("请指定发送邮件的账号和密码。");
- }
- MimeMessage message = new MimeMessage();
- message.From.Add(new MailboxAddress(GetSenderEmail()));
- message.To.Add(new MailboxAddress(receiver));
- message.Subject = title;
- message.Body = new BodyBuilder()
- {
- HtmlBody = content
- }.ToMessageBody();
- using (var client = new SmtpClient())
- {
- try
- {
- client.ServerCertificateValidationCallback = (s, c, h, e) => true;
- client.Connect(Settings.SMTPServer, Settings.SMTPPort.Value, Settings.SMTPIsSSL.Value);
- client.AuthenticationMechanisms.Remove("XOAUTH2");
- client.Authenticate(Settings.UserName, Settings.Password);
- client.Send(message);
- client.Disconnect(true);
- }
- catch (Exception ex)
- {
- throw new SendMailException("邮件发送失败,详情请参考InnerException", ex);
- }
- }
- }
- public static List<MimeMessage> ReceiveMail(string mailAddress)
- {
- if (string.IsNullOrEmpty(Settings.Pop3Server))
- {
- throw new ReceiveMailException("请指定发送邮件的POP3服务器地址。");
- }
- if (!Settings.Pop3Port.HasValue)
- {
- Settings.Pop3Port = _DEFAULT_POP3_PORT;
- }
- if (!Settings.Pop3IsSSL.HasValue)
- {
- Settings.Pop3IsSSL = false;
- }
- if (string.IsNullOrEmpty(Settings.UserName) || string.IsNullOrEmpty(Settings.Password))
- {
- throw new ReceiveMailException("请指定接收邮件的账号和密码。");
- }
- List<MimeMessage> mailList = new List<MimeMessage>();
- using (var client = new Pop3Client())
- {
- client.ServerCertificateValidationCallback = (s, c, h, e) => true;
- client.Connect(Settings.Pop3Server, Settings.Pop3Port.Value, Settings.Pop3IsSSL.Value);
- client.AuthenticationMechanisms.Remove("XOAUTH2");
- client.Authenticate(Settings.UserName, Settings.Password);
- for (int i = 0; i < client.Count; i++)
- {
- var message = client.GetMessage(i);
- mailList.Add(message);
- }
- client.Disconnect(true);
- }
- return mailList;
- }
- }
- }
|