For logging tasks I need to send me an email through Outlook. I wrote some code like this:
$outlook = New-Object -ComObject Outlook.Application
$namespace = $outlook.GetNameSpace("MAPI")
$olFolders = "Microsoft.Office.Interop.Outlook.olDefaultFolders" -as [type]
$InboxDef = $namespace.GetDefaultFolder($olFolders::olFolderInBox)
$InboxDef.FullFolderPath -match "^\\\\(.*)\\Inbox$" | Out-Null
$recipient = $matches[1]
$email = $outlook.CreateItem(0)
$email.To = "$recipient"
$email.Subject = "Title"
$email.Body = "Text"
$email.Send()
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
When I subsequently launch the Outlook client I see the email sent twice
The code looks good. One thing which is not clear enough is setting a recipient for the outgoing email:
$InboxDef.FullFolderPath -match "^\\\\(.*)\\Inbox$" | Out-Null
$recipient = $matches[1]
It is not clear what value is used in the code. To make sure the property is set correctly I'd suggest using the Recipients property of the MailItem class instead. The Recipients.Add method creates a new recipient in the Recipients collection. Then don't forget to use the Recipient.Resolve
method which attempts to resolve a Recipient object against the Address Book.
Read more about that in the article which I wrote for the technical blog, see How To: Fill TO,CC and BCC fields in Outlook programmatically.
Related
I am trying to retrieve sent emails from my outlook using powershell. When i run below code, it pulls the details, but it prints the name of the person email sent to instead of their email address. How can i get user full email address instead of their name?
Function Get-OutlookSentItems {
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”)
$folder = $namespace.getDefaultFolder($olFolders::olFolderSentMail)
$folder.items |
Select-Object -Property Subject, SentOn, Importance, To
} #end function Get-OutlookSentItems
Get-OutlookSentItems
Current output Test,user
Expected output testuser#test.com
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()
I'm trying to create a Powershell script that gets the specific part of a text file, read it and then put it into the contents of an email and sends it. This is what I currently have:
$logs = (Get-EventLog system | where {$_.InstanceId -eq 7001 -and
$_.TimeWritten -gt (Get-Date).Adddays(-1)}).TimeWritten | Out-String
#to file
$logs | Out-File ".\Results.txt"
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "SomeMailAddress.com"
$Mail.Subject = "Time"
$Mail.Body = $logs
#send message
$Mail.Send()
#quit and cleanup
$Outlook.Quit()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
I'm able to create the text file, output the data, I believe the Get-Content is getting that specific portion of time but I'm not sure how to use Set-Content and put that into the email. Any suggestions/help would be appreciated
The simplest way to send email through PowerShell is by using Send-MailMessge.
Below is how you would send using the Outlook ComOjbect.
Note: If you have to use the outlook comobject, make sure you run PowerShell and outlook the same way with the same account.
Example:
$logs = (Get-EventLog system | where {$_.InstanceId -eq 7001 -and $_.TimeWritten -gt (Get-Date).Adddays(-1)}).TimeWritten | Out-String
#create COM object named Outlook
$Outlook = New-Object -ComObject Outlook.Application
#create Outlook MailItem named Mail using CreateItem() method
$Mail = $Outlook.CreateItem(0)
#add properties as desired
$Mail.To = "jrider#yourDomain.com"
$Mail.Subject = "Time"
$Mail.Body = $logs
#send message
$Mail.Send()
[System.Runtime.Interopservices.Marshal]::ReleaseComObject($Outlook) | Out-Null
I use Outlook 2010 and Powershell 2.0.
I want send a Outlook message, and delay delivery programmatically of a message using Powershell.
How can I create a new Outlook email and immediately defer delivery?
If you try this:
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail | Get-Member
you'll get a list of all methods/properties available on the mail object.
One property is DeferredDeliveryTime. You can set it like this:
#Stay in the outbox until this date and time
$mail.DeferredDeliveryTime = "11/2/2013 10:50:00 AM"
Or:
#Wait 10 minutes before sending mail
$date = Get-Date
$date = $date.AddMinutes(10)
$mail.DeferredDeliveryTime = $date
Solution:
$ol = New-Object -comObject Outlook.Application
$ns = $ol.GetNameSpace("MAPI")
# call the save method yo dave the email in the drafts folder
$mail = $ol.CreateItem(0)
$null = $Mail.Recipients.Add("xxxx#serverdomain.es")
$Mail.Subject = "PS1 Script TestMail"
$Mail.Body = " Test Mail "
$date = Get-Date
$date = $date.AddMinutes(2)
$Mail.DeferredDeliveryTime = $date #"2/11/2013 10:50:00 AM"
$Mail.save()
# get it back from drafts and update the body
$drafts = $ns.GetDefaultFolder($olFolderDrafts)
$draft = $drafts.Items | where {$_.subject -eq 'PS1 Script TestMail'}
$draft.body += "`n foo bar"
$draft.save()
$inspector = $draft.GetInspector
$inspector.Display()
# send the message
$draft.Send()
References:
Create Outlook email draft using PowerShell
http://office.microsoft.com/en-us/outlook-help/delay-or-schedule-sending-email-messages-HP010355051.aspx
Update
To change default account:
$Mail.SendUsingAccount = $ol.Session.Accounts | where {$_.DisplayName -eq $FromMail}
References:
http://msmvps.com/blogs/richardsiddaway/archive/2011/08/08/outlook-sending-emails.aspx
Outlook automation - Change Sender Account
I'm creating a PowerShell script to automate a process at work. This process requires an email to be filled in and sent to someone else. The email will always roughly follow the same sort of template however it will probably never be the same every time so I want to create an email draft in Outlook and open the email window so the extra details can be filled in before sending.
I've done a bit of searching online but all I can find is some code to send email silently. The code is as follows:
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add("XXX#YYY.ZZZ")
$Mail.Subject = "PS1 Script TestMail"
$Mail.Body = "
Test Mail
"
$Mail.Send()
In short, does anyone have any idea how to create and save a new Outlook email draft and immediately open that draft for editing?
Based on the other answers, I have trimmed down the code a bit and use
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.Subject = "<subject>"
$mail.Body = "<body>"
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
This removes the unnecessary step of retrieving the mail from the drafts folder. Incidentally, it also removes an error that occurred in Shay Levy's code when two draft emails had the same subject.
$olFolderDrafts = 16
$ol = New-Object -comObject Outlook.Application
$ns = $ol.GetNameSpace("MAPI")
# call the save method yo dave the email in the drafts folder
$mail = $ol.CreateItem(0)
$null = $Mail.Recipients.Add("XXX#YYY.ZZZ")
$Mail.Subject = "PS1 Script TestMail"
$Mail.Body = " Test Mail "
$Mail.save()
# get it back from drafts and update the body
$drafts = $ns.GetDefaultFolder($olFolderDrafts)
$draft = $drafts.Items | where {$_.subject -eq 'PS1 Script TestMail'}
$draft.body += "`n foo bar"
$draft.save()
# send the message
#$draft.Send()
I think Shay Levy's answer is almost there: the only bit missing is the display of the item.
To do this, all you need is to get the relevant inspector object and tell it to display itself, thus:
$inspector = $draft.GetInspector
$inspector.Display()
See the MSDN help on GetInspector for fancier behaviour.
if you want to use HTML template please use HTMLbody instead of Body , please find sample code below:
$ol = New-Object -comObject Outlook.Application
$mail = $ol.CreateItem(0)
$mail.Subject = "Top demand apps-SOURCE CLARIFICATION"
$mail.HTMLBody="<html><head></head><body><b>Joseph</b></body></Html>"
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()
Thought I would add in to this as well. There are a few steps you can save yourself if you know a lot of the basics (subject, recipients, or other aspects). First create the template of the email and save that, e.g. somewhere maybe with the code?
As to the code itself, it follows much the same that others have posted.
Borrowing from Jason:
$ol = New-Object -comObject Outlook.Application
$msg = $ol.CreateItemFromTemplate(<<Path to template file>>)
Modify as needed. Append fields or modify body. The message can still be viewed prior to sending the same way $msg.GetInspector.Display(). Then call $msg.send() to send away!