优化发送邮件
This commit is contained in:
@@ -30,7 +30,7 @@ namespace ZR.Common
|
||||
/// 是否使用SSL协议
|
||||
/// </summary>
|
||||
public bool UseSsl { get; set; } = false;
|
||||
public string mailSign = @"";
|
||||
public string mailSign = @"邮件来自C# 程序发送";
|
||||
private readonly MailOptions mailOptions = new();
|
||||
|
||||
public MailHelper()
|
||||
@@ -93,9 +93,10 @@ namespace ZR.Common
|
||||
/// 发送邮件
|
||||
/// </summary>
|
||||
/// <param name="toAddress"></param>
|
||||
/// <param name="subject"></param>
|
||||
/// <param name="subject">主题</param>
|
||||
/// <param name="text"></param>
|
||||
/// <param name="path">附件url地址</param>
|
||||
/// <param name="html">网页HTML内容</param>
|
||||
private void SendMail(IEnumerable<MailboxAddress> toAddress, string subject, string text, string path = "", string html = "")
|
||||
{
|
||||
MimeMessage message = new MimeMessage();
|
||||
@@ -131,39 +132,40 @@ namespace ZR.Common
|
||||
//附件
|
||||
if (!string.IsNullOrEmpty(path))
|
||||
{
|
||||
MimePart attachment = new MimePart()
|
||||
string[] files = path.Split(",");
|
||||
foreach (var file in files)
|
||||
{
|
||||
Content = new MimeContent(File.OpenRead(path), ContentEncoding.Default),
|
||||
//读取文件,只能用绝对路径
|
||||
ContentDisposition = new ContentDisposition(ContentDisposition.Attachment),
|
||||
ContentTransferEncoding = ContentEncoding.Base64,
|
||||
//文件名字
|
||||
FileName = Path.GetFileName(path)
|
||||
};
|
||||
alternative.Add(attachment);
|
||||
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);
|
||||
}
|
||||
}
|
||||
multipart.Add(alternative);
|
||||
//赋值邮件内容
|
||||
message.Body = multipart;
|
||||
|
||||
|
||||
//开始发送
|
||||
using (var client = new SmtpClient())
|
||||
{
|
||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||
|
||||
//Smtp服务器
|
||||
//client.Connect("smtp.qq.com", 587, false);
|
||||
client.Connect(Smtp, Port, true);
|
||||
//登录,发送
|
||||
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
||||
client.Authenticate(FromEmail, FromPwd);
|
||||
using var client = new SmtpClient();
|
||||
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
|
||||
|
||||
client.Send(message);
|
||||
//断开
|
||||
client.Disconnect(true);
|
||||
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
||||
}
|
||||
//Smtp服务器
|
||||
//client.Connect("smtp.qq.com", 587, false);
|
||||
client.Connect(Smtp, Port, true);
|
||||
//登录,发送
|
||||
//特别说明,对于服务器端的中文相应,Exception中有编码问题,显示乱码了
|
||||
client.Authenticate(FromEmail, FromPwd);
|
||||
|
||||
client.Send(message);
|
||||
//断开
|
||||
client.Disconnect(true);
|
||||
Console.WriteLine($"发送邮件成功{DateTime.Now}");
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user