Example #1
0
        private List <EmailMessage> GetEmailList(ExchangeService service, EWSMailSearchQuery query, string keyword)
        {
            List <EmailMessage> list = null;

            for (int i = 0; ; i++)
            {
                try
                {
                    list = EWSMailSearchQuery.SearchMail(service, query);
                    break;
                }
                catch (Exception ex)
                {
                    Thread.Sleep(2000);
                    if (i == 4)
                    {
                        string msg = string.Format("execute GetEmailList(ExchangeService service, EWSMailSearchQuery query) failed. msg:{0}", ex.Message);
                        Logger.Log(msg, Logger.LogType.Error);
                        listErrorEamil.Add(keyword);
                        return(list);
                    }
                }
            }

            return(list);
        }
Example #2
0
        private List <string> GetAttachmentRic(string keyWord, string downloadPath)
        {
            List <string> list = new List <string>();

            try
            {
                query = new EWSMailSearchQuery("", emailAccount.MailAddress, emailFolder, keyWord, "", startDate, endDate);
                List <EmailMessage> mailList = GetEmailList(service, query, keyWord);

                if (mailList == null)
                {
                    string msg = string.Format("email account error.");
                    Logger.Log(msg, Logger.LogType.Error);
                    return(null);//ric list ==null
                }

                if (mailList.Count == 0)
                {
                    string msg = string.Format("no email in this account within this email folder");
                    Logger.Log(msg, Logger.LogType.Warning);
                    return(list);//ric list.count==0
                }

                EmailMessage email = mailList[0];
                email.Load();
                List <string> attachments = EWSMailSearchQuery.DownloadAttachments(service, email, "", "", downloadPath);
                return(GetRicFromFile(attachments));
            }
            catch (Exception ex)
            {
                string msg = string.Format("execute GetRicFromIFFM() failed. msg:{0}", ex.Message);
                Logger.Log(msg, Logger.LogType.Error);
                return(list);
            }
        }
        public List <EmailMessage> GetSearchQueryEmailMessage(string mailbox, string subjectKeyword, DateTime startDate, DateTime endDate, string sendAddress = "", string mailFolderPath = @"Inbox", string bodyKeyword = "")
        {
            List <EmailMessage> emails = null;

            this.query = new EWSMailSearchQuery(sendAddress, mailbox, mailFolderPath, subjectKeyword, bodyKeyword, startDate, endDate);

            RetryUtil.Retry(5, TimeSpan.FromSeconds(2), true, delegate
            {
                emails = EWSMailSearchQuery.SearchMail(service, query);
            });

            return(emails);
        }
        public List <string> DownloadAttachmentFile(string savePath, List <EmailMessage> emails, string keyWord = "", string excludedWord = "")
        {
            List <string> attachmentFilesPath = new List <string>();
            List <string> temp = null;

            foreach (var email in emails)
            {
                temp = EWSMailSearchQuery.DownloadAttachments(service, email, keyWord, excludedWord, savePath);
                if (temp != null && temp.Count != 0)
                {
                    attachmentFilesPath.AddRange(temp);
                }
            }

            return(attachmentFilesPath);
        }
Example #5
0
 /// <summary>
 /// ReadEmailTolistRicChain
 /// </summary>
 /// <param name="listRicChain"></param>
 /// <param name="strPattern">pattern</param>
 /// <param name="strEmailKeyWord">Email title</param>
 private void ReadEmailTolistRicChain(List <string> listRicChain, string strPattern, string strEmailKeyWord)
 {
     try
     {
         EWSMailSearchQuery  query    = new EWSMailSearchQuery("", mailAdress, mailFolder, strEmailKeyWord, "", startDate, endDate);
         List <EmailMessage> mailList = null;
         for (int i = 0; i < 5; i++)
         {
             try
             {
                 mailList = EWSMailSearchQuery.SearchMail(service, query);
                 break;
             }
             catch
             {
                 Thread.Sleep(5000);
                 if (i == 4)
                 {
                     throw;
                 }
             }
         }
         if (mailList.Count > 0)
         {
             isExistEmail = true;
             EmailMessage mail = mailList[0];
             mail.Load();
             strEmailRicChain = TWHelper.ClearHtmlTags(mail.Body.ToString());
             Regex           regex   = new Regex(strPattern);
             MatchCollection matches = regex.Matches(strEmailRicChain);
             foreach (Match match in matches)
             {
                 listRicChain.Add(match.Groups["RIC"].Value);
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Log("Get Data from mail failed. Ex: " + ex.Message);
     }
 }
Example #6
0
        /// <summary>
        /// Find the latest Email with the title "Nasdaq MF corp file"
        /// Download the attachments and put it in the result directory
        /// </summary>
        /// <returns>The name of the downloaded file</returns>
        private string DownladExcel()
        {
            try
            {
                EWSMailSearchQuery  query    = new EWSMailSearchQuery("", configObj.Email, "Inbox\\", "Mutual Fund", "", DateTime.Now.AddDays(-4), DateTime.Now);
                List <EmailMessage> mailList = EWSMailSearchQuery.SearchMail(service, query);
                string path = Path.GetDirectoryName(configObj.ResultFilePath);

                foreach (EmailMessage mail in mailList)
                {
                    mail.Load();
                    List <string> attachments = EWSMailSearchQuery.DownloadAttachments(service, mail, "", "", path);
                    return(attachments[0]);
                }
                return(null);
            }
            catch (Exception ex)
            {
                Logger.Log("Cannot get excel from email");
                throw new Exception("Get excel file from mail failed.\r\nCheck your Email login credentials.\n\r: " + ex.Message, ex);
            }
        }
Example #7
0
        private List <string> GetRicFromEmailBody(string keyWord, string pattern)
        {
            List <string> list = new List <string>();

            try
            {
                query = new EWSMailSearchQuery("", emailAccount.MailAddress, emailFolder, keyWord, "", startDate, endDate);
                List <EmailMessage> mailList = GetEmailList(service, query, keyWord);

                if (mailList == null)
                {
                    string msg = string.Format("email account error.");
                    Logger.Log(msg, Logger.LogType.Error);
                    return(null);//ric list ==null
                }

                if (mailList.Count == 0)
                {
                    string msg = string.Format("no email in this account within this email folder");
                    Logger.Log(msg, Logger.LogType.Warning);
                    return(list);//ric list.count==0
                }

                EmailMessage email = mailList[0];
                email.Load();
                string body = email.Body.ToString();

                return(GetRicIFFM(body, pattern));
            }
            catch (Exception ex)
            {
                string msg = string.Format("execute GetRicFromIFFM() failed. msg:{0}", ex.Message);
                Logger.Log(msg, Logger.LogType.Error);
                return(list);
            }
        }
Example #8
0
        private void FilllistEXLFromEmail()
        {
            try
            {
                service = EWSUtility.CreateService(new System.Net.NetworkCredential(accountName, password, domain), new Uri(@"https://apac.mail.erf.thomson.com/EWS/Exchange.asmx"));
                EWSMailSearchQuery query;
                if (string.IsNullOrEmpty(emailDate))
                {
                    query = new EWSMailSearchQuery("", mailAdress, mailFolder, "TWWNT_DROP_IFFM Expired RICs Housekeeping Report for TWWNT_DROP", "", dateTime.AddHours(-dateTime.Hour).AddMinutes(-dateTime.Minute), dateTime);
                }
                else
                {
                    startTime = Convert.ToDateTime(emailDate);
                    endTime   = startTime.AddDays(+1);
                    query     = new EWSMailSearchQuery("", mailAdress, mailFolder, "TWWNT_DROP_IFFM Expired RICs Housekeeping Report for TWWNT_DROP", "", startTime, endTime);
                }
                List <EmailMessage> mailList = null;
                for (int i = 0; i < 5; i++)
                {
                    try
                    {
                        mailList = EWSMailSearchQuery.SearchMail(service, query);
                        break;
                    }
                    catch
                    {
                        Thread.Sleep(5000);
                        if (i == 4)
                        {
                            throw;
                        }
                    }
                }
                if (mailList.Count > 0)
                {
                    string       emailString = string.Empty;
                    EmailMessage mail        = mailList[0];
                    mail.Load();
                    emailString = TWHelper.ClearHtmlTags(mail.Body.ToString());
                    string[] emailEXL = emailString.Replace("EXL", "~").Split('~');
                    foreach (string email in emailEXL.Where(email => email.Trim().StartsWith("(")))
                    {
                        List <string> listTmp;
                        string        tmp = email.Substring(email.IndexOf("(") + 1, email.IndexOf(")") - 2).Trim();
                        switch (tmp)
                        {
                        case "TAIW_INX_WNT":
                            listTmp = new List <string> {
                                tmp
                            };
                            RegexPattern(email, listTmp);
                            break;

                        case "TAIW_EQLB_WNT":
                            listTmp = new List <string> {
                                tmp
                            };
                            RegexPattern(email, listTmp);
                            break;

                        case "TAIW_CBBC":
                            listTmp = new List <string> {
                                tmp
                            };
                            RegexPattern(email, listTmp);
                            break;

                        case "OTCTWS_WNT":
                            listTmp = new List <string> {
                                tmp
                            };
                            RegexPattern(email, listTmp);
                            break;

                        case "OTCTWS_INX_WNT":
                            listTmp = new List <string> {
                                tmp
                            };
                            RegexPattern(email, listTmp);
                            break;
                        }
                    }
                }
                else
                {
                    MessageBox.Show("No email of TWWNT_DROP_IFFM in Outlook!");
                }
            }
            catch (Exception ex)
            {
                Logger.Log("Get Data from mail failed. Ex: " + ex.Message + "Try To Execute {EWSMailSearchQuery.SearchMail(service, query)} 5 times");
            }
        }
Example #9
0
 /// <summary>
 /// ReadEmailAndGetData
 /// </summary>
 /// <param name="listRic">listRic</param>
 /// <param name="strPattern">regular expression</param>
 /// <param name="strEmailKeyWord">subject of email</param>
 private void ReadEmailTolistRicChain(List <string> listRic, string strPattern, string strEmailKeyWord)
 {
     try
     {
         service = EWSUtility.CreateService(new System.Net.NetworkCredential(accountName, password, domain), new Uri(@"https://apac.mail.erf.thomson.com/EWS/Exchange.asmx"));
         EWSMailSearchQuery query = null;
         query = new EWSMailSearchQuery("", mailAdress, mailFolder, strEmailKeyWord, "", startDate, endDate);
         List <EmailMessage> mailList = null;
         for (int i = 0; i < 5; i++)
         {
             try
             {
                 mailList = EWSMailSearchQuery.SearchMail(service, query);
                 break;
             }
             catch
             {
                 Thread.Sleep(5000);
                 if (i == 4)
                 {
                     throw;
                 }
             }
         }
         if (mailList.Count > 0)
         {
             if (strEmailKeyWord.Equals("TAIFO IFFM - EDA Automation Report"))
             {
                 isExistFirstEmail = true;
             }
             else
             {
                 isExistSecondEmail = true;
             }
             EmailMessage mail = mailList[0];
             mail.Load();
             strEmailRic = TWHelper.ClearHtmlTags(mail.Body.ToString());
             Regex           regex   = new Regex(strPattern);
             MatchCollection matches = regex.Matches(strEmailRic);
             foreach (Match match in matches)
             {
                 listRic.Add(match.Groups["RIC"].Value);
             }
             if (strEmailKeyWord.Equals("TAIFO IFFM - EDA Automation Report"))
             {
                 countListRic = listRic.Count;
                 if (listRic.Count == 0)
                 {
                     isExistFirstEmptyEmail = true;
                 }
             }
             else
             {
                 if (listRic.Count == countListRic)
                 {
                     isExistSeconEmptydEmail = true;
                 }
             }
         }
     }
     catch (Exception ex)
     {
         Logger.Log("Get Data from mail failed. Ex: " + ex.Message);
     }
 }
Example #10
0
 /// <summary>
 /// DownloadAttachementFileFromEmail
 /// </summary>
 /// <param name="listAttachementFile">store file name</param>
 /// <param name="strEmailKeyWord">subject</param>
 /// <param name="isExistEmail">bool</param>
 /// <param name="isEmptyEmail">bool</param>
 private void GetAttachementFromEmail(List <string> listAttachementFile, string strEmailKeyWord, ref bool isExistEmail, ref bool isEmptyEmail)
 {
     try
     {
         service = MSAD.Common.OfficeUtility.EWSUtility.CreateService(new System.Net.NetworkCredential(accountName, password, domain), new Uri(@"https://apac.mail.erf.thomson.com/EWS/Exchange.asmx"));
         EWSMailSearchQuery  query    = new EWSMailSearchQuery("", mailAdress, mailFolder, strEmailKeyWord, "", startDate, endDate);
         List <EmailMessage> mailList = null;
         string strEmailContent       = string.Empty;
         for (int i = 0; i < 5; i++)
         {
             try
             {
                 mailList = EWSMailSearchQuery.SearchMail(service, query);
                 break;
             }
             catch
             {
                 Thread.Sleep(5000);
                 if (i == 4)
                 {
                     throw;
                 }
             }
         }
         if (mailList.Count > 0)
         {
             isExistEmail = true;
             string       attachmentPath = Path.Combine(txtFilePath, "attachment");
             EmailMessage mail           = mailList[0];
             if (!Directory.Exists(attachmentPath))
             {
                 Directory.CreateDirectory(attachmentPath);
             }
             else
             {
                 string[] rootFiles = Directory.GetFiles(attachmentPath);
                 foreach (string file in rootFiles)
                 {
                     File.Delete(file);
                 }
             }
             mail.Load();
             List <string> attachments = EWSMailSearchQuery.DownloadAttachments(service, mail, "", "", attachmentPath);
             if (attachments != null && attachments.Count != 0)
             {
                 string dir = Path.GetDirectoryName(attachments[0]);
                 foreach (string zipFile in attachments)
                 {
                     string err = null;
                     if (!Ric.Util.ZipUtil.UnZipFile(zipFile, dir, out err))
                     {
                         Logger.Log(string.Format("Error happens when unzipping the file {0}. Exception message: {1}", zipFile, err));
                     }
                 }
                 foreach (var file in Directory.GetFiles(Path.GetDirectoryName(attachments[0]), "*.xls"))
                 {
                     listAttachementFile.Add(file);
                 }
             }
             else
             {
                 isEmptyEmail = true;
                 Logger.Log("Found email but no attachement");
             }
         }
         else
         {
             Logger.Log("there is no email in mail box!");
         }
     }
     catch (Exception ex)
     {
         Logger.Log("Get Data from mail failed. Ex: " + ex.Message);
     }
 }
 public List <string> DownloadAttachmentFile(string savePath, EmailMessage email, string keyWord = "", string excludedWord = "")
 {
     return(EWSMailSearchQuery.DownloadAttachments(service, email, keyWord, excludedWord, savePath));
 }
Example #12
0
        private List <string> GetDownloadFilePathFromEmail()
        {
            List <string> list = new List <string>();

            try
            {
                EWSMailSearchQuery  query    = new EWSMailSearchQuery("", emailAccount.MailAddress, @mailFolder, "China FM for", "", startDate, endDate);
                List <EmailMessage> mailList = null;
                EmailMessage        mail     = null;

                for (int i = 0; i < 5; i++)
                {
                    try
                    {
                        mailList = EWSMailSearchQuery.SearchMail(service, query);
                        break;
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(2000);

                        if (i == 4)
                        {
                            throw new Exception("[EWSMailSearchQuery.SearchMail(service, query)] error.msg:" + ex.ToString());
                        }
                    }
                }

                if (mailList == null)
                {
                    string msg = string.Format("can't get email from mailbox. ");
                    Logger.Log(msg, Logger.LogType.Error);
                    return(list);//list.count==0 //no ipo today
                }

                emailCountInMailBox = mailList.Count;//email count received

                if (!(emailCountInMailBox > 0))
                {
                    string msg = string.Format("no email in the mailbox. ");
                    Logger.Log(msg, Logger.LogType.Warning);
                    return(list);//list.count==0 //no ipo today
                }

                if (!Directory.Exists(downloadFilePath))
                {
                    Directory.CreateDirectory(downloadFilePath);
                }

                mail = mailList[0];
                mail.Load();
                List <string> attachments = EWSMailSearchQuery.DownloadAttachments(service, mail, "", "", downloadFilePath);

                if (attachments == null && attachments.Count == 0)
                {
                    return(list);// emailcount!=0 but no attachement so list.count==0 //no ipo today
                }

                int    start    = 0;
                string fileName = string.Empty;

                foreach (var str in attachments)
                {
                    start    = str.LastIndexOf("\\");
                    fileName = str.Substring(start + 1, str.Length - start - 1);

                    if (!fileName.Contains(".xls"))
                    {
                        continue;
                    }

                    list.Add(str);
                }

                return(list);
            }
            catch (Exception ex)
            {
                string msg = string.Format("get email and download attachement error. :{0}", ex.ToString());
                Logger.Log(msg, Logger.LogType.Error);

                return(null);
            }
        }
Example #13
0
        private List <string> GetRicFromEmail(string emailKeyWord)
        {
            List <string> list = new List <string>();

            try
            {
                service = EWSUtility.CreateService(new System.Net.NetworkCredential(accountName, password, domain), new Uri(@"https://apac.mail.erf.thomson.com/EWS/Exchange.asmx"));
                EWSMailSearchQuery  query    = query = new EWSMailSearchQuery("", mailAdress, @mailFolder, emailKeyWord, "", startDate, endDate);
                List <EmailMessage> mailList = null;

                for (int i = 0; i < 5; i++)
                {
                    try
                    {
                        mailList = EWSMailSearchQuery.SearchMail(service, query);
                        break;
                    }
                    catch
                    {
                        Thread.Sleep(5000);

                        if (i == 4)
                        {
                            throw;
                        }
                    }
                }

                emailCountInMailBox = mailList.Count;

                if (!(emailCountInMailBox > 0))
                {
                    string msg = string.Format("no email in the mailbox. ");
                    Logger.Log(msg, Logger.LogType.Warning);
                    return(null);
                }

                string strEmailBody = string.Empty;

                for (int i = 0; i < emailCountInMailBox; i++)
                {
                    EmailMessage mail = mailList[i];
                    mail.Load();
                    strEmailBody = TWHelper.ClearHtmlTags(mail.Body.ToString());
                    Regex           regex   = new Regex(emailPattern);
                    MatchCollection matches = regex.Matches(strEmailBody);

                    foreach (Match match in matches)
                    {
                        if (list.Contains(match.Groups["RIC"].Value))
                        {
                            continue;
                        }

                        list.Add(match.Groups["RIC"].Value.ToString().Trim());
                    }
                }

                return(list);
            }
            catch (Exception ex)
            {
                string msg = string.Format("get ric from email error.:{0}", ex.ToString());
                Logger.Log(msg, Logger.LogType.Error);
                return(null);
            }
        }