Can't get senders email address from read receipts - powershell

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

Related

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()
}
}

Create AD Object - Not Exchange

I am currently trying to create an Active Directory contact object in a specific OU in our Active Directory. I am not looking at using Exchange PowerShell. I would like to do this via normal PowerShell directly in to AD.
I have looked online and found that I can create a contact using the below command, this creates it in a specific OU.
New-ADObject -Name SaraDavisSGTContact3 -Type contact -Path "OU=SGTestOU,OU=Contacts,DC=example,DC=Example,DC=local"
How would I also get it to add other attributes such as mail (email address), first name, surname, etc.? I tried -Mail example.com, etc., but this didn't work.
Please bear in mind I will eventually try and get this to read from a CSV to do in bulk, so the simplier the code the better.
Use the -OtherAttributes Parameter to add additional attributes.
See New-ADObject MSDN Documentation
New-ADObject -name SaraDavisSGTContact3 -Type Contact -path "OU=SGTestOU,OU=Contacts,DC=example,DC=Example,DC=local" -OtherAttributes #{
'mail'="sara#gmail.com";
'proxyAddresses'="sara#gmail.com";
'givenName'="Sara";
'sn'="Davis";
'displayname'="Sara Davis"
}

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

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.

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");

New LDAP User with Powershell

I have the challenge to create new LDAP Users with a Powershell Script.
I have googled a lot but I found no good results...
This is my Code to get Users from the LDAP...
$authenticationType = [System.DirectoryServices.AuthenticationTypes]::ServerBind
$objSearcherRoot = New-Object System.DirectoryServices.DirectoryEntry("LDAP://***.local.com:777/ou=user,o=company", "uid=testuser,ou=system,o=company", "password" , $authenticationType)
$objSearcher = New-Object System.DirectoryServices.DirectorySearcher
$objSearcher.SizeLimit= 0
$objSearcher.PageSize = 1000
$objSearcher.SearchRoot = $objSearcherRoot
$objSearcher.Filter = "cn=$cn"
$result = $objSearcher.FindAll()
My Problem is, I don't know how to insert a new LDAP User (not Active Directory)
It would be really nice if someone could help me... ;)
Thanks
Yes, it's possible, I've done it. You need to bind to the LDAP server using a System.DirectoryServices.Protocols.LdapConnection object (let's say $c) and then create a System.DirectoryServices.Protocols.AddRequest object and populate its attributes (I'm only showing a couple in this example):
[System.Reflection.Assembly]::LoadWithPartialName("System.DirectoryServices.Protocols")
[System.Reflection.Assembly]::LoadWithPartialName("System.Net")
$c = New-Object -TypeName System.DirectoryServices.Protocols.LdapConnection -ArgumentList "***.local.com:777,uid=testuser,ou=system,o=company", "password" , $authenticationType"
$c.Bind()
$r = New-Object -TypeName System.DirectoryServices.Protocols.AddRequest
$r.DistinguishedName = "uid= xxxx, ou=user, o=company"
$r.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "objectclass",#("top","organizationalPerson","person","inetorgperson","inetuser","mailrecipient","pwmuser","posixAccount"))) | Out-Null
$r.Attributes.Add((New-Object -TypeName System.DirectoryServices.Protocols.DirectoryAttribute -ArgumentList "cn",($FirstName+" "+$LastName))) | Out-Null
Then just send the request:
$c.SendRequest($r)
LDAP does not support "inserts", but supports "adds". The LDAP client must create an entry and transmit that entry to the directory server using the ADD request. The server returns an ADD result to the LDAP client which contains information about the success or failure of the ADD request. So, check the documentation for information on transmitting an ADD request to the directory server and interpreting the subsequent response.
The LDAP client must have permission to ADD an entry (a user in this case). This involves using the BIND request to change the authorization state of the connection to one which permits adding an entry at the designated place in the directory information tree.
Perhaps this link will help.
You say "create new LDAP Users" but you could create AD users and then they would be available Using LDAP.
I used a script from Microsoft to do something similar.
If you look through the code, you should be able to see how they did it. We used their code with a few tweaks to do what we needed.
-jim