Reputation: 39
I used the sample from AWS webpage to check whether I can receive an email by using AWS SES SDK.
It shows the email has been sent successfully, but no email received when I check my email account.
The sender's email has been verified.
The code is the same as the given simple. Only the email addresses are different.
When I paste the code in VS 2017, an error is thrown for Client.SendEmail().
I modified it as recommending to Client.SendEmailAsync(). Have no idea where the issue cloud be.
using Amazon;
using System;
using System.Collections.Generic;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
namespace AmazonSESSample
{
class Program
{
static readonly string senderAddress = "*****";
static readonly string receiverAddress = "******";
static readonly string configSet = "ConfigSet";
static readonly string subject = "Amazon SES test (AWS SDK for .NET)";
static readonly string textBody = "Amazon SES Test (.NET)\r\n"
+ "This email was sent through Amazon SES "
+ "using the AWS SDK for .NET.";
static readonly string htmlBody = @"<html>
<head></head>
<body>
<h1>Amazon SES Test (AWS SDK for .NET)</h1>
<p>This email was sent with
<a href='https://aws.amazon.com/ses/'>Amazon SES</a> using the
<a href='https://aws.amazon.com/sdk-for-net/'>
AWS SDK for .NET</a>.</p>
</body>
</html>";
static void Main(string[] args)
{
using (
var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1))
{
var sendRequest = new SendEmailRequest
{
Source = senderAddress,
Destination = new Destination
{
ToAddresses =
new List<string> { receiverAddress }
},
Message = new Message
{
Subject = new Content(subject),
Body = new Body
{
Html = new Content
{
Charset = "UTF-8",
Data = htmlBody
},
Text = new Content
{
Charset = "UTF-8",
Data = textBody
}
}
},
ConfigurationSetName = configSet
};
try
{
Console.WriteLine("Sending email using Amazon SES...");
var response = client.SendEmailAsync(sendRequest);
Console.WriteLine("The email was sent successfully.");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
}
Console.Write("Press any key to continue...");
Console.ReadKey();
}
}
}
Upvotes: 1
Views: 1121
Reputation: 15340
The Main
method is ending before the email is sent because we are not waiting for Task response
to finish.
SendEmailAsync
returns a Task
, meaning these two lines of code are identical:
var response = client.SendEmailAsync(sendRequest);
Task response = client.SendEmailAsync(sendRequest);
If you have the latest version of Visual Studio installed and have C#7.1 enabled, you can take advantage of async Task Main
and use the await
keyword which will tell the code to run SendEmailAsync
on a different thread and Main
won't end until SendEmail
has completed.
If you are using an older version of Visual Studio, you can add .GetAwaiter().GetResult()
, which will also ensure Main
won't end until SendEmail
has completed, but SendEmailAsync
will lock the current thread.
static async Task Main(string[] args)
{
using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1))
{
var sendRequest = new SendEmailRequest
{
Source = senderAddress,
Destination = new Destination
{
ToAddresses =
new List<string> { receiverAddress }
},
Message = new Message
{
Subject = new Content(subject),
Body = new Body
{
Html = new Content
{
Charset = "UTF-8",
Data = htmlBody
},
Text = new Content
{
Charset = "UTF-8",
Data = textBody
}
}
},
ConfigurationSetName = configSet
};
try
{
Console.WriteLine("Sending email using Amazon SES...");
var response = await client.SendEmailAsync(sendRequest);
Console.WriteLine("The email was sent successfully.");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
}
Console.Write("Press any key to continue...");
Console.ReadKey();
}
static void Main(string[] args)
{
using (var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USEast1))
{
var sendRequest = new SendEmailRequest
{
Source = senderAddress,
Destination = new Destination
{
ToAddresses =
new List<string> { receiverAddress }
},
Message = new Message
{
Subject = new Content(subject),
Body = new Body
{
Html = new Content
{
Charset = "UTF-8",
Data = htmlBody
},
Text = new Content
{
Charset = "UTF-8",
Data = textBody
}
}
},
ConfigurationSetName = configSet
};
try
{
Console.WriteLine("Sending email using Amazon SES...");
var response = client.SendEmailAsync(sendRequest).GetAwaiter().GetResult();
Console.WriteLine("The email was sent successfully.");
}
catch (Exception ex)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + ex.Message);
}
}
Console.Write("Press any key to continue...");
Console.ReadKey();
}
Upvotes: 3