How to send Mimemessage to Exchange server ? (C#) RRS feed

  • Question

  • Hi, I got an url-safe base64 encoded  email message( including headers and body) retrieved from gmail. I need to send this message directly to exchange server without specifying To,From,Subject and body as my message contains it all together. Is there a way to create such message from this gmail message ? I am new to exchange. please let me know any possible solutions. I want to do something similar to..


    best regards,

    Tuesday, October 14, 2014 5:48 PM

All replies

  • Try this:


    private static void UploadMIMEEmail(ExchangeService service)
        EmailMessage email = new EmailMessage(service);
        string emlFileName = @"C:\import\email.eml";
        using (FileStream fs = new FileStream(emlFileName, FileMode.Open, FileAccess.Read))
            byte[] bytes = new byte[fs.Length];
            int numBytesToRead = (int)fs.Length;
            int numBytesRead = 0;
            while (numBytesToRead > 0)
                int n = fs.Read(bytes, numBytesRead, numBytesToRead);
                if (n == 0)
                numBytesRead += n;
                numBytesToRead -= n;
            // Set the contents of the .eml file to the MimeContent property.
            email.MimeContent = new MimeContent("UTF-8", bytes);
        // Indicate that this email is not a draft. Otherwise, the email will appear as a 
        // draft to clients.
        ExtendedPropertyDefinition PR_MESSAGE_FLAGS_msgflag_read = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
        email.SetExtendedProperty(PR_MESSAGE_FLAGS_msgflag_read, 1);
        // This results in a CreateItem call to EWS. The email will be saved in the Inbox folder.

    Where email.eml is your gmail message.

    Michael | Microsoft Exchange Developer Content

    The Exchange Development Forum Guide has useful information for using the Exchange Development Forum.

    Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.

    Tuesday, October 14, 2014 6:33 PM
  • Sorry for very late reply.  I tried the above code but it failed with  Microsoft.Exchange.WebServices.Data.ServiceResponseException: Invalid MIME content exception. 

    First I tried with .txt extension and then I tried .eml extension. The program did not work in both of the cases.

    I have Gmail message in RAW format as gmail doesn't support .eml format.

    "raw": Returns the entire email message content in the raw field as a URL-safe base64 encoded string. Gmail is compliant with RFC2822. RAW message includes the identifiers, labels, metadata, MIME structure, and small body parts.

    My goal is to migrate gmail message to exchange server without any formatting issues.  Please help me.

    best regards,

    Tuesday, November 25, 2014 7:21 AM