2 Answers
- Newest
- Most votes
- Most comments
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);
answered 10 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.
answered 2 years ago
Relevant content
- asked 2 years ago
- asked 3 years ago
- asked a year ago
- AWS OFFICIALUpdated 2 months ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago
A note, As of version 3.7.300.55 of the AWS SDK for c# there is no property on the Message object of Attachments.
https://github.com/aws/aws-sdk-net/blob/main/sdk/src/Services/SimpleEmail/Generated/Model/Message.cs https://github.com/aws/aws-sdk-net/blob/main/sdk/src/Services/SimpleEmailV2/Generated/Model/Message.cs
In fact if you search for just Attachment on github - https://github.com/aws/aws-sdk-net/tree/main - there isn't a file under SimpleEmail called Attachment.
You'll need to use the SendRawEmail method
https://github.com/aws/aws-sdk-net/blob/4a009e3f6b72808bc3b1e5738b582e4461e66892/sdk/src/Services/SimpleEmail/Generated/_bcl35/AmazonSimpleEmailServiceClient.cs#L4073C45-L4073C57
In the SDK.
Looks like the only real way to send attachments is using rawMessage but its pretty difficult to do so.
This is the general idea of what needs to be done - https://docs.aws.amazon.com/ses/latest/dg/send-email-raw.html but no .net example provided.