Reputation: 4623
I am trying to attach an original email as an attachment to a new email, which will be sent out.
I met the following error:
Message : You must load or assign this property before you can read its value. Inner Exception : Stack Trace : at Microsoft.Exchange.WebServices.Data.PropertyBag.get_Item(PropertyDefinition propertyDefinition) at Microsoft.Exchange.WebServices.Data.EmailMessage.get_Sender() at Service1.Email.SendUpdateEmail(EmailMessage email, String caseNumber, String autoDiscoverUrl) in C:\trunk\SRC\Email.cs:line 160
Below is my code:
public static void SendUpdateMail(EmailMessage email, string caseNumber, string autoDiscoverUrl)
{
ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
service.UseDefaultCredentials = true;
service.Credentials = CredentialCache.DefaultNetworkCredentials;
try
{
service.AutodiscoverUrl(autoDiscoverUrl);
EmailMessage mail = new EmailMessage(service);
EmailMessage OriginalEmail = email;
PropertySet psPropset = new PropertySet(BasePropertySet.IdOnly);
psPropset.Add(ItemSchema.MimeContent);
psPropset.Add(ItemSchema.Subject);
OriginalEmail.Load(psPropset);
ItemAttachment Attachment = mail.Attachments.AddItemAttachment<EmailMessage>();
Attachment.Item.MimeContent = OriginalEmail.MimeContent;
ExtendedPropertyDefinition PR_Flags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
Attachment.Item.SetExtendedProperty(PR_Flags, "1");
Attachment.Name = OriginalEmail.Subject;
mail.Subject = "Case Created";
mail.ToRecipients.Add(OriginalEmail.Sender);
mail.SendAndSaveCopy();
}
catch (Exception ex)
{
LogFile.AppendError(ex);
throw ex;
}
}
I would like to seek some advice. Thank you.
Upvotes: 0
Views: 840
Reputation: 22032
In these lines you have loaded a very restricted property set into OriginalEmail
PropertySet psPropset = new PropertySet(BasePropertySet.IdOnly);
psPropset.Add(ItemSchema.MimeContent);
psPropset.Add(ItemSchema.Subject);
OriginalEmail.Load(psPropset);
Then you try to access the Sender address (which is the property the error is telling you its having a problem with)
mail.ToRecipients.Add(OriginalEmail.Sender);
Because you haven't included the Sender in the PropertySet your getting that error so all you should need to do is add EmailMessageSchema.Sender to it eg
PropertySet psPropset = new PropertySet(BasePropertySet.IdOnly);
psPropset.Add(ItemSchema.MimeContent);
psPropset.Add(ItemSchema.Subject);
psPropset.Add(EmailMessageSchema.Sender);
OriginalEmail.Load(psPropset);
Upvotes: 1