static public _com_OutlookExporer IsOutlookExplorerLoaded( ) { Outlook.Application outlook = null; Outlook.NameSpace nameSpace = null; _com_OutlookExporer explorer = null; _isFileNotFoundHappened = false; try { _tracer.Trace("Looking for explorer"); OutlookGUIInit.DebugMessageBox("Looking for explorer"); outlook = new Outlook.ApplicationClass(); _tracer.Trace("Outlook.Application object has been initialized properly."); nameSpace = outlook.GetNamespace("MAPI"); _tracer.Trace("Outlook.NameSpace object has been initialized properly? - " + (nameSpace != null).ToString()); explorer = new _com_OutlookExporer(outlook.ActiveExplorer()); _tracer.Trace("_com_OutlookExporer wrapper object has been initialized properly."); OutlookGUIInit.DebugMessageBox("Get ActiveExplorer"); if (explorer == null) { _tracer.Trace("Outlook explorer is not found"); } else { _tracer.Trace("Outlook explorer is found"); } } catch (FileNotFoundException exception) { _isFileNotFoundHappened = true; _tracer.TraceException(exception); } catch (COMException exception) { _tracer.TraceException(exception); } catch (InvalidComObjectException exception) { _tracer.TraceException(exception); } catch (InvalidCastException exception) { _tracer.TraceException(exception); } finally { COM_Object.Release(outlook); COM_Object.Release(nameSpace); } return(explorer); }
/// <summary> /// Gets the outlook folder from path. /// </summary> /// <param name="outlookNS">The outlook NS.</param> /// <param name="path">The path.</param> /// <returns></returns> public OutlookFolder GetOutlookFolderFromPath(string path) { if (string.IsNullOrEmpty(path)) { throw new ArgumentNullException("path"); } OutlookFolder retVal = null; if (this.InvokeRequired) { Func <string, OutlookFolder> func = this.GetOutlookFolderFromPath; retVal = (OutlookFolder)this.Invoke(func, path); } else { Outlook.MAPIFolder oMapiFolder = null; Outlook.NameSpace oNs = null; try { oNs = this._addinModule.OutlookApp.GetNamespace("MAPI"); string[] folders = path.Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries); if (folders.Length != 0) { oMapiFolder = oNs.Folders.Item(folders[0]); for (int i = 1; i < folders.Length; i++) { Outlook.MAPIFolder oTmpMapiFolder = oMapiFolder.Folders.Item(folders[i]); if (oTmpMapiFolder != null) { Marshal.ReleaseComObject(oMapiFolder); oMapiFolder = oTmpMapiFolder; } } } } finally { if (oNs != null) { Marshal.ReleaseComObject(oNs); } } if (oMapiFolder != null) { retVal = _factory.Create <OutlookItem>(oMapiFolder) as OutlookFolder; } } return(retVal); }
public ArrayList GetMail() { MailMessage message; Outlook.MailItem msgInbox; ArrayList arrayMessages = null; Outlook.ApplicationClass appOutlook = null; try { arrayMessages = new ArrayList(); //connect to the CDO library appOutlook = new Outlook.ApplicationClass(); Outlook.NameSpace appNamespace = appOutlook.GetNamespace("MAPI"); appNamespace.Logon("jkanalakis", "jkanalakis", false, false); //navigate to the inbox folder Outlook.Explorer exp = appOutlook.Explorers.Item(1); for (int intIdx = 1; intIdx <= exp.CurrentFolder.Items.Count; intIdx++) { //retrieve the next message from the inbox msgInbox = (Outlook.MailItem)exp.CurrentFolder.Items.Item(intIdx); //convert data into MailMessage message = new MailMessage(); message.Subject = msgInbox.Subject; message.Body = msgInbox.Body; //add MailMessage to the ArrayList arrayMessages.Add(message); } } catch (Exception exception) { LogEvent(exception.Message); } finally { Marshal.ReleaseComObject(appOutlook); } return(arrayMessages); }
Outlook.Application GetApplicationObject() { Outlook.Application application = null; // Check whether there is an Outlook process running. if (Process.GetProcessesByName("OUTLOOK").Count() > 0) { // If so, use the GetActiveObject method to obtain the process and cast it to an Application object. Or close Outlook to open a new instance with a desired profile application = Marshal.GetActiveObject("Outlook.Application") as Outlook.Application; //!! Check if the application is using the right profile - close & reopen if necessary } else { // If not, create a new instance of Outlook and log on to the default profile.; application = new Outlook.Application(); Outlook.NameSpace nameSpace = application.GetNamespace("MAPI"); nameSpace.Logon("profilename", "", Missing.Value, Missing.Value); nameSpace = null; } // Return the Outlook Application object. return(application); }
/// <summary> /// Picks the outlook folder path. /// </summary> /// <param name="oApp">The o app.</param> /// <param name="oItemType">Type of the o item.</param> /// <returns></returns> public static string PickOutlookFolderPath(Outlook._Application oApp, Outlook.OlItemType oItemType) { string retVal = null; bool correctFolderSelected = false; try { while (!correctFolderSelected) { Outlook.NameSpace oNameSpace = oApp.GetNamespace("MAPI"); Outlook.MAPIFolder oMapiFolder = oNameSpace.PickFolder(); if (oMapiFolder.DefaultItemType != oItemType) { DebugAssistant.Log(DebugSeverity.Error | DebugSeverity.MessageBox, Resources.ERR_OUTLOOK_BAD_FOLDER_TYPE, oItemType); continue; } correctFolderSelected = true; retVal = oMapiFolder.Name; while (oMapiFolder.Parent is Outlook.MAPIFolder) { oMapiFolder = (Outlook.MAPIFolder)oMapiFolder.Parent; retVal = string.Format("{0}/", oMapiFolder.Name) + retVal; } retVal = "//" + retVal; } } catch (Exception e) { DebugAssistant.Log(DebugSeverity.Debug, e.Message); DebugAssistant.Log(DebugSeverity.Debug | DebugSeverity.MessageBox, Resources.DBG_OUTLOOK_FOLDER_NOT_SELECTED); } return(retVal); }
private void DoEnumContacts() { if (btnMode.State == AddinExpress.MSO.ADXMsoButtonState.adxMsoButtonDown) { securityManager1.DisableOOMWarnings = true; } try { Outlook.NameSpace namespace_ = OutlookApp.GetNamespace("MAPI"); if (namespace_ != null) { try { Outlook.MAPIFolder folder = namespace_.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts); if (folder != null) { try { ContactsForm frmContacts = new ContactsForm(); try { Outlook.Items items = folder.Items; if (items != null) { try { for (int i = 1; i <= items.Count; i++) { Outlook.ContactItem contact = items.Item(i) as Outlook.ContactItem; if (contact != null) { try { if (!string.IsNullOrEmpty(contact.Email1DisplayName)) { DataRow newRow = frmContacts.dsContacts.Tables["Contacts"].NewRow(); newRow["DisplayName"] = contact.Email1DisplayName; newRow["Address"] = contact.Email1Address; newRow["AddressType"] = contact.Email1AddressType; frmContacts.dsContacts.Tables["Contacts"].Rows.Add(newRow); } if (!string.IsNullOrEmpty(contact.Email2DisplayName)) { DataRow newRow = frmContacts.dsContacts.Tables["Contacts"].NewRow(); newRow["DisplayName"] = contact.Email2DisplayName; newRow["Address"] = contact.Email2Address; newRow["AddressType"] = contact.Email2AddressType; frmContacts.dsContacts.Tables["Contacts"].Rows.Add(newRow); } if (!string.IsNullOrEmpty(contact.Email3DisplayName)) { DataRow newRow = frmContacts.dsContacts.Tables["Contacts"].NewRow(); newRow["DisplayName"] = contact.Email3DisplayName; newRow["Address"] = contact.Email3Address; newRow["AddressType"] = contact.Email3AddressType; frmContacts.dsContacts.Tables["Contacts"].Rows.Add(newRow); } } finally { Marshal.ReleaseComObject(contact); } } } } finally { Marshal.ReleaseComObject(items); } } } catch { } frmContacts.ShowDialog(); frmContacts.Dispose(); } finally { Marshal.ReleaseComObject(folder); } } } finally { Marshal.ReleaseComObject(namespace_); } } } finally { if (btnMode.State == AddinExpress.MSO.ADXMsoButtonState.adxMsoButtonDown) { securityManager1.DisableOOMWarnings = false; } } }