如何使用Powershell从草稿发送电子邮件?

时间:2017-05-11 16:30:22

标签: powershell

我正在尝试自动发送所有已保存的电子邮件,这些电子邮件都是使用powershell在“草稿”中提供的。我看过VBA可以做到,但PowerShell是未来。共享代码将帮助其他同行进行调整。

2 个答案:

答案 0 :(得分:0)

#Send all items in the "Drafts" folder that have a "To" address filled in.

#Setup Outlook
Add-Type -assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -comobject Outlook.Application
$myOutlook = $Outlook.Application
$myNameSpace = $myOutlook.GetNamespace("MAPI")
$myFolders = $myNameSpace.Folders

#Set Draft Folder.
$myDraftsFolder = $myFolders("rsprebitz@idafoundation.org").Folders("Drafts")

#Loop through all Draft Items

$myDraftsFolder.Items|foreach-object {

  #Check for "To" address and only send if "To" is filled in.
  If ($_.To.trim().length -gt 0) {

    #Send Item
    $_.Send

  }
}

试试这个

答案 1 :(得分:0)

VWP.CS的答案很接近,但必须稍作更改才能使其对我有用:

# Initialize components
  Add-Type -assembly "Microsoft.Office.Interop.Outlook"
  $Outlook = New-Object -comobject Outlook.Application
  $namespace = $Outlook.GetNameSpace("MAPI")

# Set drafts folder
  $myDraftsFolder = $namespace.Folders.Item('yourmailboxname').Folders.Item('Drafts')

# Loop through all Draft Items
  $myDraftsFolder.Items | foreach-object {

    # Check for "To" address and only send if "To" is filled in.
    If ($_.To.trim().length -gt 0) {

      #Send Item
      Write-Verbose "Sending..." -Verbose
      $_.Send()

    }
  }