How to Send email from Draft using Powershell? - powershell

I'm trying to automatically send all email saved, which are available in "Draft" using powershell.I've seen VBA can do, but powershell is the future. Sharing code will help other peer to tweak.

#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
}
}
try this

VWP.CS's answer was close but had to make a slight change to get it to work for me:
# 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()
}
}

Related

Is there a way to refresh an Outlook Inbox subfolder in powershell?

I have a powershell script to get e-mails from an Outlook Inbox subfolder, but it only returns e-mails after three or more executions.
Is there a way to prompt Outlook to refresh the Inbox subfolder before the script prompts the scan?
I am accessing the mailbox through another machine to guarantee the emails are on the subfolder and the flags are cleared.
Here it is a sample of the script:
# Add Interop Assembly
Add-type -AssemblyName "Microsoft.Office.Interop.Outlook" | Out-Null
# Create outlook COM object to search folders
$Outlook = New-Object -ComObject Outlook.Application
$OutlookNS = $Outlook.GetNamespace("MAPI")
$OutlookNS.Logon($null, $null, $false, $false);
$OutlookNS.SendAndReceive($false) | Wait-Job
Write-Output "Getting an email from Outlook..."
$Email = $OutlookNS.Folders.Item($Account).Folders.Item($Folder).Items | ? { ($_.FlagStatus -match 0) } | Sort ReceivedTime | Select -First 1
if($Email){
Write-Output $Email.Subject
} else {
Write-Output "No data"
}
...

Delete mail based on subject from outlook by power shell script

I wrote a script to delete particular mails from particular outlook account but it's not deleting mails based on mail subject . Can anybody tell me what is wrong in my code
CODE
$Outlook = New-Object -ComObject Outlook.Application
# Delete an Email from the folder Inbox with Subject Title "Action"
$EmailInFolderToDelete = $Outlook.Session.Folders.Item(1).Folders.Item("Inbox").Items
$EmailInFolderToDelete | ft SentOn, Subject, SenderName, To, Sensitivity -AutoSize -Wrap
$EmailToDelete = $EmailInFolderToDelete | Where-Object {$_.Subject -eq "Test mail";}
$EmailToDelete.Delete()
It's not showing the desired result and not deleting the particular mail from particular outlook account . Can anybody help me on this .
Based on the code on this hey scripting guy blog post. It worked just fine.
$olFolders = “Microsoft.Office.Interop.Outlook.olDefaultFolders” -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace(“MAPI”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderInBox)
$emailToDelete = $folder.items | Where-Object {$_.Subject -eq "Test mail";}
$EmailToDelete.Delete()

How to Set VotingResponses in Outlook via Powershell

I am trying to issue approvals on mailbox items via Powershell but can't seem to find the right method to do so. Here is the code I have used thus far.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$recipient = $namespace.CreateRecipient('othermailbox#example.local')
$recipient.Resolve();
$sharedFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolders::olFolderInBox)
$sharedFolder.Items
The member objects from $sharedFolder. Items contain string properties around VotingOptions and VotingResporse but I don't see a method to perform an approved vote. Any help is appreciated as there are hundreds of items I need to bulk approve. Thanks.
Loop through the items, for each item read the VotingOptions property, call MailItem.Reply (returns the new item), set the VotingResponse property on the reply message, call Send.
This is the code I ended up going with based on Dmitiry's input.
Add-type -assembly "Microsoft.Office.Interop.Outlook"
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$recipient = $namespace.CreateRecipient('othermailbox#example.local')
$recipient.Resolve()
$sharedFolder = $namespace.GetSharedDefaultFolder($recipient, $olFolders::olFolderInBox)
$messages = $sharedFolder.Items | Where-Object { $_.subject -eq "Approval Required" }
foreach ($message in $messages)
{
$reply = $message.Reply()
$reply.VotingResponse = "Approve"
$reply.Send()
}

New-Object cmdlet: How can I know is it the new process or it uses of existing ones?

PowerShell 4.0
I send the email through Outlook:
# Code sources:
# http://www.computerperformance.co.uk/powershell/powershell_function_send_email.htm
Function Global:Send-Email {
[cmdletbinding()]
Param (
[Parameter(Mandatory=$False,Position=0)]
[String]$Address = "abcd#yandex.ru",
[Parameter(Mandatory=$False,Position=1)]
[String]$Subject = "Hello!",
[Parameter(Mandatory=$False,Position=2)]
[String]$Body = "Hello from PowerShell through Outlook."
)
Begin {
Clear-Host
# Add-Type -assembly "Microsoft.Office.Interop.Outlook"
}
Process {
# Create an instance Microsoft Outlook
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "$Address"
$Mail.Subject = $Subject
# $Mail.Body =$Body
$Mail.HTMLBody = "<b>When</b> is swimming?"
$File = "$env:HOMEDRIVE\Temp\1.txt"
$Mail.Attachments.Add($File)
$Mail.Send()
} # End of Process section
End {
# Section to prevent error message in Outlook
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook)
$Outlook = $null
} # End of End section!
} # End of function
# Example of using this function
Send-Email #-Address deck#swimmingpool.com
This code either create new Outlook process (it such process is not exist still) or uses existing Outlook process. I want to close Outlook only if it wasn't working before. So, if Outlook was launched by user before my script begin to work, then I ought not to finish Outlook process. How can I know is the $Outlook use new process or it uses of existing Outlook process?
If I launch Get-Command -Noun *object* then I don't see something like get-object (for attempt at first to receive the existing object).

List Senders from a Shared Mailbox Folder (Recursively)

I am trying to get a full list of senders to an specific shared mailbox i have access to (vía Outlook).
So far I am using this small but handy script to choose the selected folder as I have no idea on how to call as a parameter (Param) the folder I want to check:
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
$namespace = $outlook.GetNameSpace("MAPI")
$Inbox = $namespace.pickfolder()
$Inbox.items | Select-Object -Property SenderName
After that I filter the output depending on the info I need (in this case, senders).
This forces me to choose the folder MANUALLy, I just want to set it as a parameter to the script.
How should I pass the specific folder as a parameter?.
Thanks in advance and best regards.
If I understood that correctly, you yould create a function where you pass the Inbox Name
function GetSenders
{
[CmdletBinding()]
Param
(
[Parameter(Mandatory=$True)]
[string]$Inbox
)
Begin
{
Add-type -assembly "Microsoft.Office.Interop.Outlook" | out-null
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$outlook = new-object -comobject outlook.application
}
Process
{
"Your Script here - What should happen with the Inbox?"
}
End
{
Write-Host "Script has Finished"
}
}
When you load this Function, you can just write GetSenders -Inbox "InboxName"