1
votes

We have to rework how we're sending emails since we are using Amazon SES. In the past, we were using smtp but can't do that in this case. The class that needs updated has taken in a MailMessage object and used smtp to send it. So I'm trying to rework the method to be able to continue to accept the MailMessage object and convert it to a MimeKit MimeMessage. For the most part it's working fine except when it comes to attachments. In the code I have, the attachment gets added and sent, however, when trying to open it appears it's corrupted or something. In my test case I attached a csv file. I could not open it in excel after receiving the email.

public class EmailAbstraction
{
    public virtual void Send(MailMessage mailMessage)
    {
        sendMessage(mailMessage);
    }

    private static void sendMessage(MailMessage mailMessage)
    {
        using (var client = new AmazonSimpleEmailServiceClient(AwsConstants.SESAWSKey, AwsConstants.SESAWSSecret, AwsConstants.RegionEndpoint))
        {
            foreach (var to in mailMessage.To)
            {
                using (var messageStream = new MemoryStream())
                {
                    var newMessage = new MimeMessage();

                    var builder = new BodyBuilder
                    {
                        HtmlBody = mailMessage.Body
                    };

                    newMessage.From.Add(mailMessage.From == null
                        ? new MailboxAddress(EmailConstants.DefaultFromEmailDisplayName, EmailConstants.DefaultFromEmailAddress)
                        : new MailboxAddress(mailMessage.From.Address));

                    newMessage.To.Add(new MailboxAddress(to.DisplayName, to.Address));
                    newMessage.Subject = mailMessage.Subject;

                    foreach (var attachment in mailMessage.Attachments)
                    {
                        builder.Attachments.Add(attachment.Name, attachment.ContentStream);
                    }

                    newMessage.Body = builder.ToMessageBody();
                    newMessage.WriteTo(messageStream);

                    var request = new SendRawEmailRequest
                    {
                        RawMessage = new RawMessage { Data = messageStream }
                    };

                    client.SendRawEmail(request);
                }
            }
        }
    }
}

And in my test app, I have this.

internal class Program
{
    private static void Main(string[] args)
    {
        var s = GetFileStream();
        var m = new MailMessage();

        var sender = new MailAddress("[email protected]", "info");
        m.From = sender;
        m.Sender = sender;
        m.Body = "test email";
        m.Subject = "test subject";
        m.To.Add(myemail);
        m.Attachments.Add(new Attachment(s, "test-file.csv"));
        new EmailAbstraction().Send(m);
    }

    private static MemoryStream GetFileStream()
    {
        var stream = new MemoryStream();
        var fileStream = File.Open(@"C:\Users\dev\Desktop\test-file.csv", FileMode.Open);
        fileStream.CopyTo(stream);
        fileStream.Close();
        return stream;
    }
}
1

1 Answers

0
votes

This is just copied from the MimeKit source code:

    static MimePart GetMimePart (System.Net.Mail.AttachmentBase item)
    {
        var mimeType = item.ContentType.ToString ();
        var contentType = ContentType.Parse (mimeType);
        var attachment = item as System.Net.Mail.Attachment;
        MimePart part;

        if (contentType.MediaType.Equals ("text", StringComparison.OrdinalIgnoreCase))
            part = new TextPart (contentType);
        else
            part = new MimePart (contentType);

        if (attachment != null) {
            var disposition = attachment.ContentDisposition.ToString ();
            part.ContentDisposition = ContentDisposition.Parse (disposition);
        }

        switch (item.TransferEncoding) {
        case System.Net.Mime.TransferEncoding.QuotedPrintable:
            part.ContentTransferEncoding = ContentEncoding.QuotedPrintable;
            break;
        case System.Net.Mime.TransferEncoding.Base64:
            part.ContentTransferEncoding = ContentEncoding.Base64;
            break;
        case System.Net.Mime.TransferEncoding.SevenBit:
            part.ContentTransferEncoding = ContentEncoding.SevenBit;
            break;
        //case System.Net.Mime.TransferEncoding.EightBit:
        //  part.ContentTransferEncoding = ContentEncoding.EightBit;
        //  break;
        }

        if (item.ContentId != null)
            part.ContentId = item.ContentId;

        var stream = new MemoryStream ();
        item.ContentStream.CopyTo (stream);
        stream.Position = 0;

        part.Content = new MimeContent (stream);

        return part;
    }