【问题标题】:PowerShell - How to save Office 365 email as File?PowerShell - 如何将 Office 365 电子邮件另存为文件?
【发布时间】:2016-07-20 13:45:59
【问题描述】:

我想知道是否可以在 Office 365 中将电子邮件保存为 .msg 或 .eml 文件?

这就是我对 REST API 所做的事情。

$credential = get-credential
$messagesuri = 'https://outlook.office365.com/api/v1.0/me/folders/Inbox/messages'

$messages = Invoke-RestMethod -Uri $messagesuri -Credential $credential
$messages.value | % {
    $mailitem = $_
    $subject = $mailitem.Subject
    $messageid = $mailitem.Id

    $messageid

    // Save Message ($messageid) as File
    // ???????
}

提前致谢。 问候日期。

【问题讨论】:

    标签: rest powershell office365 outlook.com


    【解决方案1】:
    // C# Console Application, using EWS connecting to Office 365
    // Working Solution (Prove of Concept).
    using System;
    using System.IO;
    using Microsoft.Exchange.WebServices.Data; // Download Library using Nugget.
    
    namespace EwsClient
    {
        class Program
        {
            static void Main(string[] args)
            {
                string myemail = ""; // Replace with your email address
                string password = ""; // Replace with your email password
    
                ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2013_SP1);
                service.Credentials = new WebCredentials(myemail, password);
                service.UseDefaultCredentials = false;
                service.AutodiscoverUrl(myemail, RedirectionUrlValidationCallback);
                //service.TraceEnabled = true;
                //service.TraceFlags = TraceFlags.All;
                ItemView itemsView = new ItemView(1);
                string querystring = "Kind:email";
    
                FindItemsResults<Item> itemResults = service.FindItems(WellKnownFolderName.Inbox, querystring, view: itemsView);
    
                foreach (var message in itemResults)
                {
                    Console.WriteLine(message.Subject);
    
                    using (FileStream fileStream = File.Open(@"C:\message.msg", FileMode.Create, FileAccess.Write))
                    {
                        message.Load(new PropertySet(ItemSchema.MimeContent));
                        MimeContent mc = message.MimeContent;
                        fileStream.Write(mc.Content, 0, mc.Content.Length);
                    }
                }
            }
    
            private 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;
            }
        }
    }
    

    【讨论】:

    • 保存 .msg 文件后无法在 Outlook 中打开。它对你有用吗?
    【解决方案2】:

    添加到@Dat 答案。用这个替换 foreach 代码。现在保存的电子邮件将在 Outlook 中打开。

    String[] invalidStings = { "\\", ",", ":", "*", "?", "\"", "<", ">", "|" };
            foreach (var message in itemResults)
            {
                EmailMessage email = message as EmailMessage;
                email.Load(new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.MimeContent));
                Byte[] content = email.MimeContent.Content;
                String fileName = email.Subject;
    
                // Replace all the invaild strings. 
                foreach (String str in invalidStings)
                {
                    fileName = fileName.Replace(str, "");
                }
    
                fileName = Path.Combine("DestinationPath", fileName + ".eml");
                File.WriteAllBytes(fileName, content);
                Console.WriteLine($"Saving: {email.Subject}");
            }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-06-11
    • 1970-01-01
    • 2020-12-22
    • 2016-08-30
    • 1970-01-01
    • 1970-01-01
    • 2015-08-23
    相关资源
    最近更新 更多