Send Attachments with Amazon-SES

前端 未结 7 802
没有蜡笔的小新
没有蜡笔的小新 2021-01-01 10:06

I\'m searching for an working C# example to send attachments with Amazon-SES.

After reading that Amazon-SES now supports sending attachments I was searching for an C

7条回答
  •  生来不讨喜
    2021-01-01 10:33

    I think that using AWS SDK for .NET and MimeKit is very easy and clean solution. You can send e-mails with attachments via SES API (instead of SMTP).

    You can write MimeMessage directly to MemoryStream and then use it with 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 = @"

    Amazon SES Test body

    ", 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("", ""); 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); } } }

提交回复
热议问题