使用powershell从outlook下载电子邮件?

使用powershell从outlook下载电子邮件?,powershell,outlook,Powershell,Outlook,如何在代码中引用它始终指向的特定电子邮件地址 默认的电子邮件帐户。我在outlook中有多个电子邮件帐户,我想从另一个帐户下载电子邮件,我想通过该电子邮件地址引用该帐户。我有一种感觉 $folder=$namespace.getDefaultFolder($olFolders::olFolderInBox) 必须更改,请给出建议 `[CmdletBinding(DefaultParameterSetName="All")] ` `Param( [Parameter(Mandatory

如何在代码中引用它始终指向的特定电子邮件地址 默认的电子邮件帐户。我在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) 
        } 
    } 
}

你认为你可以做这样的事情:

$outlook = New-Object -ComObject Outlook.Application  
$namespace =$outlook.GetNameSpace("MAPI") 
$namespace.Logon("Profilename","profilepassword",$false,$false)
[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("Account@domain.com")
$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 "Somemail@domain.com") {

New-Object -TypeName PSObject -Property @{

Emails = $Item.From

} | select Emails
}
}
您还可以使用Assembly-Microsoft.Exchange.WebServices.dll并执行以下操作:

$outlook = New-Object -ComObject Outlook.Application  
$namespace =$outlook.GetNameSpace("MAPI") 
$namespace.Logon("Profilename","profilepassword",$false,$false)
[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("Account@domain.com")
$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 "Somemail@domain.com") {

New-Object -TypeName PSObject -Property @{

Emails = $Item.From

} | select Emails
}
}
你好!!您可以加载正文文本(到html),如下所示:

$outlook = New-Object -ComObject Outlook.Application  
$namespace =$outlook.GetNameSpace("MAPI") 
$namespace.Logon("Profilename","profilepassword",$false,$false)
[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("Account@domain.com")
$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 "Somemail@domain.com") {

New-Object -TypeName PSObject -Property @{

Emails = $Item.From

} | select Emails
}
}

您能告诉我如何使用上述代码中的exchange webservices下载msg或eml格式的电子邮件吗。目前,我只能使用exchange webservices阅读电子邮件。msg格式是Outlook客户端格式,因此我认为exchange.webservices不可能。这个建筑不起作用吗$Logon(“Profilename”、“profilepassword”、$false、$false)我只需要使用ExchangeWebServices下载它。只要有效,eml格式就可以。