Send Email From Amazon SES in ASP.NET MVC App: A Guide

Send Email From Amazon SES in ASP.NET MVC App: A Guide
Keywords: Amazon SES, ASP.NET MVC, Email Sending, AWS SDK, C#, .NET Core
As a data scientist or software engineer, you might have come across the need to send emails from your application. Today, we’ll discuss how to send email from an Amazon Simple Email Service (SES) in an ASP.NET MVC app.
What is Amazon SES?
Amazon Simple Email Service (SES) is a cloud-based email sending service designed to help digital marketers and application developers send marketing, notification, and transactional emails. It’s a reliable, cost-effective service for businesses of all sizes that use email to keep in contact with their customers.
Prerequisites
- Amazon Web Services (AWS) Account
- Verified Amazon SES Account
- .NET Core 3.1 or above
- Visual Studio 2019 or later
Step 1: Setting up Amazon SES
Before you can send an email through Amazon SES, you have to verify that you own the ‘From’ address. Log into your AWS Management Console, navigate to SES, select ‘Email Addresses’ under ‘Identity Management’, and click ‘Verify a New Email Address’. Enter the email address you want to verify and click ‘Verify This Email Address’.
Step 2: Install the AWS SDK
The next step is to install the AWS SDK in our ASP.NET MVC application. Open your project in Visual Studio, then go to ‘Tools -> NuGet Package Manager -> Package Manager Console’. In the console, type the following command to install the AWS SDK for .NET:
Install-Package AWSSDK.SimpleEmail
Step 3: Configuration
First, you need to set up your AWS credentials. We’ll add them in the appsettings.json
file like so:
"AWS": {
"AccessKey": "your-access-key",
"SecretKey": "your-secret-key",
"Region": "us-east-1"
}
Replace ‘your-access-key’ and ‘your-secret-key’ with your actual AWS access key and secret key. The ‘Region’ defines where your Amazon SES is located.
Step 4: Creating the SendEmail Function
In your controller, import the Amazon.SimpleEmail
and Amazon.SimpleEmail.Model
namespaces, then create the SendEmail
function:
private async Task SendEmail(string toAddress, string subject, string body)
{
using (var client = new AmazonSimpleEmailServiceClient(_accessKey, _secretKey, Amazon.RegionEndpoint.USEast1))
{
var sendRequest = new SendEmailRequest
{
Source = _fromAddress,
Destination = new Destination { ToAddresses = new List<string> { toAddress } },
Message = new Message
{
Subject = new Content(subject),
Body = new Body { Text = new Content(body) }
}
};
try
{
var response = await client.SendEmailAsync(sendRequest);
ViewBag.Message = "Email sent!";
}
catch (Exception ex)
{
ViewBag.Message = "The email was not sent.";
ViewBag.ErrorMessage = "Error message: " + ex.Message;
}
}
}
This function creates a new instance of AmazonSimpleEmailServiceClient
using the AWS credentials. It then creates an email request and sends the email using the SendEmailAsync
method.
To use the SendEmail
function, just call it in your controller action:
public async Task<ActionResult> SendEmail()
{
await SendEmail("receiver@example.com", "Hello from Amazon SES!", "This is a test email.");
return View();
}
Conclusion
Amazon SES is a powerful, flexible, and reliable email sending service for developers. By integrating it into your ASP.NET MVC applications, you can send email notifications, transactional emails, or marketing messages directly from your app.
Remember to keep your AWS credentials secure and to follow best practices when working with AWS services. Happy coding!
Please note that all the code and instructions provided in this blog post are for educational purposes only. Always test thoroughly before implementing in a production environment.
About Saturn Cloud
Saturn Cloud is your all-in-one solution for data science & ML development, deployment, and data pipelines in the cloud. Spin up a notebook with 4TB of RAM, add a GPU, connect to a distributed cluster of workers, and more. Join today and get 150 hours of free compute per month.