Get original message with headers when forwarding message before sending in EWS
-
13-11-2019 - |
Question
I'm trying to provide a way to forward emails, just as we do in Outlook. When the user clicks forward, it opens a form with the original message and header information on top, giving the user an opportunity to modify the body. To get the message, I do the following.
var item = (EmailMessage)dataGridEmail.SelectedItem;
ResponseMessage forward = item.CreateForward();
textBlockForward.Text = forward.Body;
I get an ServiceObjectPropertyException
with message "You must load or assign this property before you can read its value". If I try to load the value of the body property before accessing, I get NotSupportedException
with message "Specified method is not supported". Is there a way to get the forwarding message before actually sending it?
Solution
Instead of using forward.Body
, you should use item.Body
to populate your textBlockForward
control. See Forwarding Email Messages using EWS on MSDN for more details.
var item = (EmailMessage)dataGridEmail.SelectedItem;
ResponseMessage forward = item.CreateForward();
textBlockForward.Text = item.Body; // needs to come from original message source
forward.BodyPrefix = "new body contents"; // prepended body content
OTHER TIPS
I am note sure if anyone could solve this. This is what i did.
ResponseMessage responseMessage = message.createForward();
// message is an EmailMessage object
responseMessage.setBodyPrefix(body);
responseMessage.save(WellKnownFolderName.Drafts);
EmailMessage saved = responseMessage.save();
saved.load(new
PropertySet(BasePropertySet.FirstClassProperties,ItemSchema.Body));
MessageBody messageBody = saved.getBody();
// do something with messageBody`enter code here`
//System.out.println(saved.getBody().toString());
saved.sendAndSaveCopy();