我使用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();
}
}
}
答案 0 :(得分:3)
Main
方法在发送电子邮件之前结束,因为我们不等待Task response
完成。
SendEmailAsync
返回Task
,这意味着这两行代码相同:
var response = client.SendEmailAsync(sendRequest);
Task response = client.SendEmailAsync(sendRequest);
如果您安装了最新版本的Visual Studio并启用了C#7.1,则可以利用async Task Main
并使用await
关键字来指示代码运行{{1}在另一个帖子上SendEmailAsync
赢了,直到Main
完成。
如果您使用的是较旧版本的Visual Studio,则可以添加SendEmail
,这也将确保.GetAwaiter().GetResult()
在Main
完成之前不会结束,但SendEmail
1}}将锁定当前线程。
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();
}