2016-03-14 36 views
1

運行Exchange 2013組回覆地址在傳出電子郵件EWS

我在使用服務帳戶發送電子郵件C#的服務使用EWS。

我希望電子郵件具有與發送帳戶不同的回覆地址,即發送清單地址。

我該怎麼做? EmailMessage.ReplyTo字段是隻讀的。

代碼:這似乎是,雖然我沒有使用PowerShell的關係,

ExchangeService service = new ExchangeService(); 
service.Credentials = EWScredentials; 
service.Url = new Uri(string.Format("https://{0}/EWS/Exchange.asmx", ExchangePath)); 

EmailMessage message = new EmailMessage(service); 
message.ToRecipients.AddRange(receipients); 

//This didn't work 
message.ReplyTo.Clear(); 
message.ReplyTo.Add(replyToAddress); 

message.Subject = subject; 
message.Body = html; 
message.SendAndSaveCopy(); 

只有其他線程:How do you set a message Reply-To address using EWS Managed API?

回答

2

可以使用PidTagReplyRecipientEntries擴展屬性https://msdn.microsoft.com/en-us/library/office/cc815710.aspx做如

 EmailMessage DifferentReplyTo = new EmailMessage(service); 
     DifferentReplyTo.Subject = "test"; 
     DifferentReplyTo.ToRecipients.Add("[email protected]"); 
     DifferentReplyTo.Body = new MessageBody("test");   
     ExtendedPropertyDefinition PidTagReplyRecipientEntries = new ExtendedPropertyDefinition(0x004F, MapiPropertyType.Binary); 
     ExtendedPropertyDefinition PidTagReplyRecipientNames = new ExtendedPropertyDefinition(0x0050, MapiPropertyType.String); 
     DifferentReplyTo.SetExtendedProperty(PidTagReplyRecipientEntries, ConvertHexStringToByteArray(GenerateFlatList("[email protected]", "jc"))); 
     DifferentReplyTo.SetExtendedProperty(PidTagReplyRecipientNames, "jc"); 
     DifferentReplyTo.SendAndSaveCopy(); 

    internal static String GenerateFlatList(String SMTPAddress, String DisplayName) 
    { 
     String abCount = "01000000"; 
     String AddressId = GenerateOneOff(SMTPAddress, DisplayName); 
     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 Flags = "00000000"; 
     String ProviderUid = "812B1FA4BEA310199D6E00DD010F5402"; 
     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; 
    } 
    internal 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; 
    } 

Cheers Glen

+0

今天我會試試這個,謝謝! – Aaron

+1

所以,1個問題:'ConvertHexStringToByteArray'方法不存在於您的代碼中。 perhaphs你的意思是像http://stackoverflow.com/questions/321370/how-can-i-convert-a-hex-string-to-a-byte-array? – Aaron

+1

使用該問題中的代碼添加一個'ConvertHexStringToByteArray'方法,並嘗試您的代碼似乎不會更改回復地址,至少不會在我們的員工Outlook客戶端上。它確實編譯並運行 – Aaron

相關問題