In this blog i'm only explaining the important aspects when reading using EWS API. I read from a child folder from root mailbox. Use WellKnownFolderName.Root it will show all folders of my Inbox.Then using web services i call the 3rd party tool and pass the information read from the specific mail box. Below i'm showing only the flow of the process but this will help you to figure our the code level commands.
public static void RunProcess()
{
DateTime dateTime = DateTime.Now;
service.Credentials = new NetworkCredential(Settings.Default.EmailAddress, Settings.Default.EmailPassword);
if (Settings.Default.ExchangeUseAutodiscover == true)
{
if (isDebugEnabled)
{
logger.Debug("Finding Autodiscover URL.");
}
service.AutodiscoverUrl(Settings.Default.EmailAddress, RedirectionCallback);
}
else
{
service.Url = new System.Uri(Settings.Default.ExchangeWebServicesEndpoint);
}
if (isDebugEnabled)
{
logger.Debug(String.Format("EWS Endpoint: {0}", service.Url));
}
BindMailboxFolders();
ProcessEmail();
}
static bool RedirectionCallback(string url)
{
return url.ToLower().StartsWith("https://");
}
static void BindMailboxFolders()
{
FolderView view = new FolderView(100);
view.PropertySet = new PropertySet(BasePropertySet.IdOnly);
view.PropertySet.Add(FolderSchema.DisplayName);
view.Traversal = FolderTraversal.Deep;
FindFoldersResults findFolderResults = service.FindFolders(WellKnownFolderName.Root, view);
foreach (Folder folder in findFolderResults)
{
if (folder.DisplayName.Equals(Settings.Default.CaseCreatedMailFolderName))
caseCreatedMailFolder = Folder.Bind(service, folder.Id);
if (folder.DisplayName.Equals(Settings.Default.ErrorMailFolderName))
errorMailFolder = Folder.Bind(service, folder.Id);
if (folder.DisplayName.Equals(Settings.Default.UnknownMailFolderName))
unknownMailFolder = Folder.Bind(service, folder.Id);
}
}
static void ProcessEmail()
{
PropertySet emailPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.IsRead);
emailPropertySet.RequestedBodyType = BodyType.Text;
ItemView itemView = new ItemView(1000);
itemView.PropertySet = emailPropertySet;
//Get unread email only
SearchFilter filter = new SearchFilter.SearchFilterCollection(LogicalOperator.And, new SearchFilter.IsEqualTo(EmailMessageSchema.IsRead, false));
FindItemsResults<Item> findEmailResults = service.FindItems(WellKnownFolderName.Inbox, filter, itemView);
int emailCount = 1;
Dictionary<int, Dictionary<string, string>> emails = new Dictionary<int, Dictionary<string, string>>();
foreach (EmailMessage email in findEmailResults.Items.Where(i => i is EmailMessage))
{
email.Load(emailPropertySet);
EmailData emailData = new EmailData();
emailData.fromAddress = email.From.ToString();
emailData.sent = email.DateTimeSent.ToString();
emailData.toAddress = email.DisplayTo.ToString();
emailData.mailSubject = email.Subject.ToString();
if (isDebugEnabled)
{
logger.Debug("-----------------------------------------------");
logger.Debug("From: " + email.LastModifiedName);
logger.Debug("To: " + email.DisplayTo);
logger.Debug("CC: " + email.DisplayCc);
logger.Debug("Subject: " + email.Subject);
logger.Debug("-----------------------------------------------");
}
emails.Add(emailCount, new Dictionary<string, string>());
email.Body.ToString().Split('\n');
foreach (string line in email.Body.ToString().Split('\n'))
{
if (Regex.IsMatch(line, "^.*:{1}.*$"))
{
//Split only on first occurance of ":"
string[] values = line.Split(new[] { ":" }, 2, StringSplitOptions.None);
emails[emailCount].Add(values[0].Trim(), values[1].Trim());
if (isDebugEnabled)
{
logger.Debug(values[0].Trim() + " - " + values[1].Trim());
}
}
}
//Attempt to create case with web services here, then move into folder based on result of web services call
// Get the Dictionary for the current email
Dictionary<string, string> emailDictionary = emails[1];
bool dataComplete = VerifyEmailProperties(emailData, emailDictionary);
// Get the process to create the Case for
Process process = processFactory.getProcess(emailData);
// Create Case in 3rd party system by calling it's web service
try
{
createCase.processRequest(process);
//Mark as read and move to appropriate folder
email.IsRead = true;
email.Update(ConflictResolutionMode.AutoResolve);
if (isDebugEnabled)
{
logger.Debug("MARKED AS READ...");
}
email.Move(caseCreatedMailFolder.Id);
if (isDebugEnabled)
{
logger.Debug("MOVED TO FOLDER...");
}
emailData = null;
}
catch (Exception e)
{
email.IsRead = true;
email.Update(ConflictResolutionMode.AutoResolve);
if (isErrorEnabled)
{
logger.Error("Failed to Process...MARKED AS READ..." + Environment.NewLine
+ emailData.ToString() + Environment.NewLine + e.InnerException.StackTrace);
}
}
emailData = null;
emailCount++;
}
}
No comments:
Post a Comment