【问题标题】:set reply-to address in outgoing email EWS在外发电子邮件 EWS 中设置回复地址
【发布时间】:2016-03-14 13:30:15
【问题描述】:

运行 Exchange 2013

我在从服务帐户发送电子邮件的 c# 服务中使用 EWS。

我希望电子邮件具有与发送帐户不同的回复地址,即分发列表地址。

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

代码:

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:How do you set a message Reply-To address using EWS Managed API?

【问题讨论】:

    标签: c# exchangewebservices


    【解决方案1】:

    您可以使用 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;
        }
    

    干杯 格伦

    【讨论】:

    • 所以,1 个问题:ConvertHexStringToByteArray 方法不存在于您的代码中。也许你的意思是stackoverflow.com/questions/321370/…
    • 使用该问题中的代码添加ConvertHexStringToByteArray 方法并尝试您的代码似乎不会改变对地址的回复,至少在我们的员工前景客户中不会。它确实可以编译并运行
    • 我添加了我使用的转换方法,如果您检查传输标头,您看到了什么?它测试对我来说没问题,你可以做的一件事是尝试在 Outlook 中做同样的事情,然后使用 Mapi 编辑器比较 Outlook 和你的代码之间的两个值。
    • 好的,所以我编译并执行了代码,并且正在查看我在 MS 的 MAPI 编辑器中从 Exchange 收到的电子邮件。我以前没有使用过编辑器,所以仍然掌握它,它列出了属性和值。属性PR_REPLY_RECIPIENT_ENTRIES 未出现在列表中。我还从链接的文档中看到,If either this property or the PR_REPLY_RECIPIENT_NAMES property is set, the other property must be set also. 不管怎样,它似乎还没有工作。顺便说一句,我们正在运行 Exchange 2013,我正在运行 Outlook 2016
    • 我已将 PidTagReplyRecipientNames 添加到代码示例中,它只是一个字符串属性。您需要查看 SentItems 文件夹中的邮件副本以查看这些属性,因为这些是信封属性,它们不会出现在收到的邮件中。为此,您应该只查看传输标头以查看是否可以看到回复标头。我正在使用 Office365(所以 Exchange 2016)和 Outlook 2013,但它应该与 Exchange 2013 相同
    【解决方案2】:

    这对我有用:

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

    【讨论】:

    • EmailAddressCollection 构造函数是内部的
    【解决方案3】:

    我通过在EmailMessage replyMessage 对象上加载ToRecipientsCcRecipients 属性解决了这个问题。

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

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

    【讨论】:

      【解决方案4】:

      有一个更简单的解决方案:使用ResponseMessage:

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

      【讨论】:

        【解决方案5】:

        我确定这是由于这个问题已有 6 年历史,并且该库已更新。

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

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2015-04-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-12-19
          相关资源
          最近更新 更多