shgx_tz_mom/ZR.Common/MailHelper.cs

171 lines
5.7 KiB
C#
Raw Normal View History

2022-01-02 10:48:27 +08:00
using Infrastructure;
using MailKit.Net.Smtp;
2021-09-28 18:17:31 +08:00
using MimeKit;
using MimeKit.Text;
using System;
using System.Collections.Generic;
using System.IO;
namespace ZR.Common
{
public class MailHelper
{
/// <summary>
/// 发送人邮箱
/// </summary>
public string FromEmail { get; set; } = "";
/// <summary>
/// 发送人密码
/// </summary>
public string FromPwd { get; set; } = "";
/// <summary>
/// 发送协议
/// </summary>
public string Smtp { get; set; } = "smtp.qq.com";
/// <summary>
/// 协议端口
/// </summary>
public int Port { get; set; } = 587;
/// <summary>
/// 是否使用SSL协议
/// </summary>
public bool UseSsl { get; set; } = false;
2022-01-02 15:56:23 +08:00
public string mailSign = @"邮件来自C# 程序发送";
2022-01-02 10:48:27 +08:00
private readonly MailOptions mailOptions = new();
2021-09-28 18:17:31 +08:00
2022-01-02 10:48:27 +08:00
public MailHelper()
{
2022-02-23 18:30:17 +08:00
AppSettings.Bind("MailOptions", mailOptions);
2022-01-02 10:48:27 +08:00
FromEmail = mailOptions.From;
Smtp = mailOptions.Smtp;
FromPwd = mailOptions.Password;
Port = mailOptions.Port;
}
2021-09-28 18:17:31 +08:00
public MailHelper(string fromEmail, string smtp, int port, string fromPwd)
{
FromEmail = fromEmail;
Smtp = smtp;
FromPwd = fromPwd;
Port = port;
}
public MailHelper(string fromEmail, string fromPwd)
{
FromEmail = fromEmail;
FromPwd = fromPwd;
}
/// <summary>
/// 发送一个人
/// </summary>
/// <param name="toAddress"></param>
/// <param name="subject"></param>
/// <param name="text"></param>
/// <param name="path"></param>
public void SendMail(string toAddress, string subject, string text, string path = "", string html = "")
{
IEnumerable<MailboxAddress> mailboxes = new List<MailboxAddress>() {
2021-12-21 21:29:55 +08:00
new MailboxAddress(toAddress, toAddress)
2021-09-28 18:17:31 +08:00
};
SendMail(mailboxes, subject, text, path, html);
}
/// <summary>
/// 发送多个邮箱
/// </summary>
/// <param name="toAddress"></param>
/// <param name="subject"></param>
/// <param name="text"></param>
/// <param name="path"></param>
public void SendMail(string[] toAddress, string subject, string text, string path = "", string html = "")
{
IList<MailboxAddress> mailboxes = new List<MailboxAddress>() { };
foreach (var item in toAddress)
{
2021-12-21 21:29:55 +08:00
mailboxes.Add(new MailboxAddress(item, item));
2021-09-28 18:17:31 +08:00
}
SendMail(mailboxes, subject, text, path, html);
}
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="toAddress"></param>
2022-01-02 15:56:23 +08:00
/// <param name="subject">主题</param>
2021-09-28 18:17:31 +08:00
/// <param name="text"></param>
/// <param name="path">附件url地址</param>
2022-01-02 15:56:23 +08:00
/// <param name="html">网页HTML内容</param>
2021-09-28 18:17:31 +08:00
private void SendMail(IEnumerable<MailboxAddress> toAddress, string subject, string text, string path = "", string html = "")
{
MimeMessage message = new MimeMessage();
//发件人
message.From.Add(new MailboxAddress(FromEmail, FromEmail));
//收件人
message.To.AddRange(toAddress);
message.Subject = subject;
2021-12-21 21:29:55 +08:00
//message.Date = DateTime.Now;
2021-09-28 18:17:31 +08:00
//创建附件Multipart
Multipart multipart = new Multipart("mixed");
2021-12-21 21:29:55 +08:00
var alternative = new MultipartAlternative();
2021-09-28 18:17:31 +08:00
//html内容
if (!string.IsNullOrEmpty(html))
{
var Html = new TextPart(TextFormat.Html)
{
Text = html
};
2021-12-21 21:29:55 +08:00
alternative.Add(Html);
2021-09-28 18:17:31 +08:00
}
//文本内容
if (!string.IsNullOrEmpty(text))
{
var plain = new TextPart(TextFormat.Plain)
{
Text = text + "\r\n\n\n" + mailSign
};
2021-12-21 21:29:55 +08:00
alternative.Add(plain);
2021-09-28 18:17:31 +08:00
}
//附件
if (!string.IsNullOrEmpty(path))
{
2022-01-02 15:56:23 +08:00
string[] files = path.Split(",");
foreach (var file in files)
2021-09-28 18:17:31 +08:00
{
2022-01-02 15:56:23 +08:00
MimePart attachment = new()
{
Content = new MimeContent(File.OpenRead(file), ContentEncoding.Default),
//读取文件,只能用绝对路径
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
ContentTransferEncoding = ContentEncoding.Base64,
//文件名字
FileName = Path.GetFileName(path)
};
alternative.Add(attachment);
}
2021-09-28 18:17:31 +08:00
}
2021-12-21 21:29:55 +08:00
multipart.Add(alternative);
2021-09-28 18:17:31 +08:00
//赋值邮件内容
message.Body = multipart;
2022-01-02 15:56:23 +08:00
2021-09-28 18:17:31 +08:00
//开始发送
2022-01-02 15:56:23 +08:00
using var client = new SmtpClient();
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
2021-09-28 18:17:31 +08:00
2022-01-02 15:56:23 +08:00
//Smtp服务器
//client.Connect("smtp.qq.com", 587, false);
client.Connect(Smtp, Port, true);
//登录,发送
//特别说明对于服务器端的中文相应Exception中有编码问题显示乱码了
client.Authenticate(FromEmail, FromPwd);
2021-09-28 18:17:31 +08:00
2022-01-02 15:56:23 +08:00
client.Send(message);
//断开
client.Disconnect(true);
Console.WriteLine($"发送邮件成功{DateTime.Now}");
}
2021-09-28 18:17:31 +08:00
}
}