Beispiel #1
0
        //Search mails in a specified account
        public static MAPIFolder SearchTargetMailAccount(OutlookApp outlookApp, string accountName)
        {
            _NameSpace ns = outlookApp.OutlookAppInstance.GetNamespace("MAPI");

            ns.Logon(null, null, false, false);
            MAPIFolder folder = null;

            try
            {
                Folders folders = outlookApp.OutlookAppInstance.Application.Session.Folders;

                foreach (MAPIFolder item in folders)
                {
                    if (item.Name.Contains(accountName))
                    {
                        folder = item;
                        break;
                    }
                }
            }

            catch (System.Exception ex)
            {
                throw new System.Exception(string.Format("Error found when searching the target mail account {0}. The exception message is: {1}", accountName, ex.Message));
            }
            return(folder);
        }
Beispiel #2
0
        //Search mails in specified sub folders under a specified account
        public static List <MailItem> SearchEmailInSpecifiedFolder(OutlookApp outlookApp, string accountName, string subFolderPath)
        {
            MAPIFolder      mapiFolder = SearchTargetMailAccount(outlookApp, accountName);
            List <MailItem> mailList   = SearchEmailInSpecifiedFolder(mapiFolder, subFolderPath);

            return(mailList);
        }
Beispiel #3
0
        public static List <MailItem> SearchMail(OutlookApp outlookApp, MailSearchQuery query)
        {
            List <MailItem> mailList = new List <MailItem>();
            MAPIFolder      folder   = SearchTargetMailAccount(outlookApp, query.AccountName);

            folder = GetFolderPath(folder, query.MailFolderPath);
            //mailList = SearchEmailInSpecifiedFolder(outlookApp, query.AccountName, query.MailFolderPath);
            //mailList = GetMailsSendInCertainTime(mailList,query.StartDate,query.EndDate);
            mailList = GetMailsSendInCertainTime(folder, query.StartDate, query.EndDate);
            mailList = GetMailsFromCertainAddress(mailList, query.Sender);
            mailList = GetMailsWithCertainSubject(mailList, query.SubjectKeywordList);
            mailList = GetMailsWithoutCertainSubject(mailList, query.SubjectExcludedwordList);
            return(mailList);
        }
Beispiel #4
0
 public static void SaveMail(OutlookApp outlookApp, List <string> attachedFileList, string subject, List <string> toTypeRecipients, List <string> ccTypeRecipient, string mailBody, string mailHtmlBody, out string err, string path)
 {
     SaveMail(outlookApp, attachedFileList, subject, null, toTypeRecipients, ccTypeRecipient, null, mailBody, mailHtmlBody, out err, path);
 }
Beispiel #5
0
        public static void SaveMail(OutlookApp outlookApp, List <string> attachedFileList, string subject, List <string> originatorRecipientList, List <string> toTypeRecipients, List <string> ccTypeRecipient, List <string> bccTypeRecipient, string mailBody, string mailHtmlBody, out string err, string path)
        {
            err = string.Empty;
            MailItem mail = outlookApp.OutlookAppInstance.CreateItem(OlItemType.olMailItem) as MailItem;

            mail.Subject = subject;
            mail.Body    = mailBody;
            if (mailHtmlBody != null)
            {
                mail.HTMLBody = mailHtmlBody;
            }
            AddressEntry currentUser = outlookApp.OutlookAppInstance.Session.CurrentUser.AddressEntry;

            if (toTypeRecipients != null && toTypeRecipients.Count > 0)
            {
                foreach (string recipient in toTypeRecipients)
                {
                    if (!string.IsNullOrEmpty(recipient))
                    {
                        Recipient toRecipient = mail.Recipients.Add(recipient);
                        toRecipient.Type = (int)OlMailRecipientType.olTo;
                    }
                }
            }

            if (originatorRecipientList != null && originatorRecipientList.Count > 0)
            {
                foreach (string recipient in originatorRecipientList)
                {
                    Recipient originalRecipient = mail.Recipients.Add(recipient);
                    originalRecipient.Type = (int)OlMailRecipientType.olOriginator;
                }
            }

            if (ccTypeRecipient != null && ccTypeRecipient.Count > 0)
            {
                foreach (string recipient in ccTypeRecipient)
                {
                    if (!string.IsNullOrEmpty(recipient))
                    {
                        Recipient ccRecipient = mail.Recipients.Add(recipient);
                        ccRecipient.Type = (int)OlMailRecipientType.olCC;
                    }
                }
            }

            if (bccTypeRecipient != null && bccTypeRecipient.Count > 0)
            {
                foreach (string recipient in bccTypeRecipient)
                {
                    Recipient bccRecipient = mail.Recipients.Add(recipient);
                    bccRecipient.Type = (int)OlMailRecipientType.olBCC;
                }
            }

            bool isResolveSuccess = mail.Recipients.ResolveAll();

            if (!isResolveSuccess)
            {
                err += "Warning: There's error when resolve the recipients' names";
            }


            if (attachedFileList != null)
            {
                try
                {
                    foreach (string attachedFilePath in attachedFileList)
                    {
                        mail.Attachments.Add(attachedFilePath, OlAttachmentType.olByValue, Type.Missing, Type.Missing);
                    }
                }
                catch (System.Exception ex)
                {
                    err += "Error: There's error when adding attach file to the mail " + ex.Message;
                    Console.WriteLine("There's error when adding attach file to the mail " + ex.Message);
                }
            }
            mail.SaveAs(path, Type.Missing);
        }
Beispiel #6
0
 public static void SaveMail(OutlookApp outlookApp, MailToSend mail, out string err, string path)
 {
     SaveMail(outlookApp, mail.AttachFileList, mail.MailSubject, mail.ToReceiverList, mail.CCReceiverList, mail.MailBody, mail.MailHtmlBody, out err, path);
 }
Beispiel #7
0
 //Send mail without attachment
 public static void CreateAndSendMail(OutlookApp outlookApp, string subject, List <string> toTypeRecipients, List <string> ccTypeRecipient, string mailBody, out string err)
 {
     CreateAndSendMail(outlookApp, null, subject, toTypeRecipients, ccTypeRecipient, mailBody, null, out err);
 }
Beispiel #8
0
 public static void CreateAndSendMail(OutlookApp outlookApp, List <string> attachedFileList, string subject, List <string> toTypeRecipients, List <string> ccTypeRecipient, string mailBody, string mailHtmlBody, out string err)
 {
     CreateAndSendMail(outlookApp, attachedFileList, subject, null, toTypeRecipients, ccTypeRecipient, null, mailBody, mailHtmlBody, out err);
 }
Beispiel #9
0
 public static void CreateAndSendMail(OutlookApp outlookApp, MailToSend mail, out string err)
 {
     CreateAndSendMail(outlookApp, mail.AttachFileList, mail.MailSubject, mail.ToReceiverList, mail.CCReceiverList, mail.MailBody, mail.MailHtmlBody, out err);
 }