
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)You can use Dynamics AX container variable to get the data.
please verify and let us know your findings