Send Email with Aattachment, on AWS SES in C#/.Net using Amazon.SimpleEmailV2

0

I'm sending simple emails and templated emails, now I need to add attachment, but I tried everything that I found and anything worked, someone can help? or send a simple code how to send an email with attachment in AWS SES v2 with C#/.Net

SCM
已提問 1 年前檢視次數 523 次
2 個答案
0
已接受的答案

To attach a file to an email with Amazon SES, you must use SendRawEmail as follows.
https://docs.aws.amazon.com/ja_jp/ses/latest/dg/send-email-raw.html

NET, you should be able to use SendRawEmailRequest as described in the following document.
https://docs.aws.amazon.com/sdkfornet/latest/apidocs/items/TSimpleEmailSendRawEmailRequestNET45.html

profile picture
專家
已回答 1 年前
0

There doesn't appear to be great support, if any support, to send emails with attachments in SES in the traditional MailMessage way using Smtp classes within .net.

This is for .net 8. It likely works for .net 6 and above.

The easiest way to send emails with attachments will be to add MimeKit to your project. The following code worked within a lambda function as well. I've verified the following works, the AWS documentation directs you to raw email sending but lacks examples using attachments if you were to piece together a raw email yourself.

 var client = new AmazonSimpleEmailServiceV2Client(RegionEndpoint.USEast1)
    var mime = new MimeMessage();

    var bodyBuilder = new BodyBuilder();
    mime.From.Add(new MailboxAddress("email", username));
    mime.To.Add(new MailboxAddress("email", sendTo));

    mime.Subject = $"Email Subject With Date - {DateTime.UtcNow:yyyyMMdd}";

    // attachments here would be a List of a custom class of attachments that looks like
    // public class MyAttachment
    // {
    //     public Byte[] File { get; set; }
    //     public string FileName { get; set; }
    // }
    // var attachments = new List<MyAttachment>();
    // attachments.Add(new MyAttachment { FileName = "MyFileName.csv", File = <read in a file and convert to bytes> });'

    foreach (var item in attachments)
    {
        bodyBuilder.Attachments.Add(item.FileName, item.File, ContentType.Parse("text/csv"));
    }

    mime.Body = bodyBuilder.ToMessageBody();

    var stream = new MemoryStream();
    mime.WriteTo(stream);

    var request = new SendEmailRequest();
    request.Content = new EmailContent();
    request.Content.Raw = new RawMessage();
    request.Content.Raw.Data = stream;

    await client.SendEmailAsync(request);

Adjust text/csv, csv file information to meet needs of whatever it is you are attaching. I believe there is a 10MB attachment limit when sending emails with SES.

profile picture
已回答 2 個月前

您尚未登入。 登入 去張貼答案。

一個好的回答可以清楚地回答問題並提供建設性的意見回饋,同時有助於提問者的專業成長。

回答問題指南