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.
 
 
 
 
 

280 lines
9.1 KiB

using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
using MailKit.Security;
using MimeKit;
using MimeKit.Text;
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 = false;
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 = "新的邮件通知";
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;
try
{
//是否使用默认凭据,若为false,则使用自定义的证书,就是下面的networkCredential实例对象
smtpClient.UseDefaultCredentials = true;
//指定邮箱账号和密码,需要注意的是,这个密码是你在QQ邮箱设置里开启服务的时候给你的那个授权码
System.Net.NetworkCredential networkCredential = new System.Net.NetworkCredential(username, password);
smtpClient.Credentials = networkCredential;
//发送邮件
smtpClient.Send(mailMessage);
}
catch (System.Net.Mail.SmtpException ex)
{
throw new Exception(ex.Message + ex.InnerException?.Message);
// Console.WriteLine(ex.Message, "发送邮件出错");
}
}
}
}
public class SmtpMailSender
{
private readonly SmtpClient _client = new SmtpClient();
private MailMessage _message = new MailMessage();
private readonly string _targetName;
public SmtpMailSender(string targetName)
{
_targetName = targetName;
}
public void BuildClient(string host, int port, bool enableSsl, string username, string password)
{
_client.Host = host;
//587 or 25
_client.Port = port;
// This require to be before setting Credentials property
_client.UseDefaultCredentials = false;
_client.DeliveryMethod = SmtpDeliveryMethod.Network;
// you must give a full email address for authentication
_client.Credentials = new System.Net.NetworkCredential(username, password);
// Set to avoid MustIssueStartTlsFirst exception
_client.TargetName = _targetName;
// Set to avoid secure connection exception
_client.EnableSsl = enableSsl;
}
public void BuildMessage(EmailAddress fromAddress, List<EmailAddress> toAddressList, string subject, string body)
{
_message = new MailMessage()
{
// sender must be a full email address
From = new MailAddress(fromAddress.Address, fromAddress.Name),
Subject = subject,
IsBodyHtml = true,
Body = body,
BodyEncoding = Encoding.UTF8,
SubjectEncoding = Encoding.UTF8,
};
foreach (var emailAddress in toAddressList)
{
_message.To.Add(new MailAddress(emailAddress.Address, emailAddress.Name));
}
}
public void Send()
{
try
{
_client.Send(_message);
Console.WriteLine("Smtp Send Success:" + _message.To[0].Address);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine();
}
}
public async Task SendAsync()
{
try
{
await _client.SendMailAsync(_message);
Console.WriteLine("Smtp Send Success:" + _message.To[0].Address);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
Console.WriteLine();
}
}
}
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();
}
}
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();
}
}
}
public class EmailAddress
{
public EmailAddress(string address, string name)
{
Address = address;
Name = name;
}
public string Name { get; set; }
public string Address { get; set; }
}
}