Send Email with attachment in SES via c# SDK

0

Can you provide the link of document where I can achieve Send Email with attachment in SES via c# SDK.

2 Answers
0

The easiest way to send an email with via SES that needs attachments in c# will be using MimeKit and then SendEmail/SendEmailAsync method.

Example:

    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);

profile picture
answered 2 months ago
-1

Haven't tried this myself - but found this, please check if this helps

using Amazon;
using Amazon.SimpleEmail;
using Amazon.SimpleEmail.Model;
using System.IO;

public static void SendEmailWithAttachment(string senderEmailAddress, string recipientEmailAddress, string emailSubject, string emailBody, string attachmentFilePath)
{
    // Instantiate the Amazon SES client with your AWS credentials
    AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(RegionEndpoint.USWest2); // Change the region to your desired region

    // Create the email message
    Content subject = new Content(emailSubject);
    Content body = new Content(emailBody);
    Body messageBody = new Body { Html = body };

    // Create the email attachment
    var attachmentStream = new MemoryStream(File.ReadAllBytes(attachmentFilePath));
    var attachment = new Attachment
    {
        FileName = Path.GetFileName(attachmentFilePath),
        Content = attachmentStream
    };

    // Create the email request
    SendEmailRequest emailRequest = new SendEmailRequest
    {
        Source = senderEmailAddress,
        Destination = new Destination { ToAddresses = new List<string> { recipientEmailAddress } },
        Message = new Message { Subject = subject, Body = messageBody },
        // Add the attachment to the email
        Message = new Message { Subject = subject, Body = messageBody, Attachments = new List<Attachment> { attachment } }
    };

    // Send the email
    client.SendEmail(emailRequest);
}

  • senderEmailAddress: The email address you want to send the email from.
  • recipientEmailAddress: The email address you want to send the email to.
  • emailSubject: The subject of the email.
  • emailBody: The body of the email.
  • attachmentFilePath: The path to the file you want to attach to the email.
AWS
Arun
answered a year ago

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions