Steve Hoyer
Steve Hoyer

Reputation: 1

mimekit/mailkit email saved as .eml, opened in the New Outlook, won't add attachments

I'm using mailkit/mimekit to create a .eml file. I'm then using ProcessStartInfo to open the .eml file in the default windows application. I'm doing it this way so that the user can add an attachment before sending the email.

Here's the code (pieced together from other folks - thank you) I'm using to create the email and start it up in Outlook (being my default program for .eml files).

   Public Sub SetUpEML(ByVal emailAddress As String, ByVal emailSubject As String, ByVal emailHTMLBody As String)
        Dim TempMailMessageFile As String = String.Format("{0}\", Environment.CurrentDirectory) & $"Export_{Now.ToString("yyyyMMddHHmmss")}.eml"

        Dim htmlBody = New BodyBuilder()
        htmlBody.HtmlBody = emailHTMLBody
        'htmlBody.Attachments.Add("C:\Users\myemail\Pictures\Programmers.jpg")

        Using Email As New MimeMessage
            With Email
                .To.Add(New MailboxAddress("My Email", "[email protected]"))
                .Headers.Add(New Header("X-Unsent", "1"))
                .Headers.Add(New Header("Content-Type", "multipart/mixed"))
                .Subject = emailSubject
                .Body = htmlBody.ToMessageBody
            End With

            Email.WriteTo(TempMailMessageFile)
        End Using

        Dim processStartInfo = New ProcessStartInfo
        processStartInfo.FileName = TempMailMessageFile
        processStartInfo.UseShellExecute = True
        Process.Start(processStartInfo)

    End Sub

This code works fine, but attaching files when the email opens in the New Outlook only works when the htmlBody.Attachments.Add... line is uncommented.

If I'm running Outlook (Classic), this works just fine. If I switch to the New Outlook, this doesn't work. The .eml file opens in outlook, but when I try to attach a file, I get the message "The following files couldn't be attached: filename.jpg. Please try again later." If I switch to the New Outlook, and programmatically add an attachment, I can then also add attachments when the .eml opens in the New Outlook.

I'd like it if my users didn't have to delete a random attachment before adding their own. While I could write a little code to open a dialog and have the user select files prior to creating the email, I'd rather allow them to just drag and drop or do whatever they do now.

What am I missing to allow attachments to be added to the email after it's opened in the New Outlook?

Upvotes: 0

Views: 121

Answers (1)

jstedfast
jstedfast

Reputation: 38528

The problem you are hitting is that Outlook makes assumptions about the MIME structure of the saved email and assumes that it is structured in a way that Outlook itself would structure it.

What you need to do is make sure to 100% match the same MIME structure that the email client you are using would save it in the same hierarchy of MIME parts.

What I would recommend doing is to create messages in the "New Outlook" with all of the attachments/etc that you want to have, save it, and then try to reproduce the same structure using MimeKit.

Upvotes: 0

Related Questions