我使用AWS网页上的示例来检查我是否可以使用AWS SES SDK接收电子邮件。
它显示电子邮件已成功发送,但在我查看我的电子邮件帐户时未收到任何电子邮件。
发件人的电子邮件已经过验证。
代码与给定的简单相同。只有电子邮件地址不同。
当我在VS 2017中粘贴代码时,Client.SendEmail()会引发错误。
我将其修改为推荐给Client.SendEmailAsync()。不知道问题云在哪里。
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();
}
}
}
Main
方法在发送电子邮件之前结束,因为我们不等待Task response
完成。
SendEmailAsync
返回Task
,意思是这两行代码是相同的:
var response = client.SendEmailAsync(sendRequest);
Task response = client.SendEmailAsync(sendRequest);
如果您安装了最新版本的Visual Studio并启用了C#7.1,则可以利用async Task Main
并使用await
关键字,该关键字将告诉代码在不同的线程上运行SendEmailAsync
,Main
将不会结束,直到SendEmail
完成。
如果您使用的是较旧版本的Visual Studio,则可以添加.GetAwaiter().GetResult()
,这也将确保Main
在SendEmail
完成之前不会结束,但SendEmailAsync
将锁定当前线程。
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();
}