Skip to content

Instantly share code, notes, and snippets.

@nanonanomachine
Created July 31, 2015 11:53
Show Gist options
  • Save nanonanomachine/28aa9e45dd0d6897e11a to your computer and use it in GitHub Desktop.
Save nanonanomachine/28aa9e45dd0d6897e11a to your computer and use it in GitHub Desktop.
ExchangeをC#で操作するマン
using Microsoft.Exchange.WebServices.Data;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace OutlookTest
{
internal class Program
{
private static void Main(string[] args)
{
// init
var service = new ExchangeService(ExchangeVersion.Exchange2010_SP2)
{
UseDefaultCredentials = false,
Credentials = new WebCredentials("【メールアドレスいれてね!】", "【パスワード入れてね!】")
};
service.AutodiscoverUrl("【メールアドレスいれてね!】", url => true);
// Set specific duration
var startDate = DateTime.Now.AddDays(-60);
var endDate = startDate.AddDays(30);
// Mail
// Get all folders
var mailFolders = GetAllMailFolders(service);
mailFolders.ToList().ForEach(f =>
Console.WriteLine
(
new StringBuilder().
AppendLine(string.Format("FolderId: {0}", f.Id)).
AppendLine(string.Format("Name: {0}", f.DisplayName))
));
// Get mails
var maiItems = mailFolders.Select(f => GetMailItems(service, f, 10, startDate, endDate));
maiItems.ToList().ForEach(i => i.ToList().ForEach(m =>
Console.WriteLine
(
new StringBuilder().
AppendLine(string.Format("Subject: {0}", m.Item.Subject)).
AppendLine(string.Format("Date:{0}", m.Item.DateTimeReceived.ToString())).
AppendLine(string.Format("From:{0}", m.Item.LastModifiedName)).
AppendLine(string.Format("Body:{0}", m.Item.Body)
))));
// Get appointments in calendar
var appointments = GetAppointments(service, 10, startDate, endDate);
appointments.ToList().ForEach(a =>
Console.WriteLine
(
new StringBuilder().
AppendLine(string.Format("Subject: {0}", a.Subject.ToString())).
AppendLine(string.Format("Start: {0}", a.Start.ToString())).
AppendLine(string.Format("End: {0}", a.End.ToString()))
));
}
/// <summary>
/// Get all mail folders
/// refs: https://msdn.microsoft.com/en-us/library/office/dd633627(v=exchg.80).aspx
/// </summary>
/// <param name="service"></param>
private static FindFoldersResults GetAllMailFolders(ExchangeService service)
{
var view = new FolderView(1000);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);
var filter = new SearchFilter.IsGreaterThan(FolderSchema.TotalCount, 0);
view.Traversal = FolderTraversal.Deep;
return service.FindFolders(WellKnownFolderName.Root, filter, view);
}
/// <summary>
/// Get mail items for a specific duration
/// refs: http://stackoverflow.com/a/11363175
/// </summary>
/// <param name="service"></param>
/// <param name="folder"></param>
/// <param name="count"></param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
private static IEnumerable<GetItemResponse> GetMailItems(ExchangeService service, Folder folder, int count, DateTime startDate, DateTime endDate)
{
var filter = new SearchFilter.SearchFilterCollection
(
LogicalOperator.And,
new SearchFilter.IsGreaterThanOrEqualTo(ItemSchema.DateTimeReceived, startDate),
new SearchFilter.IsLessThan(ItemSchema.DateTimeReceived, endDate)
);
var mailFolder = Folder.Bind(service, folder.Id);
var findResults = mailFolder.FindItems(filter, new ItemView(count));
if (findResults.Count() == 0) return Enumerable.Empty<GetItemResponse>();
return service.BindToItems
(
findResults.Select(i => i.Id),
new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From, EmailMessageSchema.ToRecipients)
);
}
/// <summary>
/// Get appointments in calendar for a specific duration
/// refs: http://anopara.matrix.jp/2014/05/13/coutlook%E3%81%8B%E3%82%89%E4%BA%88%E5%AE%9A%E8%A1%A8%E3%83%87%E3%83%BC%E3%82%BF%E3%81%A8%E3%81%8B%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B/
/// </summary>
/// <param name="service"></param>
/// <param name="count"></param>
/// <param name="startDate"></param>
/// <param name="endDate"></param>
/// <returns></returns>
private static FindItemsResults<Appointment> GetAppointments(ExchangeService service, int count, DateTime startDate, DateTime endDate)
{
var calendar = CalendarFolder.Bind(service, WellKnownFolderName.Calendar, new PropertySet());
var cView = new CalendarView(startDate, endDate, count)
{
PropertySet = new PropertySet(AppointmentSchema.Subject, AppointmentSchema.Start, AppointmentSchema.End)
};
return calendar.FindAppointments(cView);
}
}
}
@nanonanomachine
Copy link
Author

ExchangeのDLLは

C:\Program Files (x86)\Microsoft\Exchange\Web Services\2.0あたりに入っています。

参考:

http://anopara.matrix.jp/2014/05/13/coutlook%E3%81%8B%E3%82%89%E4%BA%88%E5%AE%9A%E8%A1%A8%E3%83%87%E3%83%BC%E3%82%BF%E3%81%A8%E3%81%8B%E3%82%92%E5%8F%96%E5%BE%97%E3%81%99%E3%82%8B/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment