ASP.Net core 使用mailkit发送邮件

首先安装mailkit
然后 控制器

using Microsoft.AspNetCore.Mvc;
using MimeKit;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Threading.Tasks;
//SmtpClient smtpClient = System.Net.Mail.SmtpClient;
namespace emil.Controllers
{
    public class Get : Controller
    {
        public void xx() {

            //收件人邮箱
            string mailName = "22580557@qq.com";
            //发送的标题
            string title = "测试数据";
            //发送的内容
            string bobyText = "吃鸡丁拌面!";
            // 邮件服务器smtp.qq.com表示qq邮箱服务器    
            string host = "smtp.qq.com";
            // 发送端账号   
            string userName = "5068449@qq.com";
            // 发送端授权码,需要在邮箱获取授权码
            string pwd = "vtwssafdobjab";
            MimeMessage message = new MimeMessage();
            //发件人
            message.From.Add(new MailboxAddress("T.Shiller", userName));
            //收件人
            message.To.Add(new MailboxAddress(title, mailName));
            //   message.To.Add(new MailboxAddress(title,mailName ));
           
            //标题
            message.Subject = title;
            //正文内容,发送
            message.Body = new BodyBuilder
            {
                HtmlBody = bobyText
            }.ToMessageBody();
            try
            {
                using (var client = new MailKit.Net.Smtp.SmtpClient())
                {
                    //Smtp服务器
                    client.Connect(host, 587, false);
                    //登录,发送
                    client.Authenticate(userName, pwd);
                    client.Send(message);
                    //断开
                    client.Disconnect(true);
                }

            }
            catch (Exception)
            {
                throw;
            }


        }
    }
}
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
要在 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 发送电子邮件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值