Send Email Using API in .NET Core With Postman

 In this article, we will learn how to send an email in ASP.Net Core Web API with Postman.
Create the ASP.Net Core Web API Project
First, you need to create the ASP.Net core web project using the ASP.Net core web API.
Install MailKit & Mimekit Nuget Packages in project
In Menubar go to following order :
Tools>>Nuget Package Manager >> Manage Nuget Pakages for Solution
It will open follwing window. Add both MimeKit & MailKit packages in your project.
Access the application.json file
To access the appsettings.json file we need to create a configuration folder inside our ASP.NET Core Web API project. And create a class with name as EmailSetting.cs.

namespace EmailApi.Models
{
    public class EmailSetting
    {
        public string EmailId { get; set; }
        public string Name { get; set; }
        public string Password { get; set; }
        public string Host { get; set; }
        public int Port { get; set; }
        public bool UseSSL { get; set; }
    }
}
After creating Emailsetting .cs file , Add SMPT server & Email Account Detail in appsettings.json file as following:

"EmailSetting": {
    "EMailId": "your email address..",
    "Name": "name...",
    "Password": "password...",
    "Host": "smtp.gmail.com",
    "Port": 587
  },
We have data in application.json file.To access this data add instance of Emaillsetting in startup file. Open the startup file and add  following line in ConfigureServices method.

services.Configure<EmailSetting>(Configuration.GetSection(“EmailSetting”));

Now create another class named EmailData.cs

namespace EmailApi.Model
{
    public class EmailData
    {
        public string EmailToId { get; set; }
        public string EmailToName { get; set; }
        public string EmailSubject { get; set; }
        public string EmailBody { get; set; }
    }
}
Now,we will need to allow your Gmail account to send emails using external (less- secure applications). To enable Less Secure App Access, go to https://myaccount.google.com/security and turn ON Less Secure App Access.
Create Interface that responsible to send email.
Let’s create interface inside services folder with following method.

namespace EmailApi.Services
{
    public interface IEmailService
    {
        string SendEmail(EmailData emailData);
    }
}
Now create class that implement interface.
Create another class in services folder with name EmailServices.cs to implement interface as following.

using EmailApi.Model;
using EmailApi.Models;
using MailKit.Net.Smtp;
using Microsoft.Extensions.Options;
using MimeKit;
using System;
namespace EmailApi.Services
{
    public class EmailService : IEmailService
    {
        EmailSetting _emailSettings = null;
       
        public EmailService(IOptions<EmailSetting> options)
        {
            _emailSettings = options.Value;
        }
        public string SendEmail(EmailData emailData)
        {
            try
            {
                MimeMessage emailMessage = new MimeMessage();
                MailboxAddress emailFrom = new MailboxAddress(_emailSettings.Name, _emailSettings.EmailId);
                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(_emailSettings.Host, _emailSettings.Port, _emailSettings.UseSSL);
                emailClient.Authenticate(_emailSettings.EmailId, _emailSettings.Password);
                emailClient.Send(emailMessage);
                emailClient.Disconnect(true);
                emailClient.Dispose();
                return "Email Send Successfully....";
            }
            catch (Exception)
            {
                return "Email Not Sent...";
            }
        }
    }
}
Add the Controller in the project
Create the Email Controller in which you define the endpoint. Send Email is responsible for sending the email.

namespace EmailApi.Controllers
{
    [ApiController]
    public class EmailController : ControllerBase
    {
        IEmailService _emailService = null;
        public EmployeeDbContext context;
        public EmailController(IEmailService emailService)
        {
            _emailService = emailService;
        }
        [Route("api/Email/SendEmail")]
        [HttpPost]
        public string SendEmail(EmailData emailData)
        {
            return _emailService.SendEmail(emailData);
        }
    }
}
After creating controller run your project & open postman.

select POST method to send mail & add Email information in body part as above.Click send button to send email.You will get the following

output:

Post a Comment

0 Comments