1

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?

runaboutfence
  • 120
  • 3
  • 9

3 Answers3

0

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.

Bacon Bits
  • 30,782
  • 5
  • 59
  • 66
  • Thanks for the reply and vote! The `EmailMessage.ReplyTo` property in the EmailMessage class is read-only. I think this property is set by an Exchange transport provider when the extended properties `PidTagReplyRecipientEntries` and `PidTagReplyRecipientNames` are properly configured. – runaboutfence Dec 17 '14 at 17:00
0

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.

jolySoft
  • 2,948
  • 2
  • 30
  • 34
0

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");
Yan F.
  • 1,554
  • 2
  • 15
  • 16