2
votes

Do either Microsoft Graph or Outlook REST API support importing existing emails into an Office 365 mailbox?

By definition, importing means copying emails in a way that preserves their original information, including their created / sent / received date.

I have tried these endpoints to no avail:

Thus either I have used them wrongly, or it is simply that they do not support setting date-related fields.

2

2 Answers

2
votes

No, the APIs do not have any ability for importing. It's a great idea though! You should make an entry on our UserVoice forum.

0
votes

What i could understand that you have existing emails somewhere on an archiving server and you would like to import them to your Outlook Online or Outlook Office 365.

What you can do that you can utilize the Exchange WebServices and import your exported emails. Mostly the emails can be imported by .eml or .msg format. I can provide you the guidance for .eml files.

On your archiving server you can obtain the .eml file backups of emails or you can generate one by exporting an email from Outlook Desktop / Mozilla Thunderbird for testing purposes.

Now you can use the Nuget Package Microsoft.Exchange.WebServices which is Actually Managed API for Microsoft Exchange WebServices

You can utilize the following code

    void main()
    {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        // Get the information of the account
        service.Credentials = new WebCredentials("account email here", "account password here");
        service.AutodiscoverUrl("account email here", RedirectionUrlValidationCallback);
        UploadMIMEEmail(service);
    }

    public static bool RedirectionUrlValidationCallback(string redirectionUrl)
    {
        // The default for the validation callback is to reject the URL.
        bool result = false;

        Uri redirectionUri = new Uri(redirectionUrl);

        // Validate the contents of the redirection URL. In this simple validation
        // callback, the redirection URL is considered valid if it is using HTTPS
        // to encrypt the authentication credentials. 
        if (redirectionUri.Scheme == "https")
        {
            result = true;
        }
        return result;
    }


    private static void UploadMIMEEmail(ExchangeService service)
    {
        EmailMessage email = new EmailMessage(service);

        string emlFileName = @"E:\asad.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)
                    break;

                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.
        email.Save(WellKnownFolderName.Inbox);
    }

What this method does is that uploads the email to the exchange server as an imported email with all the email data as exactly found in the exported .eml.

If you exchange server is running locally / within domain then you can also specify the exchange URL by

service.Url = new Uri("https://computername.domain.contoso.com/EWS/Exchange.asmx");

Also if you want to login using default credentials then you can specify

service.UseDefaultCredentials = true;

For more information you can follow

https://msdn.microsoft.com/en-us/library/office/dn672319(v=exchg.150).aspx#bk_importproperties

https://code.msdn.microsoft.com/how-to-import-vcard-files-ffa0ff50