You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

92 lines
3.2 KiB
C#

12 months ago
using MailKit.Net.Smtp;
12 months ago
using Microsoft.Extensions.Options;
12 months ago
using MimeKit;
12 months ago
using NETCore.MailKit.Core;
using NETCore.MailKit.Infrastructure.Internal;
namespace Ds.Module.Mailkit
{
internal class DsSendEmail
{
private readonly EmailOptions options;
private readonly IEmailService _EmailService;
public DsSendEmail(IOptions<EmailOptions> options, IEmailService emailService)
{
this.options = options.Value;
_EmailService = emailService;
}
public async void SendEmail(string entity)
{
//设置发送人的邮件地址和名称,在接收人接收到邮件提示时会显示该信息
var sendInfo = new SenderInfo
{
SenderEmail = options.FromAddress,
SenderName = "投诉",
};
await _EmailService.SendAsync(options.ToAddress, "投诉", "邮件内容", false, sender: sendInfo);
}
12 months ago
/// <summary>
/// 发送邮件
/// </summary>
/// <param name="dicToEmail"></param>
/// <param name="title"></param>
/// <param name="content"></param>
/// <param name="name"></param>
/// <param name="fromEmail"></param>
/// <returns></returns>
public static bool _SendEmail(
Dictionary<string, string> dicToEmail,
string title, string content,
string name = "", string fromEmail = "",
string host = "smtp.qq.com", int port = 587,
string userName = "", string userPwd = "123123")
{
var isOk = false;
try
{
if (string.IsNullOrWhiteSpace(title) || string.IsNullOrWhiteSpace(content)) { return isOk; }
//设置基本信息
var message = new MimeMessage();
message.From.Add(new MailboxAddress(name, fromEmail));
foreach (var item in dicToEmail.Keys)
{
message.To.Add(new MailboxAddress(item, dicToEmail[item]));
}
message.Subject = title;
message.Body = new TextPart("html")
{
Text = content
};
//链接发送
using (var client = new SmtpClient())
{
// For demo-purposes, accept all SSL certificates (in case the server supports STARTTLS)
client.ServerCertificateValidationCallback = (s, c, h, e) => true;
//采用qq邮箱服务器发送邮件
client.Connect(host, port, false);
// Note: since we don't have an OAuth2 token, disable
// the XOAUTH2 authentication mechanism.
client.AuthenticationMechanisms.Remove("XOAUTH2");
//qq邮箱密码(安全设置短信获取后的密码) ufiaszkkulbabejh
client.Authenticate(userName, userPwd);
client.Send(message);
client.Disconnect(true);
}
isOk = true;
}
catch (Exception ex)
{
}
return isOk;
}
12 months ago
}
}