public static bool Send(string To, string CC, string BCC, string Subject, string Body, string Attachment) { try { if (To == string.Empty && CC == string.Empty && BCC == string.Empty) { return(false); } Outlook._Application OlApp = new Outlook.ApplicationClass(); Outlook.MailItem OlNewMail = (Outlook.MailItem)OlApp.CreateItem(Outlook.OlItemType.olMailItem); OlNewMail.To = To; OlNewMail.CC = CC; OlNewMail.BCC = BCC; OlNewMail.Subject = Subject; OlNewMail.Body = Body; if (Attachment != string.Empty) { OlNewMail.Attachments.Add(Attachment, Type.Missing, Type.Missing, Type.Missing); } OlNewMail.Send(); return(true); } catch (System.Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); return(false); } }
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); }
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); }
private IEnumerable<Employees> EnumerateGAL() { Outlook.Application ol = new Outlook.ApplicationClass(); /*Outlook.AddressList gal = Application.Session.GetGlobalAddressList(); won't work because '_Application' needs to be instantiated using an object before using. */ Outlook.AddressList gal = ol.Session.GetGlobalAddressList(); /*Declaring a list emp of type Employees.*/ List<Employees> emp = new List<Employees>(); if (gal != null) { for (int i = 1; i <= Math.Min(100, gal.AddressEntries.Count - 1); i++) { Outlook.AddressEntry addrEntry = gal.AddressEntries[i]; if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeUserAddressEntry || addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeRemoteUserAddressEntry) { Outlook.ExchangeUser exchUser =addrEntry.GetExchangeUser(); /*Storing fetched records in the list.*/ emp.Add(new Employees {EmployeeName = exchUser.Name,EmployeeEmail = exchUser.PrimarySmtpAddress}); } if (addrEntry.AddressEntryUserType == Outlook.OlAddressEntryUserType.olExchangeDistributionListAddressEntry) { Outlook.ExchangeDistributionList exchDL = addrEntry.GetExchangeDistributionList(); } } } displayListOfOutLookUsers = emp; supplierDataBindingSource.DataSource = displayListOfOutLookUsers.Select(l => new { l.EmployeeName, l.EmployeeEmail }); dataGridView1.AutoResizeColumns( DataGridViewAutoSizeColumnsMode.DisplayedCells); dataGridView1.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.AllCells; int j = 0; foreach (DataGridViewColumn c in dataGridView1.Columns) { c.SortMode = DataGridViewColumnSortMode.Automatic; j += c.Width; } dataGridView1.Width = j + dataGridView1.RowHeadersWidth + 232; this.Width = dataGridView1.Width + 40; return displayListOfOutLookUsers; }