set reply-to address in outgoing email EWS

Viewed 2967

Running Exchange 2013

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.

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

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?

5 Answers

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.

C# code is something like this:

// 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. 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 = 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.

The ReplyTo property is a readonly EmailAddressCollection that can be cleared, and added to.

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