如何将MailAttachment转换为MimeKit附件

时间:2019-02-22 15:57:28

标签: amazon-ses mimekit

由于我们正在使用Amazon SES,因此我们必须重新设计如何发送电子邮件。过去,我们使用的是smtp,但在这种情况下不能这样做。需要更新的类已接收MailMessage对象,并使用smtp发送它。因此,我正在尝试对该方法进行修改,使其能够继续接受MailMessage对象并将其转换为MimeKit MimeMessage。在大多数情况下,它工作正常,除了附件方面。在我的代码中,附件被添加并发送,但是,当尝试打开它时,它似乎已损坏或发生了什么。在我的测试案例中,我附加了一个csv文件。收到电子邮件后,我无法在excel中打开它。

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

在我的测试应用中,我有这个。

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

        var sender = new MailAddress("info@ourwebsite.com", "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 个答案:

答案 0 :(得分:0)

这只是从MimeKit源代码复制而来:

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