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

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
전문가
답변함 일 년 전
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달 전

로그인하지 않았습니다. 로그인해야 답변을 게시할 수 있습니다.

좋은 답변은 질문에 명확하게 답하고 건설적인 피드백을 제공하며 질문자의 전문적인 성장을 장려합니다.

질문 답변하기에 대한 가이드라인

관련 콘텐츠