I have the following code in place, and it works:
private void OnEvent(object sender, NotificationEventArgs args)
{
StreamingSubscription sub = args.Subscription;
foreach (NotificationEvent notification in args.Events)
{
switch (notification.EventType)
{
case EventType.NewMail:
if (notification is ItemEvent)
{
ItemEvent item = (ItemEvent)notification;
EmailMessage message = EmailMessage.Bind(service, item.ItemId);
string fAddress = message.From.Address;
string subject = message.Subject;
string body = message.Body.Text;
string tAddress = message.ToRecipients[0].Address;
//and so on...
}
break;
}
}
}
However, if I try to set "body" equal to UniqueBody like this...
string body = message.UniqueBody.Text;
That errors out saying, "You must load or assign this property before you can read its value." I was hoping UniqueBody would work out-of-the-box, meaning I wouldn't have to parse a new email to grab the new details I care about. I'm assuming 开发者_JAVA百科there is something easy I'm missing. Any ideas?
When you bind the ItemId
you wish to receive you need to be explicit in which properties you want.
For example,
var propertySet = new PropertySet(ItemSchema.UniqueBody);
var email = EmailMessage.Bind(service, item.ItemId, propertySet);
The PropertySet
class has an overload that includes params[]
so you're free to include/exclude a number of additional properties. Simply look through the ItemSchema
enum and select the ones you want.
This is what I ended up using:
PropertySet pSet = new PropertySet(new[]{ ItemSchema.UniqueBody });
pSet.RequestedBodyType = BodyType.Text;
pSet.BasePropertySet = BasePropertySet.FirstClassProperties;
精彩评论