在ASP.net中使用MimeKit,发送邮件

 public String GetAccountActivationCode()
        {
            int n = 6;
            StringBuilder code = new StringBuilder();
            Random ran = new Random();
            for (int i = 0; i < n; i++)
            {
                code.Append(ran.Next(9).ToString());
            }
            return code.ToString();
        }


   public String SendEmail()

        {
            string mailTo = "xxx@qq.com";
            var message = new MimeMessage(); //设置消息对象
            string mailFromAccount = "XXXX@qq.com";
            string mailPassword = "tprnifnjzlhgcaib";  //这是在邮箱中开通smtp服务后的密码,在开通smtp服务的时候会发给你
            string mailFrom = "XXXX@qq.com";
           
            //添加from and to的消息地址列表和添加的地址
            message.From.Add(new MailboxAddress("gg", mailFrom));
            message.To.Add(new MailboxAddress("xx", mailTo ));   //前边的名字随便写,
            //设置消息主题
            message.Subject = "Account Activation Code:";
            //生成6位的验证码
            string code = GetAccountActivationCode();


            //创建消息的文本
            message.Body = new TextPart("plain")
            {
                Text = @code
            };


            using (var client = new MailKit.Net.Smtp.SmtpClient())
            {
                client.ServerCertificateValidationCallback = (s, c, h, e) => true;
                client.Connect("smtp.qq.com", 587, false); //前边是smtp的服务器,端口号
                client.AuthenticationMechanisms.Remove("XOAUTH2");
                client.Authenticate(mailFromAccount, mailPassword);//认证发送者
                client.Send(message);//发消息
                client.Disconnect(true);


            }
            return code;
        }
要在 ASP.NET Core 中使用 MailKit 发送电子邮件,需要执行以下步骤: 1. 安装 MailKit 和 MimeKit NuGet 包。 2. 在 Startup.cs 文件中添加以下代码: ```csharp using MailKit.Net.Smtp; using MimeKit; public void ConfigureServices(IServiceCollection services) { // ... services.AddMailKit(optionBuilder => { optionBuilder.UseSmtp("smtp.gmail.com", 587); optionBuilder.EnableSsl = true; optionBuilder.AuthenticationOptions = AuthenticationOptions.DoNotAuthenticate; }); // ... } ``` 3. 在控制器或服务中注入 `IMailer` 接口。 ```csharp using MailKit.Net.Smtp; using MimeKit; public class MyController : Controller { private readonly IMailer _mailer; public MyController(IMailer mailer) { _mailer = mailer; } public async Task<IActionResult> SendEmail() { var message = new MimeMessage(); message.From.Add(new MailboxAddress("From Name", "from@example.com")); message.To.Add(new MailboxAddress("To Name", "to@example.com")); message.Subject = "Test Email"; message.Body = new TextPart("plain") { Text = "This is a test email." }; await _mailer.SendAsync(message); return Ok(); } } ``` 4. 通过 MailKit 发送电子邮件: ```csharp using MailKit.Net.Smtp; using MimeKit; public interface IMailer { Task SendAsync(MimeMessage message); } public class Mailer : IMailer { private readonly SmtpClient _smtpClient; public Mailer(SmtpClient smtpClient) { _smtpClient = smtpClient; } public async Task SendAsync(MimeMessage message) { await _smtpClient.SendAsync(message); } } ``` 现在,当执行 `SendEmail()` 方法时,将通过 MailKit 发送电子邮件
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

野狼位位

给点辛苦费0.1元

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值