Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- EWS Reading Email [closed]
- internal static List<string> ReadMailContent()
- {
- List<string> mailContentList = new List<string>();
- string MailAddress = "yourusername@yourmail.com";
- // mail username
- string Username = "yourusername";
- // mail password
- string Password = "password";
- // domain
- // who doesn't know what it is: in command line enter 'whoami' , result x/y: (x-domain, y-username)
- string domain = "domain";
- // mail filter subject to read
- string mailFilterSubject = "possible subject to looking for";
- //init service with exchange version
- ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
- //find the mail inbox
- service.AutodiscoverUrl(MailAddress);
- //put credentials
- service.Credentials = new WebCredentials(Username, Password, domain);
- //filter all unread message with subject "possible subject to looking for"
- // here I entered 2 cases: 1.IsEqualTo, 2.ContainsSubstring (it's optional)
- SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
- new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false),
- new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, mailFilterSubject));
- //get messages with filter
- FindItemsResults<Item> findMails = service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(int.MaxValue));
- foreach (Item item in findMails.Items)
- {
- //init mail properties
- ExtendedPropertyDefinition htmlBodyProperty = new ExtendedPropertyDefinition(0x1013, MapiPropertyType.Binary);
- PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties, htmlBodyProperty);
- //load message with its properties
- EmailMessage message = EmailMessage.Bind(service, item.Id, propertySet);
- //mark found mails as read
- message.IsRead = true;
- message.Update(ConflictResolutionMode.AlwaysOverwrite);
- //add mail content to list
- mailContentList.Add(message.Body.Text); // also message.Subject, message.Body.Text
- }
- return mailContentList;
- }
Add Comment
Please, Sign In to add comment