我正在尝试使用 SDK 通过 AWS SES API 发送电子邮件。
我的代码基于此处的官方文档:https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-sdk.html https://docs.aws.amazon.com/ses/latest/DeveloperGuide/examples-send-using-sdk.html
我已经到了await client.SendEmailAsync(sendRequest);
并收到错误消息:
无法从 EC2 实例元数据服务检索凭证。
// initialization
var client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2);
var response = new SendEmailResponse();
// build email
var sendRequest = new SendEmailRequest
{
Source = ToAddress,
Destination = new Destination
{
ToAddresses =
new List<string> { ReceiverAddress }
},
Message = new Message
{
Subject = new Content(model.Subject),
Body = new Body
{
Html = new Content
{
Charset = "UTF-8",
Data = model.HtmlBody
},
}
},
};
// send async call to api
try
{
var response = await client.SendEmailAsync(sendRequest);
}
catch (Exception ex)
{
}
我已确认我的域已通过 AWS 控制台进行验证,并且还显示为“已启用发送”。
我哪里错了?
我找到了我的问题的答案。
该问题可以通过创建 IAM 用户组和有权访问 SES 服务的用户来解决。
然后我编辑代码以传递 AccessKeyId 和 SecretAccessKey。
var client = new AmazonSimpleEmailServiceClient(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.USWest2);
var response = new SendEmailResponse();
这是有效的。但是为了更安全,建议共享凭证文件 https://docs.aws.amazon.com/ses/latest/DeveloperGuide/create-shared-credentials-file.html使用。
希望这对其他人有帮助。
EDIT:在 AWS SES SDK V2 中,您需要更改AmazonSimpleEmailServiceClient
to AmazonSimpleEmailServiceV2Client
.
var client = new AmazonSimpleEmailServiceV2Client(awsAccessKeyId, awsSecretAccessKey, RegionEndpoint.USWest2);
var response = new SendEmailResponse();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)