C# 设置发送电子邮件EWS中的回复地址

C# 设置发送电子邮件EWS中的回复地址,c#,exchangewebservices,C#,Exchangewebservices,运行Exchange 2013 我在一个c#服务中使用EWS,该服务通过服务帐户发送电子邮件 我想让电子邮件有一个不同于发送帐户的回复地址,一个通讯组列表地址 我该怎么做?EmailMessage.ReplyTo字段为只读 代码: 尽管我没有使用powershell,但只有其他线程似乎是相关的:您可以使用PidTagReplyRecipientEnterties扩展属性来实现这一点,例如 EmailMessage DifferentReplyTo = new EmailMessa

运行Exchange 2013

我在一个c#服务中使用EWS,该服务通过服务帐户发送电子邮件

我想让电子邮件有一个不同于发送帐户的回复地址,一个通讯组列表地址

我该怎么做?
EmailMessage.ReplyTo
字段为只读

代码:


尽管我没有使用powershell,但只有其他线程似乎是相关的:

您可以使用PidTagReplyRecipientEnterties扩展属性来实现这一点,例如

        EmailMessage DifferentReplyTo = new EmailMessage(service);
        DifferentReplyTo.Subject = "test";
        DifferentReplyTo.ToRecipients.Add("destination@domain.com");
        DifferentReplyTo.Body = new MessageBody("test");           
        ExtendedPropertyDefinition PidTagReplyRecipientEntries = new ExtendedPropertyDefinition(0x004F, MapiPropertyType.Binary);
        ExtendedPropertyDefinition PidTagReplyRecipientNames = new ExtendedPropertyDefinition(0x0050, MapiPropertyType.String);
        DifferentReplyTo.SetExtendedProperty(PidTagReplyRecipientEntries, ConvertHexStringToByteArray(GenerateFlatList("departmentdg@domain.com", "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;
    }
EmailMessage DifferentReplyTo=新的EmailMessage(服务);
DifferentReplyTo.Subject=“test”;
不同的收件人地址添加(“destination@domain.com");
DifferentReplyTo.Body=新消息体(“测试”);
ExtendedPropertyDefinition PidTagReplyRecipientEnterties=新的ExtendedPropertyDefinition(0x004F,MapPropertyType.Binary);
ExtendedPropertyDefinition PidTagReplyRecipientNames=新的ExtendedPropertyDefinition(0x0050,MapPropertyType.String);
DifferentReplyTo.SetExtendedProperty(PidTagReplyRecipientEnterties,ConvertHexStringToByteArray(GenerateFlatList('departmentdg@domain.com","jc"),;
DifferentReplyTo.SetExtendedProperty(PidTagReplyRecipientNames,“jc”);
发送和保存复制()的不同路径;
内部静态字符串GenerateFlatList(字符串SMTPAddress,字符串DisplayName)
{
字符串abCount=“01000000”;
字符串地址ID=GenerateOneOff(SMTPAddress,DisplayName);
返回abCount+BitConverter.ToString(INT2LE((AddressId.Length/2)+4)).Replace(“-”,”“)+BitConverter.ToString(INT2LE(AddressId.Length/2)).Replace(“-”,”“)+AddressId;
}
内部静态字符串GenerateOneOff(字符串SMTPAddress,字符串DisplayName)
{
字符串标志=“00000000”;
字符串提供程序uid=“812B1FA4BEA310199D6E00DD010F5402”;
字符串版本=“0000”;
字符串xFlags=“0190”;
字符串DisplayNameHex=BitConverter.ToString(Unicode编码.Unicode.GetBytes(DisplayName+“\0”))。替换(“-”,”);
字符串SMTPAddressHex=BitConverter.ToString(Unicode编码.Unicode.GetBytes(SMTPAddress+“\0”))。替换(“-”,”);
String AddressType=BitConverter.ToString(Unicode编码.Unicode.GetBytes(“SMTP”+“\0”))。替换(“-”,”);
返回标志+ProviderUid+Version+xFlags+DisplayNameHex+AddressType+SMTPAddressHex;
}
内部静态字节[]INT2LE(int数据)
{
字节[]b=新字节[4];
b[0]=(字节)数据;
b[1]=(字节)((uint)数据>>8)和0xFF);
b[2]=(字节)((uint)数据>>16)和0xFF);
b[3]=(字节)((uint)数据>>24)和0xFF);
返回b;
}
内部静态字节[]ConvertHexStringToByteArray(字符串hexString)
{
如果(hexString.Length%2!=0)
{
抛出新的ArgumentException(String.Format(CultureInfo.InvariantCulture,“二进制键不能有奇数位数:{0}”,hexString));
}
byte[]HexAsBytes=新字节[hexString.Length/2];
for(int index=0;index
干杯 格伦为我工作:

EmailAddressCollection emailAddressCollection = new EmailAddressCollection();
emailAddressCollection.add(new EmailAddress("ReplayToEmail@gmail.com"));
emailMessage.getPropertyBag().setObjectFromPropertyDefinition(EmailMessageSchema.ReplyTo, emailAddressCollection);

我通过在
EmailMessage replyMessage
对象上加载
ToRecipients
CcRecipients
属性来解决这个问题

C#代码是这样的:

// somehow get a hold of an ExchangeService object
var message = service.Bind(service, mail.Id);
var replyMessage = message.CreateReply(replyToAll: true);

replyMessage.Load(new PropertySet() { EmailMessageSchema.ToRecipients, EmailMessageSchema.CcRecipients });

replyMessage.ToRecipients.Add("some_email@email.com");
replyMessage.CcRecipients.Add("some_email_cc@email.com");

replyMessage.Send();

当然,如果您不打算使用Cc,则不必加载它。这允许您附加新地址,或清除收件人并添加任何您想要的内容。

有一个更简单的解决方案:使用
响应消息

ResponseMessage responseMessage = originalEmail.createReply(isReplyAll);
responseMessage.getToRecipients().addEmailRange(...email address...);
responseMessage.sendAndSaveCopy();

因此,有一个问题:
convertHextStringToByteArray
方法不存在于代码中。也许你的意思是这样的?使用该问题中的代码添加
convertHextStringToByteArray
方法并尝试您的代码似乎不会改变回复地址,至少在我们的员工outlook客户端上不会。虽然我添加了我使用的转换方法,但它确实可以编译和运行,如果您检查传输头,您会看到什么?它对我来说测试正常,您可以做的一件事是尝试在outlook中执行相同的操作,然后使用Mapi编辑器比较outlook和代码之间的两个值。好的,因此我编译并执行了代码,正在MS的Mapi编辑器中查看从Exchange收到的电子邮件。我以前没用过这个编辑器,所以仍然掌握了它的窍门,它列出了属性和值。列表中未显示属性
PR\u REPLY\u RECIPIENT\u条目。我还从链接的文档中看到,
如果设置了此属性或PR\u REPLY\u RECIPIENT\u NAMES属性,则还必须设置另一个属性。
无论哪种方式,它似乎都不起作用。顺便说一句,我们运行的是exchange 2013,我运行的是Outlook 2016。我已将PidTagReplyRecipientNames添加到代码示例中,它只是一个字符串属性。您需要查看SentItems文件夹中的邮件副本以查看这些属性,因为这些是信封属性,它们不会出现在收到的邮件上。为此,您应该只查看传输头,看看是否可以看到回复头。我使用的是Office365(因此Exchange 2016)和Outlook 2013,但它应该与Exchange 2013EmailAddressCollection内部构造函数相同
ResponseMessage responseMessage = originalEmail.createReply(isReplyAll);
responseMessage.getToRecipients().addEmailRange(...email address...);
responseMessage.sendAndSaveCopy();