-1

i was trying to get email from Exchange Server through EWS Managed API, managed to connect to the server but when i tried to get the list of email I always get "Object reference not set to an instance of an object." error. Anyone can help where did I do wrong?

Here is the procedure that i create to get the email :

public void MssPeekInboxEmails(string ssusername, string sspassword, out RLEmailDigestRecordList ssemails, out bool ssloginValid) {
        // TODO: Write implementation for action
        ssemails = new RLEmailDigestRecordList();

        ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };

        ExchangeService service = new ExchangeService();

        service.Credentials = new NetworkCredential(ssusername, sspassword);
        service.AutodiscoverUrl(ssusername, RedirectionCallback);
        //service.Url = serviceUrl;

        //ArrayList arrMessages = new ArrayList();
        GenericExtendedActions.LogMessage(AppInfo.GetAppInfo().OsContext, "1", "EWS");
        FindItemsResults<Item> findResults = null;
        try
        {
            findResults = service.FindItems(WellKnownFolderName.Inbox, new ItemView(100));
        }
        catch
        {
            ssemails = new RLEmailDigestRecordList();
            ssloginValid = false;

            return;
        }

        service.LoadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
        GenericExtendedActions.LogMessage(AppInfo.GetAppInfo().OsContext, "2", "EWS");
        foreach (EmailMessage item in findResults)
        {
            GenericExtendedActions.LogMessage(AppInfo.GetAppInfo().OsContext, "3", "EWS");
            RCEmailDigestRecord element = new RCEmailDigestRecord();
            element.ssSTEmailDigest.ssId = item.Id.UniqueId;
            element.ssSTEmailDigest.ssFrom.ssSTEmailAddress.ssAddress = item.From.Address;

            foreach (EmailAddress toElement in item.ToRecipients)
            {
                RCEmailAddressRecord record = new RCEmailAddressRecord();
                record.ssSTEmailAddress.ssAddress = toElement.Address;
                element.ssSTEmailDigest.ssTo = new RLEmailAddressRecordList();
                element.ssSTEmailDigest.ssTo.Add(record);
            }

            element.ssSTEmailDigest.ssPriority = (int)item.Importance;
            if (item.Subject.Length > EmailDigestSubjectLenght)
            {

                element.ssSTEmailDigest.ssSubject = item.Subject.Substring(0, EmailDigestSubjectLenght - 3);
                element.ssSTEmailDigest.ssSubject += "...";
            }
            else
            {
                element.ssSTEmailDigest.ssSubject = item.Subject;
            }

            if (item.Body.Text.Length > EmailDigestBodyLenght)
            {
                element.ssSTEmailDigest.ssBody = item.Body.Text.Substring(0, EmailDigestBodyLenght - 3);
                element.ssSTEmailDigest.ssBody += "...";
            }
            else
            {
                element.ssSTEmailDigest.ssBody = item.Body.Text;
            }

            element.ssSTEmailDigest.ssHasAttachment = (item.Attachments.Count > 0);

            ssemails.Add(element);
            ssloginValid = true;

        }
        GenericExtendedActions.LogMessage(AppInfo.GetAppInfo().OsContext, "4", "EWS");

        ssloginValid = true;
    } // MssPeekInboxEmailsenter code here
Alv
  • 21
  • 7

1 Answers1

0

So after checking my code, i found the problem myself. Its on this particular line

RCEmailDigestRecord element = new RCEmailDigestRecord();

I changed it to

RCEmailDigestRecord element = new RCEmailDigestRecord(null);

and its working now.

Sorry for not mentioning which line the error is, since i create this program in rush and its being used on another platform (outsystems) which make it not possible to debug it directly from visual studio.

Alv
  • 21
  • 7