2017-06-21 85 views
1

如何在此代碼中引用特定電子郵件地址,該代碼始終指向 默認電子郵件帳戶。我在Outlook中有多個電子郵件帳戶,我想從其他帳戶下載我想通過該電子郵件地址引用的電子郵件。我有一種感覺使用PowerShell從outlook下載電子郵件?

$ folder = $ namespace.getDefaultFolder($ olFolders :: olFolderInBox) 必須改變,請給出建議。

`[CmdletBinding(DefaultParameterSetName="All")] ` 

`Param( 
    [Parameter(Mandatory=$true, 
     Position=0, 
     HelpMessage='Folder path to store emails. Do not use quotation marks even if the path has spaces.', 
     ValueFromPipelineByPropertyName=$true 
    )] 
    [Alias("Destination", "Dest", "FullName")] 
    [String]$DestinationPath, ` 

    [Parameter(ParameterSetName="All")] 
    [Parameter(Mandatory=$true,ParameterSetName="Unread")] 
    [Switch]$UnreadOnly, 

    [Parameter(ParameterSetName="Unread")] 
    [Switch]$MarkRead 
) 

    #Removes invalid Characters for file names from a string input and outputs 
    the clean string 
` #Similar to VBA CleanString() Method 
    #Currently set to replace all illegal characters with a hyphen (-) 
    Function Remove-InvalidFileNameChars {` 

    param( 
     [Parameter(Mandatory=$true, Position=0)] 
     [String]$Name 
    ) 

    return [RegEx]::Replace($Name, "[{0}]" -f ([RegEx]::Escape([String][System.IO.Path]::GetInvalidFileNameChars())), '-') 
} 

#Test for destination folder nonexistence 
if (!(Test-Path $DestinationPath)) { 
    #Set values for prompt and menu 
    $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", ` 
     "Confirmation Choice" 
    $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", ` 
     "Negative Response" 
    $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no) 
    $title = "Invalid Destination" 
    $message = "The folder you entered does not exist. Would you like to create the folder?" 

    #Prompt for folder creation and store answer 
    $result = $host.UI.PromptForChoice($title, $message, $options, 0) 

    #If yes, create. 
    if ($result -eq 0) { 
     New-Item $DestinationPath -ItemType Directory | Out-Null 
     Write-Host "Directory created." 
    } 
    #If no, exit 
    else {exit} 
} 

#Add a trailing "\" to the destination path if it doesn't already 
if ($DestinationPath[-1] -ne "\") { 
    $DestinationPath += "\" 
} 

#Add Interop Assembly 
Add-type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null 

#Type declaration for Outlook Enumerations, Thank you Hey, Scripting Guy! blog for this demonstration 
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type] 
$olSaveType = "Microsoft.Office.Interop.Outlook.OlSaveAsType" -as [type] 
$olClass = "Microsoft.Office.Interop.Outlook.OlObjectClass" -as [type] 

#Add Outlook Com Object, MAPI namespace, and set folder to the Inbox 
$outlook = New-Object -ComObject Outlook.Application 
$namespace = $outlook.GetNameSpace("MAPI") 
#Future Functionality to Receive Email before saving - Still Needs Testing 
#$outlook.Session | Out-Null 
#$outlook.Session.SendAndReceive($false) | Out-Null 
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox) 

#Iterate through each object in the chosen folder 
foreach ($email in $folder.Items) { 

    #Get email's subject and date 
    [string]$subject = $email.Subject 
    [string]$sentOn = $email.SentOn 

    #Strip subject and date of illegal characters, add .msg extension, and combine 
    $fileName = Remove-InvalidFileNameChars -Name ($sentOn + "-" + $subject + ".msg") 

    #Combine destination path with stripped file name 
    $dest = $DestinationPath + $fileName 

    #Test if object is a MailItem 
    if ($email.Class -eq $olClass::olMail) { 

     #Test if UnreadOnly switch was used 
     if ($UnreadOnly) { 

      #Test if email is unread and save if true 
      if ($email.Unread) { 

       #Test if MarkRead switch was used and mark read 
       if ($MarkRead) { 
        $email.Unread = $false 
       } 
       $email.SaveAs($dest, $olSaveType::olMSG) 
      } 
     } 
     #UnreadOnly switch not used, save all 
     else { 
      $email.SaveAs($dest, $olSaveType::olMSG) 
     } 
    } 
} 

回答

1

認爲你可以做這樣的事情:

$outlook = New-Object -ComObject Outlook.Application 
$namespace =$outlook.GetNameSpace("MAPI") 
$namespace.Logon("Profilename","profilepassword",$false,$false) 

您也可以使用裝配 - Microsoft.Exchange.WebServices.dll,做這樣的事情:

[Reflection.Assembly]::LoadFile("C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0\Microsoft.Exchange.WebServices.dll") > $nul 
$getref = New-Object Microsoft.Exchange.WebServices.Data.ExchangeService([Microsoft.Exchange.WebServices.Data.ExchangeVersion]::Exchange2010_SP2) 
$getref.Credentials = New-Object Net.NetworkCredential('Account', 'Password', 'domain.local') 
$getref.AutodiscoverUrl("[email protected]") 
$inbox = [Microsoft.Exchange.WebServices.Data.Folder]::Bind($getref,[Microsoft.Exchange.WebServices.Data.WellKnownFolderName]::Inbox) 
#Write-Host "Total Messages:" $inbox.TotalCount 
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties) 
$ivItemView = New-Object Microsoft.Exchange.WebServices.Data.ItemView($inbox.TotalCount) 
$fiItems = $getref.FindItems($Inbox.Id,$ivItemView) 
[Void]$getref.LoadPropertiesForItems($fiItems,$psPropset) 

foreach($Item in $fiItems.Items){ 

if ($Item.From -EQ "[email protected]") { 

New-Object -TypeName PSObject -Property @{ 

Emails = $Item.From 

} | select Emails 
} 
} 
+0

能否請你告訴我,我怎麼能在上面的代碼中使用web服務交換電子郵件,下載味精或EML格式。現在我只能使用交換web服務來閱讀電子郵件。 –

+0

msg格式是Outlook客戶端格式,所以我認爲這是不可能的Exchange.WebServices。這種結構不起作用? :$ namespace.Logon(「Profilename」,「profilepassword」,$ false,$ false) – Vitaly

+0

我只需要使用交換web服務下載它。只要它有效,eml格式就沒有問題。 –

0

你好!您可以加載一個正文(爲HTML)是這樣的:

foreach($Item in $fiItems.Items){ 
if ($Item.Subject -match "Something special") { 
$Item.Load() 
$Save = ((Get-Date -Format "yyMMdd") + "-" + $Item.Subject[0] + ".html") 
New-Item -Path "C:\file\exch\" -name $Save -ItemType file -value $Item.Body.Text 
} 
} 
相關問題