我使用 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);任务响应 = client.SendEmailAsync(sendRequest);
解决方案
如果您安装了最新版本的 Visual Studio 并启用了 C#7.1,则可以利用 async Task Main 并使用 await 关键字在不同线程和 Main 上运行 SendEmailAsync 的代码在 SendEmail 完成之前不会结束。
如果您使用的是旧版本的 Visual Studio,您可以添加 .GetAwaiter().GetResult(),这也将确保 Main 直到SendEmail 已完成,但 SendEmailAsync 将锁定当前线程。
C#7.1及以上(首选)
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();
}
C#7.0及以下
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();
}
