Asp.Net Core MailKit发送邮件
时间: 2024-05-14 15:18:28 浏览: 14
在 Asp.Net Core 中使用 MailKit 发送邮件需要以下步骤:
1. 安装 MailKit:在 Visual Studio 中,右键单击项目,选择“管理 NuGet 包”,搜索 MailKit 并安装。
2. 在 appsettings.json 文件中配置 SMTP 服务器信息:
```
"SmtpSettings": {
"Host": "smtp.example.com",
"Port": 587,
"Username": "your_username",
"Password": "your_password"
}
```
3. 创建一个邮件服务类,例如:
```
using MailKit.Net.Smtp;
using MailKit.Security;
using Microsoft.Extensions.Options;
using MimeKit;
using System.Threading.Tasks;
public class EmailService : IEmailService
{
private readonly SmtpSettings _smtpSettings;
public EmailService(IOptions<SmtpSettings> smtpSettings)
{
_smtpSettings = smtpSettings.Value;
}
public async Task SendEmailAsync(string toEmail, string subject, string message)
{
var email = new MimeMessage();
email.From.Add(new MailboxAddress(_smtpSettings.SenderName, _smtpSettings.SenderEmail));
email.To.Add(new MailboxAddress(toEmail));
email.Subject = subject;
var builder = new BodyBuilder();
builder.HtmlBody = message;
email.Body = builder.ToMessageBody();
using (var smtp = new SmtpClient())
{
await smtp.ConnectAsync(_smtpSettings.Host, _smtpSettings.Port, SecureSocketOptions.StartTls);
await smtp.AuthenticateAsync(_smtpSettings.Username, _smtpSettings.Password);
await smtp.SendAsync(email);
await smtp.DisconnectAsync(true);
}
}
}
```
4. 在 Startup.cs 文件的 ConfigureServices 方法中注册服务:
```
services.Configure<SmtpSettings>(Configuration.GetSection("SmtpSettings"));
services.AddTransient<IEmailService, EmailService>();
```
5. 在需要发送邮件的地方注入 IEmailService 并调用 SendEmailAsync 方法即可发送邮件。
```
public class HomeController : Controller
{
private readonly IEmailService _emailService;
public HomeController(IEmailService emailService)
{
_emailService = emailService;
}
public IActionResult Index()
{
return View();
}
public async Task<IActionResult> SendEmail(string email)
{
await _emailService.SendEmailAsync(email, "测试邮件", "这是一封测试邮件。");
return RedirectToAction("Index");
}
}
```
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)