0

I am working on a PowerShell script where I am sending mail to one account but want to set replyto address to some other email address.

 $eMail = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $exchService
$PidTagReplyRecipientEntries = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x004F,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);  
$PidTagReplyRecipientNames = new-object Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition(0x0050,[Microsoft.Exchange.WebServices.Data.MapiPropertyType]::String);  
$eMail.SetExtendedProperty($PidTagReplyRecipientEntries,$ReplyTo);
            $eMail.SetExtendedProperty($PidTagReplyRecipientNames,"RajniKant"); 

Till now I have used this, but it is not working.

Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition I also want to know what is 1st parameter in above constructor and where to find it. Code reference: set reply-to address in outgoing email EWS

Abhi619
  • 47
  • 1
  • 1
  • 7
  • 1
    what about it is not working? are you receiving errors? – ShanayL Oct 04 '17 at 13:53
  • It is throwing : "ErrorMessage": "Exception calling \"SendAndSaveCopy\" with \"0\" argument(s): \"The extended property attribute combination is invalid. while sending mail. – Abhi619 Oct 05 '17 at 05:21

1 Answers1

0

I have solved it by myself and have changed a bit in above referred link code to use with Powershell:

function LoadExtendedHexStrClass(){
$MyCode = @"
using System;
using System.Globalization;
using System.Text;


namespace ExtProp
{
public class BinaryConfig
{
    public static String GenerateFlatList(String SMTPAddress, String DisplayName, String ProviderName)
    {
        String abCount = "01000000";
        String AddressId = GenerateOneOff(SMTPAddress, DisplayName,ProviderName);
        return abCount + BitConverter.ToString(INT2LE((AddressId.Length / 2) + 4)).Replace("-", "") + BitConverter.ToString(INT2LE(AddressId.Length / 2)).Replace("-", "") + AddressId;
    }

    internal static String GenerateOneOff(String SMTPAddress, String DisplayName, String ProviderName)
    {
        String Flags = "00000000";
        String ProviderNameHex = BitConverter.ToString(UnicodeEncoding.Unicode.GetBytes(ProviderName)).Replace("-", "");
        String ProviderUid = ProviderNameHex;
        String Version = "0000";
        String xFlags = "0190";
        String DisplayNameHex = BitConverter.ToString(UnicodeEncoding.Unicode.GetBytes(DisplayName + "\0")).Replace("-", "");
        String SMTPAddressHex = BitConverter.ToString(UnicodeEncoding.Unicode.GetBytes(SMTPAddress + "\0")).Replace("-", "");
        String AddressType = BitConverter.ToString(UnicodeEncoding.Unicode.GetBytes("SMTP" + "\0")).Replace("-", "");
        return Flags + ProviderUid + Version + xFlags + DisplayNameHex + AddressType + SMTPAddressHex;
    }
    internal static byte[] INT2LE(int data)
    {
        byte[] b = new byte[4];
        b[0] = (byte)data;
        b[1] = (byte)(((uint)data >> 8) & 0xFF);
        b[2] = (byte)(((uint)data >> 16) & 0xFF);
        b[3] = (byte)(((uint)data >> 24) & 0xFF);
        return b;
    }
    public static byte[] ConvertHexStringToByteArray(string hexString)
    {
        if (hexString.Length % 2 != 0)
        {
            throw new ArgumentException(String.Format(CultureInfo.InvariantCulture, "The binary key cannot have an odd number of digits: {0}", hexString));
        }

        byte[] HexAsBytes = new byte[hexString.Length / 2];
        for (int index = 0; index < HexAsBytes.Length; index++)
        {
            string byteValue = hexString.Substring(index * 2, 2);
            HexAsBytes[index] = byte.Parse(byteValue, NumberStyles.HexNumber, CultureInfo.InvariantCulture);
        }

        return HexAsBytes;
    }
}
"@

Add-Type  -TypeDefinition $MyCode -Language CSharp
}

and used it as below:

$eMail = New-Object -TypeName Microsoft.Exchange.WebServices.Data.EmailMessage -ArgumentList $exchService
 if($ReplyTo -ne ""){
            LoadExtendedHexStrClass
            $UserProperties=Get-kUserProfileProperties $ReplyTo
            $DisplayName =$UserProperties.PreferredName
            $FlatList= [ExtProp.BinaryConfig]::GenerateFlatList($ReplyTo,$DisplayName, $CurrentUserEmail);

            $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);  
            $eMail.SetExtendedProperty($PidTagReplyRecipientEntries,[ExtProp.BinaryConfig]::ConvertHexStringToByteArray($FlatList));
            $eMail.SetExtendedProperty($PidTagReplyRecipientNames,$DisplayName); 
         }
       $eMail.SendAndSaveCopy()     

Here $ReplyTo is email address of user where I want to send Reply Email.

Abhi619
  • 47
  • 1
  • 1
  • 7