2016-07-20 7 views
0

Office 365에서 .msg 또는 .eml 파일로 전자 메일을 저장하는 것이 가능합니까?PowerShell - Office 365 전자 메일을 파일로 저장하는 방법?

이것이 내가 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 
    // ??????? 
} 

감사합니다. 감사합니다.

답변

0
// 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; 
     } 
    } 
}