You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
298 lines
11 KiB
298 lines
11 KiB
using MailKit.Security;
|
|
using MimeKit;
|
|
using MimeKit.Text;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Net.Mail;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace CK.SCP.Common
|
|
{
|
|
|
|
public class MyWebSmtpMail
|
|
{
|
|
private MailMessage mailMessage;
|
|
private SmtpClient smtpClient;
|
|
private string from;
|
|
private string server;
|
|
private string username;
|
|
private string password;
|
|
private bool enablessl;
|
|
private bool credentials;
|
|
private int port;
|
|
|
|
public MyWebSmtpMail()
|
|
{
|
|
string From = ConfigurationManager.AppSettings["MailSender"];
|
|
string Server = ConfigurationManager.AppSettings["MailServer"];
|
|
string UserName = ConfigurationManager.AppSettings["MailUserName"];
|
|
string Password = ConfigurationManager.AppSettings["MailPassword"];
|
|
int Port = Convert.ToInt32(ConfigurationManager.AppSettings["MailPort"]);
|
|
|
|
this.from = From;
|
|
this.server = Server;
|
|
this.password = Password;
|
|
this.username = UserName;
|
|
this.enablessl = true;
|
|
this.port = Port;
|
|
this.credentials = false;
|
|
}
|
|
|
|
public void Send(string To, string Body, string Title)
|
|
{
|
|
mailMessage = new MailMessage();
|
|
mailMessage.To.Add(To);
|
|
mailMessage.From = new System.Net.Mail.MailAddress(this.from);
|
|
mailMessage.Subject = Title;
|
|
mailMessage.Body = Body;
|
|
mailMessage.IsBodyHtml = true;
|
|
mailMessage.BodyEncoding = System.Text.Encoding.UTF8;
|
|
mailMessage.Priority = System.Net.Mail.MailPriority.Normal;
|
|
|
|
if (mailMessage != null)
|
|
{
|
|
smtpClient = new SmtpClient();
|
|
if (!credentials)
|
|
{
|
|
smtpClient.Credentials = new System.Net.NetworkCredential(username, password);
|
|
}
|
|
smtpClient.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
|
|
smtpClient.Host = server;
|
|
smtpClient.Port = port;
|
|
smtpClient.EnableSsl = enablessl;
|
|
//smtpClient.Send(mailMessage);
|
|
//smtpClient.SendAsync(mailMessage, null);
|
|
|
|
//如果服务器支持安全连接,则将安全连接设为true
|
|
//smtpClient.EnableSsl = true;
|
|
try
|
|
{
|
|
//是否使用默认凭据,若为false,则使用自定义的证书,就是下面的networkCredential实例对象
|
|
smtpClient.UseDefaultCredentials = false;
|
|
|
|
//指定邮箱账号和密码,需要注意的是,这个密码是你在QQ邮箱设置里开启服务的时候给你的那个授权码
|
|
System.Net.NetworkCredential networkCredential = new System.Net.NetworkCredential(username, password);
|
|
smtpClient.Credentials = networkCredential;
|
|
|
|
//发送邮件
|
|
smtpClient.Send(mailMessage);
|
|
|
|
}
|
|
catch (System.Net.Mail.SmtpException ex)
|
|
{
|
|
Console.WriteLine(ex.Message, "发送邮件出错");
|
|
}
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 材料邮件
|
|
/// </summary>
|
|
/// <param name="To"></param>
|
|
/// <param name="Body"></param>
|
|
/// <param name="Title"></param>
|
|
public void SendOrdersEmail(string To, string Body, string Title)
|
|
{
|
|
mailMessage = new MailMessage();
|
|
mailMessage.To.Add(To);
|
|
mailMessage.From = new MailAddress("faway_cyjdd@163.com");
|
|
mailMessage.Subject = Title;
|
|
mailMessage.Body = Body;
|
|
mailMessage.IsBodyHtml = true;
|
|
mailMessage.BodyEncoding = Encoding.UTF8;
|
|
mailMessage.Priority = MailPriority.Normal;
|
|
|
|
if (mailMessage != null)
|
|
{
|
|
smtpClient = new SmtpClient();
|
|
if (!credentials)
|
|
{
|
|
smtpClient.Credentials = new System.Net.NetworkCredential("faway_cyjdd@163.com", "XOVCAHAWSHYUNWWT");
|
|
}
|
|
smtpClient.DeliveryMethod = SmtpDeliveryMethod.Network;
|
|
smtpClient.Host = "smtp.163.com";
|
|
smtpClient.Port = 25;
|
|
smtpClient.EnableSsl = false;
|
|
//smtpClient.Send(mailMessage);
|
|
//smtpClient.SendAsync(mailMessage, null);
|
|
|
|
//如果服务器支持安全连接,则将安全连接设为true
|
|
//smtpClient.EnableSsl = true;
|
|
try
|
|
{
|
|
//是否使用默认凭据,若为false,则使用自定义的证书,就是下面的networkCredential实例对象
|
|
smtpClient.UseDefaultCredentials = false;
|
|
|
|
//指定邮箱账号和密码,需要注意的是,这个密码是你在QQ邮箱设置里开启服务的时候给你的那个授权码
|
|
System.Net.NetworkCredential networkCredential = new System.Net.NetworkCredential("faway_cyjdd@163.com", "XOVCAHAWSHYUNWWT");
|
|
smtpClient.Credentials = networkCredential;
|
|
|
|
//发送邮件
|
|
smtpClient.Send(mailMessage);
|
|
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
throw ex;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
public class EmailAddress
|
|
{
|
|
public EmailAddress(string address, string name)
|
|
{
|
|
Address = address;
|
|
Name = name;
|
|
}
|
|
public string Name { get; set; }
|
|
public string Address { get; set; }
|
|
}
|
|
|
|
public class MailKitMailSender
|
|
{
|
|
private readonly MailKit.Net.Smtp.SmtpClient _client = new MailKit.Net.Smtp.SmtpClient();
|
|
private MimeMessage _message = new MimeMessage();
|
|
|
|
private string host;//邮箱服务器
|
|
private int port;//端口
|
|
private string from;//发送的邮箱
|
|
private string password;//密码
|
|
private bool enablessl;//是否ssl
|
|
private string username;//发送名称
|
|
|
|
private readonly SecureSocketOptions _secureSocketOptions;
|
|
|
|
private MailKitMailSender()
|
|
{
|
|
_client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
|
}
|
|
|
|
public MailKitMailSender(SecureSocketOptions secureSocketOptions) : this()
|
|
{
|
|
_secureSocketOptions = secureSocketOptions;
|
|
host = ConfigurationManager.AppSettings["MailServer"];
|
|
port = Convert.ToInt32(ConfigurationManager.AppSettings["MailPort"]);
|
|
from = ConfigurationManager.AppSettings["MailSender"];
|
|
username = ConfigurationManager.AppSettings["MailUserName"];
|
|
password = ConfigurationManager.AppSettings["MailPassword"];
|
|
enablessl = false;
|
|
}
|
|
|
|
public void BuildMessage(List<EmailAddress> toAddressList, string subject, string body, string model)
|
|
{
|
|
_message = new MimeMessage();
|
|
|
|
_message.Sender = new MailboxAddress(username, from);
|
|
_message.From.Add(new MailboxAddress(username, from));
|
|
_message.Subject = subject;
|
|
_message.Body = new TextPart(TextFormat.Html) { Text = body };
|
|
|
|
foreach (var emailAddress in toAddressList)
|
|
{
|
|
if (model == "BCC")//抄送模式(抄送模式只发送一个邮件,避免大量发重复邮件)
|
|
{
|
|
_message.Bcc.Add(new MailboxAddress(emailAddress.Name, emailAddress.Address));
|
|
}
|
|
else
|
|
{
|
|
_message.To.Add(new MailboxAddress(emailAddress.Name, emailAddress.Address));
|
|
}
|
|
|
|
}
|
|
}
|
|
public void Send()
|
|
{
|
|
try
|
|
{
|
|
_client.Connect(host, port, _secureSocketOptions);
|
|
_client.Authenticate(from, password);
|
|
_client.Send(_message);
|
|
_client.Disconnect(true);
|
|
Console.WriteLine("MailKit Send Success:" + _message.To.Mailboxes.First());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
Console.WriteLine();
|
|
|
|
}
|
|
}
|
|
|
|
/// <summary>
|
|
/// 材料
|
|
/// </summary>
|
|
/// <param name="toAddressList"></param>
|
|
/// <param name="subject"></param>
|
|
/// <param name="body"></param>
|
|
/// <param name="model"></param>
|
|
public void BuildOrderMessage(List<EmailAddress> toAddressList, string subject, string body, string model)
|
|
{
|
|
_message = new MimeMessage();
|
|
|
|
_message.Sender = new MailboxAddress("faway_cyjdd@163.com", "faway_cyjdd@163.com");
|
|
_message.From.Add(new MailboxAddress("faway_cyjdd@163.com", "faway_cyjdd@163.com"));
|
|
_message.Subject = subject;
|
|
_message.Body = new TextPart(TextFormat.Html) { Text = body };
|
|
|
|
foreach (var emailAddress in toAddressList)
|
|
{
|
|
if (model == "BCC")//抄送模式(抄送模式只发送一个邮件,避免大量发重复邮件)
|
|
{
|
|
_message.Bcc.Add(new MailboxAddress(emailAddress.Name, emailAddress.Address));
|
|
}
|
|
else
|
|
{
|
|
_message.To.Add(new MailboxAddress(emailAddress.Name, emailAddress.Address));
|
|
}
|
|
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 材料
|
|
/// </summary>
|
|
public void SendOrder()
|
|
{
|
|
try
|
|
{
|
|
_client.Connect("smtp.163.com", 25, _secureSocketOptions);
|
|
_client.Authenticate("faway_cyjdd@163.com", "XOVCAHAWSHYUNWWT");
|
|
_client.Send(_message);
|
|
_client.Disconnect(true);
|
|
Console.WriteLine("MailKit Send Success:" + _message.To.Mailboxes.First());
|
|
}
|
|
catch (SmtpException ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
Console.WriteLine();
|
|
throw ex;
|
|
}
|
|
}
|
|
|
|
|
|
public async Task SendAsync()
|
|
{
|
|
try
|
|
{
|
|
await _client.ConnectAsync(host, port, _secureSocketOptions);
|
|
await _client.AuthenticateAsync(from, password);
|
|
await _client.SendAsync(_message);
|
|
await _client.DisconnectAsync(true);
|
|
Console.WriteLine("MailKit Send Success:" + _message.To.Mailboxes.First());
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
Console.WriteLine(ex.Message);
|
|
Console.WriteLine();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|