特定の交換メールボックスを監視する簡単なコンソールアプリを作成中です。特定の条件を満たすメールを受信すると、アプリはXMLファイルの添付ファイルをダウンロードし、メールをアーカイブします。
EWS OKに接続し、すべての電子メールをループできましたが、添付ファイルにアクセスするために使用できるEmailMessageオブジェクトを作成するのに苦労しています。
以下のサンプルコードでは、EmailMessage message = EmailMessage.Bind(...)
行はエラーなしで実行されますが、有効なメッセージを返さないため、プロパティまたはメソッドにアクセスすると、エラーが発生します:'オブジェクト参照がオブジェクトのインスタンスに設定されていません'。
私はEWSはもちろんC#を初めて使用するので、どこから始めればよいかわからないのです...
コードスニペット:
public static void FindItems()
{
try
{
ItemView view = new ItemView(10);
view.OrderBy.Add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
view.PropertySet = new PropertySet(
BasePropertySet.IdOnly,
ItemSchema.Subject,
ItemSchema.DateTimeReceived);
findResults = service.FindItems(
WellKnownFolderName.Inbox,
new SearchFilter.SearchFilterCollection(
LogicalOperator.Or,
new SearchFilter.ContainsSubstring(ItemSchema.Subject, "Sales Enquiry")),
view);
log2.LogInfo("Total number of items found: " + findResults.TotalCount.ToString());
foreach (Item item in findResults)
{
log2.LogInfo(item.Id);
EmailMessage message = EmailMessage.Bind(service, item.Id, new PropertySet(BasePropertySet.IdOnly, ItemSchema.Attachments));
Console.WriteLine(message.Subject.ToString());
if (message.HasAttachments && message.Attachments[0] is FileAttachment)
{
FileAttachment fileAttachment = message.Attachments[0] as FileAttachment;
fileAttachment.Load("C:\\temp\\" + fileAttachment.Name);
fileAttachment.Load();
Console.WriteLine("FileName: " + fileAttachment.FileName);
}
}
}
catch (Exception ex)
{
log2.LogError(ex.InnerException);
}
}
添付ファイルにアクセスするための私のコードはMSDNから直接のものなので、そこにあることを願っています...何かアイデアはありますか?