Hi @Coreysan ,
I don't recommend that you use the SmtpClient class for new development because SmtpClient doesn't support many modern protocols. Try to use MailKit, SendGrid or other libraries instead.
Here is a sample about using MailKit to send email, you can refer to it.
- Create an Asp.net 3.1 MVC application, and install the MailKit package (you can also try to use the NETCore.Mailkit package, I checked it on Asp.net 6 application, it also works well) via the Nuget Package Manager.
- Create a MailRequest model. Then we can according to it to create a page and let use enter the Email address and Email content.
public class MailRequest { public string ToEmail { get; set; } public string Subject { get; set; } public string Body { get; set; } public List<IFormFile> Attachments { get; set; } }
- Create a MailSettings model. We can use it define mail settings.
public class MailSettings { public string Mail { get; set; } public string DisplayName { get; set; } public string Password { get; set; } public string Host { get; set; } public int Port { get; set; } }
- Configure the Mail Setting in AppSetting.Json, since I will send the email using my Hotmail account, the
SMTP Host
will be "smtp.office365.com", and the port is587
, you can change them based on your email account. Refer to POP, IMAP, and SMTP settings"MailSettings": { "Mail": "yourEmail@......COM", "DisplayName": "Your Brand Name", "Password": "YourPassword", "Host": "SMTP Host", "Port": 587 },
- Create a IMailService and MailService, and add the SendMail methods:
//using Microsoft.Extensions.Options; //using MimeKit; //using MailKit.Security; //using MvcApp3_1.Models; //using MailKit.Net.Smtp; //using System.IO; //using System.Threading.Tasks; public interface IMailService { Task SendEmailAsync(MailRequest mailRequest); } public class MailService : IMailService { private readonly MailSettings _mailSettings; public MailService(IOptions<MailSettings> mailSettings) { _mailSettings = mailSettings.Value; } public async Task SendEmailAsync(MailRequest mailRequest) { var email = new MimeMessage(); email.Sender = MailboxAddress.Parse(_mailSettings.Mail); email.To.Add(MailboxAddress.Parse(mailRequest.ToEmail)); email.Subject = mailRequest.Subject; var builder = new BodyBuilder(); if (mailRequest.Attachments != null) { byte[] fileBytes; foreach (var file in mailRequest.Attachments) { if (file.Length > 0) { using (var ms = new MemoryStream()) { file.CopyTo(ms); fileBytes = ms.ToArray(); } builder.Attachments.Add(file.FileName, fileBytes, ContentType.Parse(file.ContentType)); } } } builder.HtmlBody = mailRequest.Body; email.Body = builder.ToMessageBody(); using var smtp = new SmtpClient(); smtp.Connect(_mailSettings.Host, _mailSettings.Port, SecureSocketOptions.StartTls); smtp.Authenticate(_mailSettings.Mail, _mailSettings.Password); await smtp.SendAsync(email); smtp.Disconnect(true); } }
- In the Startup.cs or Program.cs file, register the MailService:
services.Configure<MailSettings>(Configuration.GetSection("MailSettings")); services.AddScoped<MvcApp3_1.Services.IMailService, MvcApp3_1.Services.MailService>();
- In the Controller, create the endpoint to send mail:
public class HomeController : Controller { private readonly ILogger<HomeController> _logger; private readonly IMailService _mailService; public HomeController(ILogger<HomeController> logger, IMailService mailService) { _logger = logger; _mailService = mailService; } public IActionResult SendMail() { return View(); } [HttpPost("SendMail")] public async Task<IActionResult> SendMail(MailRequest request) { try { await _mailService.SendEmailAsync(request); return Ok(); } catch (Exception ex) { throw ex; } }
- The SendMail view page like this:
After that, running the application, in the SendMail page, we can enter the email path and content.
After clicking the Create button, we can receive the email like this:
If you want to send mail using SendGrid, you can refer to the following tutorials:
.NET Core App Sending An Email Using SendGrid
Asp.Net Core create a controller to send emails using SendGrid
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
Best regards,
Dillion