web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics AX (Archived)

Pass multiple data from C# to AX

(0) ShareShare
ReportReport
Posted on by 1,040

Hi,

I have implemented a C# code to retrieve the unread email from Exchange. I am going to call this class methods in ax and get the emails data. I consume C# class as reference into AOT and wondering how can I get the data returned by C#? Below is my C# code and AX code.

<code>

public class MicrosoftExchangeEmail

   {

       public class MailItem

       {

           public string EmailId;

           public string From;

           public string Subject;

           public string Body;

       }

       public MailItem[] ReadUnreadEmails(string _EmailAddress, string _Username, string _Password, Boolean _MarkRead)

       {

           try

           {

               ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);

               service.Credentials = new WebCredentials(_Username, _Password);

               service.AutodiscoverUrl(_EmailAddress);

               //Read all unread emails

               PropertySet itempropertyset = new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.From);

               itempropertyset.RequestedBodyType = BodyType.Text;

               ItemView view = new ItemView(int.MaxValue);

               view.PropertySet = itempropertyset;

               SearchFilter searchFilter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));

               FindItemsResults<Item> findResults = service.FindItems(WellKnownFolderName.Inbox, searchFilter, view);

               ServiceResponseCollection<GetItemResponse> items = service.BindToItems(findResults.Select(item => item.Id), itempropertyset);

               //Mark email as Read

               if (_MarkRead)

               {

                   foreach (EmailMessage message in findResults.Items)

                   {

                       message.IsRead = true;

                       message.Update(ConflictResolutionMode.AlwaysOverwrite);

                   }

               }

               //Return All email messages

               return items.Select(item =>

               {

                   return new MailItem()

                   {

                       EmailId = item.Item.Id.ToString(),

                       From = ((Microsoft.Exchange.WebServices.Data.EmailAddress)item.Item[EmailMessageSchema.From]).Address,

                       Subject = item.Item.Subject,

                       Body = item.Item.Body.ToString(),

                   };

               }).ToArray();

           }

           catch (Exception ex)

           {

               //throw ex;

               return null;

           }

       }

   }

</code>

AX code: 

ZonMicrosoftExchangeEmail.MicrosoftExchangeEmail a = new ZonMicrosoftExchangeEmail.MicrosoftExchangeEmail();
a.ReadUnreadEmails("EmailAddress","UserName","Password",true);

Thanks

*This post is locked for comments

I have the same question (0)
  • Suggested answer
    Community Member Profile Picture
    on at

    You can use Dynamics AX container variable to get the data.

    please verify and let us know your findings

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics AX (Archived)

#1
Martin Dráb Profile Picture

Martin Dráb 4 Most Valuable Professional

#1
Priya_K Profile Picture

Priya_K 4

#3
MyDynamicsNAV Profile Picture

MyDynamicsNAV 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans