在传出电子邮件EWS中设置回复地址 [英] set reply-to address in outgoing email EWS

查看:103
本文介绍了在传出电子邮件EWS中设置回复地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

运行Exchange 2013

我在c#服务中使用EWS,该服务从服务帐户发送电子邮件.

I am using EWS in a c# service that sends emails from a service account.

我想让电子邮件的回复地址与发送帐户的地址不同,即通讯组列表地址.

I want to have the emails have a reply-to address different than the sending account, a distribution list address.

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

How can I do this? The EmailMessage.ReplyTo field is read only.

代码:

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

虽然我没有使用Powershell,但只有其他看起来相关的线程:

Only other thread that seemed related, though I'm not using powershell: How do you set a message Reply-To address using EWS Managed API?

推荐答案

您可以使用PidTagReplyRecipientEntries扩展属性 https://msdn.microsoft.com/en-us/library/office/cc815710.aspx 来做到这一点,例如

You can use the PidTagReplyRecipientEntries extended property https://msdn.microsoft.com/en-us/library/office/cc815710.aspx to do that eg

        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;
    }

欢呼 格伦

这篇关于在传出电子邮件EWS中设置回复地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆