Solved

How to get ToRecipients in Exchange Web Service

Posted on 2011-03-04
2
1,723 Views
Last Modified: 2012-05-11
Experts,

I am working on Exchange Web Service API to read mails from a users inbox. I want to do the following

Get the list of mails from a users inbox - Done.
Resend these mails again to their intended recipients - PROBLEM

When i am trying to fetch the intended recipients, ToRecipients property of the EmailMessage class is always empty. Any idea why this is happening? I am attaching my code along with this question.

Working on Exchange Server 2010, Visual Studio 2008

It's an urgent requirement. Ur help is much needed.

Thank you
 
Imports System
Imports System.Configuration
Imports System.Net
Imports Microsoft.Exchange.WebServices.Autodiscover
Imports Microsoft.Exchange.WebServices.Data

Public Class Form2
#Region "Private members"
    Private _objService As ExchangeService
#End Region

#Region "Form/Control events"
    Private Sub FormLoad(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        InitializeService()
        ProcessMailQueue()
    End Sub
#End Region

#Region "User defined methods"
    Private Sub InitializeService() ''Initializes Exchange service
        Try
            _objService = New ExchangeService(ExchangeVersion.Exchange2010)
            _objService.Credentials = New WebCredentials("xxxx@xxxx.com", "xxxxxx")
            _objService.AutodiscoverUrl("xxxx@xxxx.com")
        Catch ex As Exception
        End Try
    End Sub

    Private Function GetInboxItems() As FindItemsResults(Of Item)
        Dim objFindItemResults As FindItemsResults(Of Item)
        Try
            objFindItemResults = _objService.FindItems(WellKnownFolderName.Inbox, New ItemView(10))
        Catch ex As Exception
        End Try
        Return objFindItemResults
    End Function

    Private Sub ProcessMailQueue()
        Dim objMails As FindItemsResults(Of Item)
        Dim objItem As Item

        Try
            objMails = GetInboxItems() ''Getting mails from users inbox

            For Each objItem In objMails ''Looping thru found items and resend them
                ResendMail(objItem)
            Next
        Catch ex As Exception
        End Try
    End Sub

    Private Sub ResendMail(ByVal Mail As EmailMessage)
        Dim objEmail As EmailMessage
        Dim objEmailAddress As EmailAddress

        objEmail = New EmailMessage(_objService)

        For Each objEmailAddress In Mail.ToRecipients ''Mail.ToRecipients list is always empty! 
            objEmail.ToRecipients.Add(objEmailAddress)
        Next

        objEmail.Subject = Mail.Subject
        objEmail.Body = Mail.Body
        objEmail.SendAndSaveCopy()
    End Sub

#End Region
End Class

Open in new window

0
Comment
Question by:RTKHOT
2 Comments
 
LVL 9

Accepted Solution

by:
jkofte earned 500 total points
ID: 35036199
did you try using LoadPropertiesForItems method ?

check my code below:

               
ExchangeService service = oEWS.CallEWSService(); 
                FindItemsResults<Item> findResults = service.FindItems(
                WellKnownFolderName.Inbox,
                new ItemView(Convert.ToInt16(AppSettings.GetConfigValue("BMAEMailCountPerCall"))));
                if (findResults.Items.Count > 0)
                {
                    service.LoadPropertiesForItems(findResults, PropertySet.FirstClassProperties.BasePropertySet);
                }

Open in new window

0
 

Author Closing Comment

by:RTKHOT
ID: 35054944
Perfect! Resolved one other problem i had as well. thx a lot dude! :)
0

Featured Post

Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Scam emails are a huge burden for many businesses. Spotting one is not always easy. Follow our tips to identify if an email you receive is a scam.
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In this video we show how to create a Resource Mailbox in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: Navigate to the Recipients >> Resources tab.: "Recipients" is our default selection …
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question