簡體   English   中英

通過PowerShell在EWS中設置ReplyTo電子郵件地址

[英]Setting ReplyTo email address in EWS by PowerShell

我正在使用PowerShell腳本,在該腳本中我將郵件發送到一個帳戶,但希望將replyto地址設置為其他電子郵件地址。

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

到目前為止,我已經使用了它,但是它不起作用。

Microsoft.Exchange.WebServices.Data.ExtendedPropertyDefinition我也想知道上面的構造函數中的第一個參數是什么,以及在哪里可以找到它。 代碼參考: 在傳出電子郵件EWS中設置回復地址

我自己解決了它,並在上面提到的鏈接代碼中做了一些更改,以用於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
}

並如下使用:

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

$ ReplyTo是我要發送回復電子郵件的用戶的電子郵件地址。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM