How to add mail sending in webapi application
To add mail sending functionality in a WebAPI application, you can use the System.Net.Mail
namespace in.NET. Here's a step-by-step guide:
Step 1: Install the necessary NuGet package
In your WebAPI project, right-click on the project in Visual Studio, select "Manage NuGet Packages", and search for "System.Net.Mail". Install the package.
Step 2: Create a mail settings class
Create a new class to hold your mail settings, e.g., MailSettings.cs
:
public class MailSettings
{
public string SmtpServer { get; set; }
public int SmtpPort { get; set; }
public string SmtpUsername { get; set; }
public string SmtpPassword { get; set; }
public string FromEmail { get; set; }
public string FromName { get; set; }
}
Step 3: Configure mail settings
In your WebAPI project, create a new instance of the MailSettings
class and configure the mail settings. For example, you can add the following code to your WebApiConfig.cs
file:
public static class MailSettings
{
public static MailSettings Instance { get; } = new MailSettings
{
SmtpServer = "your-smtp-server.com",
SmtpPort = 587,
SmtpUsername = "your-smtp-username",
SmtpPassword = "your-smtp-password",
FromEmail = "[email protected]",
FromName = "Your Name"
};
}
Step 4: Create a mail service class
Create a new class to handle mail sending, e.g., MailService.cs
:
public class MailService
{
private readonly MailSettings _mailSettings;
public MailService(MailSettings mailSettings)
{
_mailSettings = mailSettings;
}
public void SendMail(string toEmail, string subject, string body)
{
using (var mailMessage = new MailMessage())
{
mailMessage.From = new MailAddress(_mailSettings.FromEmail, _mailSettings.FromName);
mailMessage.To.Add(toEmail);
mailMessage.Subject = subject;
mailMessage.Body = body;
using (var smtpClient = new SmtpClient())
{
smtpClient.Host = _mailSettings.SmtpServer;
smtpClient.Port = _mailSettings.SmtpPort;
smtpClient.EnableSsl = true;
smtpClient.Credentials = new NetworkCredential(_mailSettings.SmtpUsername, _mailSettings.SmtpPassword);
smtpClient.Send(mailMessage);
}
}
}
}
Step 5: Inject the mail service into your WebAPI controllers
In your WebAPI controllers, inject the MailService
instance using dependency injection. For example, in your ValuesController.cs
file:
public class ValuesController : ApiController
{
private readonly MailService _mailService;
public ValuesController(MailService mailService)
{
_mailService = mailService;
}
[HttpPost]
public IHttpActionResult SendMail([FromBody]MailRequest mailRequest)
{
_mailService.SendMail(mailRequest.ToEmail, mailRequest.Subject, mailRequest.Body);
return Ok();
}
}
public class MailRequest
{
public string ToEmail { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
Step 6: Test the mail sending functionality
Create a new instance of the ValuesController
and call the SendMail
action to test the mail sending functionality.
That's it! You should now have a basic mail sending functionality in your WebAPI application.