I am building a small client that can automatically reply to some emails. I would like to add a custom internet header to these replies.
What I tried to do is to set the extended property to the email I receive, and use the EmailMessage.Reply
method.
But it does not work. I also tried to use the EmailMessage.CreateReply
. However, it creates a ResponseMessage
object that has no SetExtendedProperty
method.
This the relevant part of the code :
private static readonly ExtendedPropertyDefinition _redFlag = new ExtendedPropertyDefinition(DefaultExtendedPropertySet.InternetHeaders, "X-RED", MapiPropertyType.String);
private static readonly PropertySet _customHeaders = new PropertySet(BasePropertySet.FirstClassProperties, _redFlag);
/* ... some code to connect to Exchange Service ... */
EmailMessage email = EmailMessage.Bind(_service, id, _customHeaders);
email.SetExtendedProperty(_redFlag, "test");
email.Reply(new MessageBody(answer), false);
You first need to save the ResponseMessage
in the Drafts
folder via the Save()
method and then you can obtain an instance of the EmailMessage
. This is the same pattern used to add attachments to message replies.
var message = (EmailMessage) Item.Bind(service, new ItemId(uniqueId), PropertySet.FirstClassProperties);
var reply = message.CreateReply(false);
reply.BodyPrefix = "Response text goes here";
var replyMessage = reply.Save(WellKnownFolderName.Drafts); // default is drafts folder - this is explicit
replyMessage.SetExtendedProperty(_redFlag, "test");
replyMessage.Update(ConflictResolutionMode.AlwaysOverwrite);
replyMessage.SendAndSaveCopy();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With