Results 1 to 3 of 3

Thread: Only read unread emails, set to read and move to deleted folder in Web Services

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jan 2016
    Posts
    17

    Only read unread emails, set to read and move to deleted folder in Web Services

    I am using Microsoft.Exchange.WebServices nuget package in VB.Net. I have the following code to read data in an email, but I cannot figure out 3 things:

    1. Read only unread emails
    2. Set email to read after getting data from it
    3. Move email to deleted folder

    Could you please help? Thank you.

    Code:
    Dim serverURI As New Uri("https://corpmail.....com/ews/exchange.asmx")
            Dim exch As New Microsoft.Exchange.WebServices.Data.ExchangeService()
            exch.Url = serverURI
            exch.UseDefaultCredentials = False
            exch.Credentials = New System.Net.NetworkCredential("username", "password", "domain")
    
    
            Dim iv As ItemView = New ItemView(999)
            iv.Traversal = ItemTraversal.Shallow
            Dim inboxItems As FindItemsResults(Of Item) = Nothing
            inboxItems = exch.FindItems(WellKnownFolderName.Inbox, iv)
            'Dim Count As Integer = inboxItems.Count
    
            For Each i As Item In inboxItems
                MsgBox("this is" & i.Subject)
            Next

  2. #2
    Frenzied Member
    Join Date
    May 2014
    Location
    Central Europe
    Posts
    1,388

    Re: Only read unread emails, set to read and move to deleted folder in Web Services

    i don't know exchange but did some Outlook scripting many years ago. i guess the concept is similar.
    your exch.FindItems(WellKnownFolderName.Inbox, iv) will most likely get a list of all mails in the inbox and i guess you cant do the filtering for unread there. but that is not an issue as your "i As Item" in the loop will have a property like "i.IsRead" that will tell you if the item was already opened. it most likely will also have a "i.MoveTo(NewFolder)" method or there is a "SomeParentObject.MoveItem(i,ToFolder)" method.

    lets see:
    https://www.google.com/search?q=.net...vice+mail+item
    https://docs.microsoft.com/en-us/dot...change-ews-api
    turns out your "i as Item" is a "i as EmailMessage"
    https://docs.microsoft.com/en-us/dot...Message_IsRead
    https://docs.microsoft.com/en-us/dot...ownFolderName_

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jan 2016
    Posts
    17

    Re: Only read unread emails, set to read and move to deleted folder in Web Services

    Hi, you definitely put me on the right path and I highly appreciated it. I was able to the read an email and get its subject and the from. Although, I am having troubles to find a way to read its body. I found four different ways of doing it but they all give me the same result, that is the text is like this:

    Code:
    <html xmlns:v="urn:schemas-microsoft-com:vml" xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns:m="http://schemas.microsoft.com/office/2004/12/omml" xmlns="http://www.w3.org/TR/REC-html40">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    
    ........ MORE HTML
    Which it looks like it gives me the html of the email.
    How can I just get the text in the body of the email? What would be the code for that?
    This is the code I have with the four approaches I tried:

    Code:
    Dim iv As ItemView = New ItemView(999)
            iv.Traversal = ItemTraversal.Shallow
            Dim inboxItems As FindItemsResults(Of Item) = Nothing
            inboxItems = exch.FindItems(WellKnownFolderName.Inbox, iv)
            Dim Count As Integer = inboxItems.Count
    
    For Each i As Item In inboxItems
                Dim Subject = i.Subject
                Dim oMail As EmailMessage = EmailMessage.Bind(exch, i.Id)
                Dim email = oMail.From.Address.ToString
    
    'FIRST WAY TRIED
                Dim body = i.Body.Text
    
    'SECOND WAY TRIED
                i.Load()
                Dim bodyagain = i.Body
    
    'THIRD WAY TRIED
                Dim item As Item = Item.Bind(exch, i.Id)
                Dim bodynew = item.Body.ToString
    
    'FOURTH WAY TRIED
                For Each msg As EmailMessage In inboxItems
                   msg.Load()
                   Dim bodyTry = msg.Body
                Next
    
    Next

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width