Reading MSG file from outlook via powershell .. how to get email address? - powershell

Fairly simple script at the moment I simply need to get the recipients email address out of a msg file, apparently there's not an easy way to parse it in text so I've used the following code, but I only get a name such as 'Joe Smith' when reading the message not joe.smith#mydomain.com
Any ideas?
Thanks!
$outlook = New-Object -comobject outlook.application
$msg = $outlook.CreateItemFromTemplate("c:\MyMessage.msg")
$msg | Select to

You need to access the Recipients collection and read the Recipient.Address property for the items in that collection. You also might want to use Application.Session.OpenSharedItem instead of CreateItemFromTemplate.

Related

Outlook PowerShell script to set Direct Replies To address

I have an automated powershell script to send alert email. Since the alert are send from a generic email.. i want people replying to the email to goto another email address say efg#outlook.com.
$Outlook = New-Object -comObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "asd#outlook.com"
$Mail.Subject = "Alert Email"
$Mail.HtmlBody = $HTML
$Mail.Headers.Add("In-Reply-To", "<efg#outlook.com>")
$Mail.display()
$Mail.send()
somehow this is not working.
Managed to get it from the below
ReplyRecipientNames
$Mail.ReplyRecipientNames = "efg#outlook.com"
Firstly, MailItem object does not expose the Headers property - your script will stop rigth there.
If you want to redirect replies to a different address, use Mail.ReplyRecipients.Add("efg#outlook.com")
You can use the MailItem.SendUsingAccount property which allows setting up an Account object that represents the account under which the MailItem is to be sent. Be aware, the account should be configured in Outlook in that case. For example, here is a VBA code sample which shows how to set up the property:
Sub SendUsingAccount()
Dim oAccount As Outlook.account
For Each oAccount In Application.Session.Accounts
If oAccount.AccountType = olPop3 Then
Dim oMail As Outlook.MailItem
Set oMail = Application.CreateItem(olMailItem)
oMail.Subject = "Sent using POP3 Account"
oMail.Recipients.Add ("someone#example.com")
oMail.Recipients.ResolveAll
Set oMail.SendUsingAccount = oAccount
oMail.Send
End If
Next
End Sub
For Exchange users you may also consider using the MailItem.SentOnBehalfOfName property which is represented by a string indicating the display name for the intended sender of the mail message.
But also you may be interested in the MailItem.ReplyRecipients property which returns a Recipients collection that represents all the reply recipient objects for the Outlook item. The following article which I wrote for the technical blog shows how to deal with a recipient collection, see How To: Fill TO,CC and BCC fields in Outlook programmatically for more information.

Issue related to Powershell send-mail

I am using powershell send-mail command to send mail. It is working fine. But I cant see the mail which I sent in "sent items". I can see it if I sent mail manually, but I am not able to understand why it is not visible if i sent a mail using send-mail command.
Thanks
I have a function which creates an Outlook "Draft". I do it this way so that the e-mail can be reviewed before hitting "Send". This may not work for you; perhaps you'd want to add a send command at the end. But what you describe wanting to do is probably going to need outlook available on the system which is executing the commands.
It will check to see if you have a mail profile set up first, but otherwise it's not super-robust.
Function Compose-Email {
Param ([String]$recipients, [string]$subject, [string]$body)
$reg="HKCU:\Software\Microsoft\Office\16.0\Outlook\Profiles"
$child=(Get-ChildItem -Path $reg).name
if (!((Get-ChildItem -Path $reg).name)) {
Write-Error "No Mail Profile found! Cannot compose Draft."
}
else {
$olFolderDrafts = 16
$ol = New-Object -comObject Outlook.Application
$ns = $ol.GetNameSpace("MAPI")
# call the save method to save the email in the drafts folder
$mail = $ol.CreateItem(0)
$Mail.Recipients.Add($recipients)
$Mail.Subject = $subject
$Mail.Body = $body
$Mail.save()
$mail.display()
}
}

Can't get senders email address from read receipts

I am using the code posted on here to try and retrieve the senders email address form read receipts in PowerShell. For some emails this works OK but for others it does not work.
In Outlook I can clearly see who it's from, but PowerShell returns a blank cell.
Any help please?
function Get-OutlookInBox {
Add-Type -AssemblyName "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.pickfolder()
$Folder.Items | Get-Member
$Folder.Items |
Select-Object Subject, SenderName, SenderEmailAddress, CreationTime,
ReceivedTime, Final-recipient, UserProperties, ItemProperties,
MAPIOBJECT, ReceivedByName, Recipents, Sender, SentOn,
SentOnBehalfOfName |
Export-CSV -NoTypeInformation xxx\Trial.csv
}
First of all, make sure that specified exist and can't be retrieved for a particular object. For example, I have noticed the Final-recipient which doesn't have any corresponding property in the OOM.
Note, you need to iterate over all items in the folder and deal with mail items only because other Outlook items may not have properties specified to export. So, you may check the Class property before doing anything.
A yet better solution is to use the Find/FindNext or Restrict methods of the Items class to get items that correspond to your conditions. Read more about these methods in the following articles:
How To: Use Find and FindNext methods to retrieve Outlook mail items from a folder (C#, VB.NET)
How To: Use Restrict method to retrieve Outlook mail items from a folder

How do you set a message Reply-To address using EWS Managed API?

How do you set the reply-to header when sending a message using Exchange Web Services Managed API in Powershell v3?
I have a Microsoft.Exchange.WebServices.Data.EmailMessage object and can set the from address, add attachments, and send mail successfully.
I was able to add an x-header using:
$xheader = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition([Microsoft.Exchange.WebServices.Data.DefaultExtendedPropertySet]::InternetHeaders,"x-my-header",[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
and adding it to $pspropset but if I use reply-to as the value the header is not inserted.
Using valuable and hard to find information posted by Glen Scales in this thread I believe two extended properties, PidTagReplyRecipientEntries and PidTagReplyRecipientNames need to be set on the EmailMessage object.
I am able to set both extended properties without errors but this does not result in a Reply-To header in the message.
Relevant code below:
function SendResponse($orgMsg, $bodyTxt){
$message = [Microsoft.Exchange.WebServices.Data.EmailMessage]::Bind($service, $($orgMsg.Id), $psPropset)
$reply = $message.CreateReply($true)
$reply.BodyPrefix = $bodyTxt
$replyMsg = $reply.Save($drftFolderid.Id)
$replyMsg.From = "my_desired_from#example.com"
$replyMsg.SetExtendedProperty($PidTagReplyRecipientEntries, $byteVal)
$replyMsg.SetExtendedProperty($PidTagReplyRecipientNames, "my_desired_replyto#example.com")
$replyMsg.Update([Microsoft.Exchange.WebServices.Data.ConflictResolutionMode]::AlwaysOverwrite)
$replyMsg.SendAndSaveCopy($sentFolderid.Id)
}
function convert-fromhex {
process
{
$_ -replace '^0x', '' -split "(?<=\G\w{2})(?=\w{2})" | %{ [Convert]::ToByte( $_, 16 ) }
}
}
# below is hex of string "my_desired_replyto#example.com"
[Byte[]]$byteVal = "6d795f646573697265645f7265706c79746f406578616d706c652e636f6d" | convert-fromhex
$PidTagReplyRecipientEntries = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x004F,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::Binary)
$PidTagReplyRecipientNames = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0050,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String)
$psPropset = new-object Microsoft.Exchange.WebServices.Data.PropertySet([Microsoft.Exchange.WebServices.Data.BasePropertySet]::FirstClassProperties)
$psPropset.Add($PidTagReplyRecipientEntries)
$psPropset.Add($PidTagReplyRecipientNames)
Does anyone know how this might be accomplished?
No clue why you were downvoted, but there does appear to be an EmailMessage.ReplyTo property in the Microsoft.Exchange.WebServices.Data.EmailMessage class. I can't tell if it's read-only, however. Looks like it might be.
As far as I know you can't. Replyto is read-only property. I've been trying to use 'ImpersonatedUserId' but it seems a little clunky (read I can't getting it working). However I did find that if you have impersonation permissions then you can set Fromproperty and it will send. I understand this might not be what you're looking for but it will get the email to come from the right place.
Microsoft.Exchange.WebServices.Data.EmailMessage.ReplyTo is a read-only EmailAddressCollection deriving from IEnumerable. You don't instantiate it. It's auto instantiated when you create your EmailMessage, and all you need to do is to add your reply-to email address(es) to it (sorry not PowerShell but C# code):
message.ReplyTo.Add(new EmailAddress("someReply-ToEmailAddress#email.com");
message.ReplyTo.Add(new EmailAddress("anotherReply-ToEmailAddress#email.com");

Open outlook's new email dialog with prefilled information from powershell

I'm trying to write a powershell script (which will run periodicaly) for opening the new email windows of outlook with "to", "subject" and "body" filled with some data.
I found a way to send mails from powershell but you have to send it from powershell. this doesn't fit the need because I have to edit the body of the mail.
$Outlook = New-Object -ComObject Outlook.Application
$Mail = $Outlook.CreateItem(0)
$Mail.To = "random.dude#email.com"
$Mail.Subject = "data for Subject"
$Mail.Body ="Example of body..."
$Mail.Send()
Basicaly what i need is a $Mail.Show() wich will open a new e-mail popup with the data pre-filled
powershell is not a requirement, it just seams able to manipulate outlook so I tried with it.
thanks to this thread, the $Mail.Show() is actually $Mail.Display()
I know this is a little late however if you add the following to your script removing your $Mail.Send() line it should open the email ready for editing:
$mail.save()
$inspector = $mail.GetInspector
$inspector.Display()