Send Email Messages via MailGun and SendGrid Delivery Services
Aspose.Email provides the ability to send email messages using MailGun or SendGrid services. Its unified API allows you to initialize a client, prepare and send the email message.
First, it’s important to set up options depending on which service is going to be used for sending messages. DeliveryServiceOptions class will help you with that. The following code sample shows how to set up options for the services.
MailGun client options:
string domain = "YOUR_MAILGUN_DOMEN";
string privApiKey = "YOUR_MAILGUN_PRIVATE_API_KEY";
var opt = new MailgunClientOptions { Domain = domain, ApiKey = privApiKey };
SendGrid client options:
string privApiKey = "YOUR_SENDGRID_PRIVATE_API_KEY";
var opt = new SendGridClientOptions { ApiKey = privApiKey };
To prepare and send a message, use the following code sample and steps:
- Create an instance of the IDeliveryServiceClient interface.
- Create a new eml message using the MailMessage class. Specify its properties.
- Use the Send method of the client object to send the email. The result of the send operation is stored in the resp variable.
- If the sending operation was not successful (resp.Successful is false), the code enters a foreach loop to iterate over the ErrorMessages collection of the resp object. Each error message is printed to the console using Console.WriteLine.
IDeliveryServiceClient client = DeliveryServiceClientFactory.Get(opt);
MailMessage eml = new MailMessage(fromAddress, toAddress, subject, body);
var resp = client.Send(eml);
if (!resp.Successful)
{
foreach (var error in resp.ErrorMessages)
{
Console.WriteLine(error);
}
}
Send Messages Asynchronously
MailMessage eml = new MailMessage(fromAddress, toAddress, subject, body);
var sendTask = client.SendAsync(eml);
sendTask.Wait();
if (!sendTask.Result.Successful)
{
foreach (var error in sendTask.Result.ErrorMessages)
{
Console.WriteLine(error);
}
}
Set a Custom Base URL for SendGrid
Configure the base URL for the SendGrid client using the SendGridRegion enumeration and properties in the SendGridClientOptions class. This feature allows developers to specify the desired SendGrid region, such as the EU or default global region, ensuring optimal performance and compliance with regional requirements. The following code sample demonstrates how to configure the SendGridClientOptions to use the SendGrid EU region. Adjusting the base URL helps align with data residency and latency preferences.
SendGridClientOptions opt = new SendGridClientOptions()
{
ApiKey = "YourApiKey",
Region = SendGridRegion.EU
};
IDeliveryServiceClient client = DeliveryServiceClientFactory.Get(opt);
client.EndPoint = "https://api.eu.sendgrid.com";