title: How to convert MailAttachment to MimeKit Attachment
body: <p>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.</p>
  
  <pre><code>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);
                  }
              }
          }
      }
  }
  </code></pre>
  
  <p>And in my test app, I have this.</p>
  
  <pre><code>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;
      }
  }
  </code></pre>