Guest User

Untitled

a guest
Sep 11th, 2018
176
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.32 KB | None | 0 0
  1. EWS Reading Email [closed]
  2. internal static List<string> ReadMailContent()
  3. {
  4. List<string> mailContentList = new List<string>();
  5. string MailAddress = "yourusername@yourmail.com";
  6.  
  7. // mail username
  8. string Username = "yourusername";
  9. // mail password
  10. string Password = "password";
  11. // domain
  12. // who doesn't know what it is: in command line enter 'whoami' , result x/y: (x-domain, y-username)
  13. string domain = "domain";
  14. // mail filter subject to read
  15. string mailFilterSubject = "possible subject to looking for";
  16.  
  17.  
  18. //init service with exchange version
  19. ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
  20.  
  21. //find the mail inbox
  22. service.AutodiscoverUrl(MailAddress);
  23.  
  24. //put credentials
  25. service.Credentials = new WebCredentials(Username, Password, domain);
  26.  
  27. //filter all unread message with subject "possible subject to looking for"
  28. // here I entered 2 cases: 1.IsEqualTo, 2.ContainsSubstring (it's optional)
  29. SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And,
  30. new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false),
  31. new SearchFilter.ContainsSubstring(EmailMessageSchema.Subject, mailFilterSubject));
  32.  
  33. //get messages with filter
  34. FindItemsResults<Item> findMails = service.FindItems(WellKnownFolderName.Inbox, filter, new ItemView(int.MaxValue));
  35.  
  36. foreach (Item item in findMails.Items)
  37. {
  38. //init mail properties
  39. ExtendedPropertyDefinition htmlBodyProperty = new ExtendedPropertyDefinition(0x1013, MapiPropertyType.Binary);
  40. PropertySet propertySet = new PropertySet(BasePropertySet.FirstClassProperties, htmlBodyProperty);
  41.  
  42. //load message with its properties
  43. EmailMessage message = EmailMessage.Bind(service, item.Id, propertySet);
  44.  
  45. //mark found mails as read
  46. message.IsRead = true;
  47. message.Update(ConflictResolutionMode.AlwaysOverwrite);
  48.  
  49. //add mail content to list
  50. mailContentList.Add(message.Body.Text); // also message.Subject, message.Body.Text
  51. }
  52. return mailContentList;
  53. }
Add Comment
Please, Sign In to add comment