How to send a Smtp Email in C#/Dotnet.
Hello there,
In this blog, we will see the topic mentioned in the title...
For the mail process, we will use some of the main packages they are...
using Microsoft.AspNetCore.Mvc; using MimeKit; using MailKit.Net.Smtp; using Microsoft.Extensions.Options; using Self.Models;
After Imports, we will start creating and class with namespace as below...
namespace Self.Controllers { [Route("api/[controller]")] [ApiController] public class NotifyController: IEmailService { //Code Goes here... } public interface IEmailService { bool SendEmail(Mail emailData); } }
After creating a Controller class we will now create a method/function in the Class as below...
[HttpPost] [Route("Send_mail")] public bool SendEmail(Mail emailData) { try { MimeMessage emailMessage = new MimeMessage(); MailboxAddress emailFrom = new MailboxAddress(emailData.EmailFromName, "anymail@gmail.com"); emailMessage.From.Add(emailFrom); MailboxAddress emailTo = new MailboxAddress(emailData.EmailToName, emailData.EmailToId); emailMessage.To.Add(emailTo); emailMessage.Subject = emailData.EmailSubject; BodyBuilder emailBodyBuilder = new BodyBuilder(); emailBodyBuilder.TextBody = emailData.EmailBody; emailMessage.Body = emailBodyBuilder.ToMessageBody(); SmtpClient emailClient = new SmtpClient(); emailClient.Connect("smtp.gmail.com", 465, true); emailClient.Authenticate("anymail@gmail.com", "smtp_pwd"); emailClient.Send(emailMessage); emailClient.Disconnect(true); emailClient.Dispose(); return true; } catch (Exception ex) { //Log Exception Details return false; } }
In the above function, we have created a function but we had an input method called Mail in the above function, It's a modal called to store the data in it and get the data...
public class Mail { public string EmailFromName { get; set; } public string EmailToId { get; set; } public string EmailToName { get; set; } public string EmailSubject { get; set; } public string EmailBody { get; set; } }
The Total Email sending code is below...
using Microsoft.AspNetCore.Mvc; using MimeKit; using MailKit.Net.Smtp; using Microsoft.Extensions.Options; using Self.Models; namespace surakshaQR.Controllers { [Route("api/[controller]")] [ApiController] public class NotifyController : IEmailService { [HttpPost] [Route("Send_mail")] public bool SendEmail(Mail emailData) { try { MimeMessage emailMessage = new MimeMessage(); MailboxAddress emailFrom = new MailboxAddress(emailData.EmailFromName, "anymail@gmail.com"); emailMessage.From.Add(emailFrom); MailboxAddress emailTo = new MailboxAddress(emailData.EmailToName, emailData.EmailToId); emailMessage.To.Add(emailTo); emailMessage.Subject = emailData.EmailSubject; BodyBuilder emailBodyBuilder = new BodyBuilder(); emailBodyBuilder.TextBody = emailData.EmailBody; emailMessage.Body = emailBodyBuilder.ToMessageBody(); SmtpClient emailClient = new SmtpClient(); emailClient.Connect("smtp.gmail.com", 465, true); emailClient.Authenticate("anymail@gmail.com", "smtp_pwd"); emailClient.Send(emailMessage); emailClient.Disconnect(true); emailClient.Dispose(); return true; } catch (Exception ex) { //Log Exception Details return false; } } } public interface IEmailService { bool SendEmail(Mail emailData); } }
in any of the modal pages place the Mail modal function and import it into the controller page...
public class Mail { public string EmailFromName { get; set; } public string EmailToId { get; set; } public string EmailToName { get; set; } public string EmailSubject { get; set; } public string EmailBody { get; set; } }
If any issues or any doubts please have commented...
Thank you...