【问题标题】:Import existing emails into Office 365 While Preserving Original Date将现有电子邮件导入 Office 365,同时保留原始日期
【发布时间】:2016-04-15 10:24:32
【问题描述】:

Microsoft Graph 或 Outlook REST API 是否支持将现有电子邮件导入 Office 365 邮箱?

根据定义,导入意味着以保留原始信息(包括创建/发送/接收日期)的方式复制电子邮件。

我尝试了这些端点无济于事:

所以要么是我用错了,要么就是不支持设置日期相关的字段。

【问题讨论】:

    标签: office365 office365api microsoft-graph-api outlook-restapi office365-restapi


    【解决方案1】:

    不,API 没有任何导入功能。不过这是个好主意!您应该在我们的UserVoice 论坛上留言。

    【讨论】:

    • 啊,会的。同时,提供这种能力的最接近的 API 是什么? EWS 托管 API 能解决问题吗?
    • EWS 可以做到,特别是如果您有要导入的 MIME 流:msdn.microsoft.com/en-us/library/office/…
    【解决方案2】:

    我可以理解的是,您在存档服务器上的某处有现有电子邮件,并且您希望将它们导入您的 Outlook Online 或 Outlook Office 365。

    您可以使用 Exchange 网络服务并导入导出的电子邮件。大多数电子邮件可以通过 .eml 或 .msg 格式导入。我可以为您提供 .eml 文件的指导。

    在您的存档服务器上,您可以获得电子邮件的 .eml 文件备份,或者您可以通过从 Outlook 桌面/Mozilla Thunderbird 导出电子邮件来生成备份以进行测试。

    现在您可以使用 Nuget 包 Microsoft.Exchange.WebServices,它实际上是 Microsoft Exchange WebServices 的托管 API

    您可以使用以下代码

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

    此方法的作用是将电子邮件作为导入的电子邮件上传到交换服务器,其中所有电子邮件数据与导出的 .eml 中的所有电子邮件数据完全相同。

    如果您的交换服务器在本地/域内运行,那么您还可以通过

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

    此外,如果您想使用默认凭据登录,则可以指定

    service.UseDefaultCredentials = true;
    

    更多信息你可以关注

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-06-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-09-24
      • 1970-01-01
      相关资源
      最近更新 更多