简体   繁体   English

在传出 email EWS 中设置回复地址

[英]set reply-to address in outgoing email EWS

Running Exchange 2013运行 Exchange 2013

I am using EWS in a c# service that sends emails from a service account.我在 c# 服务中使用 EWS,该服务从服务帐户发送电子邮件。

I want to have the emails have a reply-to address different than the sending account, a distribution list address.我想让电子邮件有一个不同于发送帐户的回复地址,一个分发列表地址。

How can I do this?我怎样才能做到这一点? The EmailMessage.ReplyTo field is read only. EmailMessage.ReplyTo字段是只读的。

Code:代码:

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

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?只有其他似乎相关的线程,尽管我没有使用 powershell: How do you set a message Reply-To address using EWS Managed API?

You can use the PidTagReplyRecipientEntries extended property https://msdn.microsoft.com/en-us/library/office/cc815710.aspx to do that eg您可以使用 PidTagReplyRecipientEntries 扩展属性https://msdn.microsoft.com/en-us/library/office/cc815710.aspx来做到这一点,例如

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

Cheers Glen干杯格伦

that work for me :这对我有用:

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

I solved this by loading the ToRecipients and CcRecipients properties on the EmailMessage replyMessage object.我通过在EmailMessage replyMessage对象上加载ToRecipientsCcRecipients属性解决了这个问题。

C# code is something like this: 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();

Of course, if you don't plan on working with Cc, you don't have to load it.当然,如果您不打算使用 Cc,则不必加载它。 This allows you to append new addresses, or clear the recipients and add whatever you want.这允许您附加新地址,或清除收件人并添加您想要的任何内容。

There is a simpler solution: use ResponseMessage :有一个更简单的解决方案:使用ResponseMessage

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

I'm sure it's due to this question being 6 years old, and the library has been updated since.我确定这是因为这个问题已有 6 年历史,并且此后图书馆已经更新。

The ReplyTo property is a readonly EmailAddressCollection that can be cleared, and added to. ReplyTo属性是一个只读的 EmailAddressCollection,可以清除和添加。

var emailSend = new EmailMessage(service)
emailSend.ToRecipients.Add("recipient@example.com");

// change the reply-to
if(email.FromAddress.ToLower() != "noreply@example.com"){
    emailSend.ReplyTo.Clear();
    emailSend.ReplyTo.Add(new EmailAddress() { Address = "replyto@example.com" });
}
                                
emailSend.Save();
emailSend.Send(); 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM