public static void CheckMailToExecute() { try { Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass(); Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); Microsoft.Office.Interop.Outlook.Items oItems = myInbox.Items.Restrict("[UnRead] = True"); foreach (var item in oItems) { if (item is Microsoft.Office.Interop.Outlook.MailItem) { Microsoft.Office.Interop.Outlook.MailItem mail = (Microsoft.Office.Interop.Outlook.MailItem)item; if (mail.UnRead && mail.Subject.StartsWith("MC")) { mail.UnRead = false; ExecuteMail(mail); } } } } catch (Exception ex) { Logger.Log(ex.Message); } }
public OutlookUtil() { // // TODO: Add constructor logic here // myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass(); myName = myApp.GetNamespace("MAPI"); myName.Logon("Outlook", "", false, true); }
public void getOutlookMailsSignatues() { //Microsoft.Office.Interop.Outlook.Application app = null; //Microsoft.Office.Interop.Outlook._NameSpace ns = null; //Microsoft.Office.Interop.Outlook.MailItem item = null; //Microsoft.Office.Interop.Outlook.MAPIFolder inboxFolder = null; //Microsoft.Office.Interop.Outlook.MAPIFolder subFolder = null; Microsoft.Office.Interop.Outlook.Application myApp = new Microsoft.Office.Interop.Outlook.ApplicationClass(); Microsoft.Office.Interop.Outlook.NameSpace mapiNameSpace = myApp.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); Microsoft.Office.Interop.Outlook.Folders inboxFolders = myInbox.Folders; //mapiNameSpace.Logon("*****@*****.**", "success", false, false); myInbox = mapiNameSpace.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox); String sCriteria = "[From] = '*****@*****.**'"; Microsoft.Office.Interop.Outlook.Items filteredItems = myInbox.Items.Restrict(sCriteria); Microsoft.Office.Interop.Outlook.MAPIFolder destFolder = myInbox.Folders["BounceMails"]; if (filteredItems.Count > 0) { foreach (Microsoft.Office.Interop.Outlook.MailItem item in filteredItems) { //[email protected] bounceUrl = item.To.ToString(); // bounceUrl = "*****@*****.**"; if (bounceUrl.IndexOf("+") > 0) { bounceUrl = bounceUrl.Substring(bounceUrl.IndexOf("+")); int length = bounceUrl.IndexOf("@"); bounceUrl = bounceUrl.Substring(1, length - 1); string[] BounceDetails = bounceUrl.Split('-'); userName = BounceDetails[0].ToString(); ProfileBase objProfileBase = ProfileBase.Create(userName, true); string dbName = objProfileBase.GetPropertyValue("DBName").ToString(); string dbUserID = objProfileBase.GetPropertyValue("DBUserID").ToString(); string dbPassword = objProfileBase.GetPropertyValue("DBPassword").ToString(); string dbServerName = objProfileBase.GetPropertyValue("DBServerName").ToString(); connectionString = string.Format("Data Source={0};Initial Catalog={1};User ID={2};Password={3};Pooling=True", dbServerName, dbName, dbUserID, dbPassword); ConnectionStringHelper.ConStr = connectionString; clientConnectionString = connectionString; DatabaseHelper oDatabaseHelper = new DatabaseHelper(connectionString); bool ExecutionState = false; // The parameter '@ErrorCode' will contain the status after execution of the stored procedure. oDatabaseHelper.AddParameter("@ErrorCode", -1, System.Data.ParameterDirection.Output); oDatabaseHelper.AddParameter("@CampaignID", BounceDetails[1]); oDatabaseHelper.AddParameter("@ContactID", BounceDetails[2]); oDatabaseHelper.AddParameter("@BounceCategory", "bad-mailbox"); oDatabaseHelper.ExecuteDataSet("sp_MailBounce_Insert_CustomProc", ref ExecutionState); Microsoft.Office.Interop.Outlook.MailItem moveMail = null; moveMail = item as Microsoft.Office.Interop.Outlook.MailItem; if (moveMail != null) { //string titleSubject = (string)moveMail.Subject; //if (titleSubject.IndexOf("Test") > 0) //{ moveMail.Move(destFolder); //} } } } } }
private DataTable ReadOutlookPstDB() { DataTable table = new DataTable(); table.Columns.Add("entryID", typeof(string)); table.Columns.Add("folderName", typeof(string)); Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.ApplicationClass(); //app.Session.AddStore("D:\\achievo\\outlook_mail.pst"); Microsoft.Office.Interop.Outlook.NameSpace NS = app.GetNamespace("MAPI"); try { foreach (Microsoft.Office.Interop.Outlook.MAPIFolder objFolder in NS.Folders) { GetSubFolder(table, objFolder, objFolder.Name); } } catch (COMException ex) { MessageBox.Show(ex.Message); } finally { NS.Logoff(); app = null; } return table; }
void fsForm_SelectFinishedEvent(object sender, MyEventArgs e) { fsForm.SelectFinishedEvent -= new SelectFinishedEventHandler(fsForm_SelectFinishedEvent); List<string> entries = (List<string>)e.Value; Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.ApplicationClass(); Microsoft.Office.Interop.Outlook.NameSpace NS = app.GetNamespace("MAPI"); Microsoft.Office.Interop.Outlook.MAPIFolder objFolder; Microsoft.Office.Interop.Outlook.MailItem objMail; DataTable table = new DataTable(); table.Columns.Add("subject", typeof(string)); table.Columns.Add("body", typeof(string)); table.Columns.Add("sentOn", typeof(string)); try { foreach (string entryID in entries) { objFolder = NS.GetFolderFromID(entryID); for (int i = 1; i <= objFolder.Items.Count; i++) { try { objMail = (Microsoft.Office.Interop.Outlook.MailItem)objFolder.Items[i]; DataRow dr = table.NewRow(); if (!string.IsNullOrEmpty(objMail.Subject) && !string.IsNullOrEmpty(objMail.HTMLBody)) { dr["subject"] = objMail.Subject; dr["body"] = objMail.HTMLBody; dr["sentOn"] = objMail.SentOn.ToString("yyyy-MM-dd HH:mm"); table.Rows.Add(dr); } } catch (System.InvalidCastException ex) { Console.WriteLine(ex.Message); } } } } catch (COMException ex) { MessageBox.Show(ex.Message); } finally { NS.Logoff(); objFolder = null; objMail = null; app = null; } origDataTable = table; this.toolStripStatusLabel.Text = "从Outlook 2007读取原始邮件成功。"; this.runToolBtn.Enabled = true; this.runToolStripMenuItem.Enabled = true; this.fsForm.Close(); this.fsForm.Dispose(); this.fsForm = null; DataSet ds = new DataSet(); ds.Tables.Add(origDataTable); if (origForm == null) { origForm = new OrigForm(); origForm.MdiParent = this; origForm.WindowState = FormWindowState.Maximized; origForm.FormClosed += new FormClosedEventHandler(origForm_FormClosed); origForm.setDataGrigViewContext(ds); origForm.Show(); } else { origForm.setDataGrigViewContext(ds); origForm.Activate(); origForm.WindowState = FormWindowState.Maximized; origForm.Focus(); } }