gpt4 book ai didi

c# - 使用 Amazon-SES 发送附件

转载 作者:IT王子 更新时间:2023-10-29 04:34:38 29 4
gpt4 key购买 nike

我正在寻找一个可用的 C# 示例来使用 Amazon-SES 发送附件。

看完之后 Amazon-SES现在支持发送附件我正在搜索 C# 示例但找不到。

最佳答案

我认为使用 AWS SDK for .NETMimeKit是非常简单和干净的解决方案。您可以通过 SES API(而不是 SMTP)发送带附件的电子邮件。

您可以将 MimeMessage 直接写入 MemoryStream,然后与 SES SendRawEmail 一起使用:

using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using Amazon;
using Amazon.Runtime;
using MimeKit;

private static BodyBuilder GetMessageBody()
{
var body = new BodyBuilder()
{
HtmlBody = @"<p>Amazon SES Test body</p>",
TextBody = "Amazon SES Test body",
};
body.Attachments.Add(@"c:\attachment.txt");
return body;
}

private static MimeMessage GetMessage()
{
var message = new MimeMessage();
message.From.Add(new MailboxAddress("Foo Bar", "foo@bar.com"));
message.To.Add(new MailboxAddress(string.Empty, "foobar@example.com"));
message.Subject = "Amazon SES Test";
message.Body = GetMessageBody().ToMessageBody();
return message;
}

private static MemoryStream GetMessageStream()
{
var stream = new MemoryStream();
GetMessage().WriteTo(stream);
return stream;
}

private void SendEmails()
{
var credentals = new BasicAWSCredentials("<aws-access-key>", "<aws-secret-key>");

using (var client = new AmazonSimpleEmailServiceClient(credentals, RegionEndpoint.EUWest1))
{
var sendRequest = new SendRawEmailRequest { RawMessage = new RawMessage(GetMessageStream()) };
try
{
var response = client.SendRawEmail(sendRequest);
Console.WriteLine("The email was sent successfully.");
}
catch (Exception e)
{
Console.WriteLine("The email was not sent.");
Console.WriteLine("Error message: " + e.Message);
}
}
}

关于c# - 使用 Amazon-SES 发送附件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6743139/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com