public OutlookContactManager() { if (this.IsInstalled) { olSecurityManager = new AddinExpress.Outlook.SecurityManager(); try { _outlookApplication = new Microsoft.Office.Interop.Outlook.ApplicationClass(); } catch { errorCreatingOutlookApp = true; return; } try { olSecurityManager.ConnectTo(_outlookApplication); } catch { } } }
private void DoOOM() { object nameSpace = null, items = null, folder = null; object outlookApp = null, mailItem = null; tbInfo.Clear(); Type olType = Type.GetTypeFromProgID("Outlook.Application", false); if (olType != null) { try { outlookApp = Activator.CreateInstance(olType); if (outlookApp != null) { if (rbOFF.Checked) { securityManager1.ConnectTo(outlookApp); // switch OFF securityManager1.DisableOOMWarnings = true; } try { nameSpace = outlookApp.GetType().InvokeMember("GetNamespace", BindingFlags.InvokeMethod, null, outlookApp, new object[] { "MAPI" }); folder = nameSpace.GetType().InvokeMember("GetDefaultFolder", BindingFlags.InvokeMethod, null, nameSpace, new object[] { OlDefaultFolders.olFolderInbox }); items = folder.GetType().InvokeMember("Items", BindingFlags.GetProperty, null, folder, null); mailItem = items.GetType().InvokeMember("Item", BindingFlags.InvokeMethod, null, items, new object[] { 1 }); if (mailItem != null) { string[] lines = new string[4]; lines[0] = ("*** First message in Microsoft Outlook inbox (Outlook Object Model) ***"); lines[1] = ""; lines[2] = "From: " + Convert.ToString(mailItem.GetType().InvokeMember("SenderName", BindingFlags.GetProperty, null, mailItem, null)); lines[3] = "Subject: " + Convert.ToString(mailItem.GetType().InvokeMember("Subject", BindingFlags.GetProperty, null, mailItem, null)); tbInfo.Lines = lines; Marshal.ReleaseComObject(mailItem); } } catch { } } } finally { if (items != null) { Marshal.ReleaseComObject(items); } if (folder != null) { Marshal.ReleaseComObject(folder); } if (nameSpace != null) { Marshal.ReleaseComObject(nameSpace); } if (rbOFF.Checked) { // switch ON securityManager1.DisableOOMWarnings = false; securityManager1.Disconnect(outlookApp); } if (outlookApp != null) { Marshal.ReleaseComObject(outlookApp); } } } }
private void toolBar1_ButtonClick(object sender, System.Windows.Forms.ToolBarButtonClickEventArgs e) { switch (e.Button.Text) { case "Send": Outlook._Application outlookApp = null; Outlook._MailItem newMail = null; Outlook.Recipient recipient = null; Outlook.Recipients recipients = null; Type olType = Type.GetTypeFromProgID("Outlook.Application", false); if (olType != null) { try { outlookApp = Marshal.GetActiveObject("Outlook.Application") as Outlook._Application; } catch { } try { if (outlookApp == null) { outlookApp = Activator.CreateInstance(olType) as Outlook._Application; } } catch { } if (outlookApp != null) { if (tbtnMode.Pushed) { securityManager1.ConnectTo(outlookApp); securityManager1.DisableOOMWarnings = true; } try { newMail = outlookApp.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; if (newMail != null) { try { recipients = newMail.Recipients; if (recipients != null) { try { if (txbTO.Text != string.Empty) { recipient = recipients.Add(txbTO.Text); if (recipient != null) { try { recipient.Type = (int)Outlook.OlMailRecipientType.olTo; recipient.Resolve(); } finally { Marshal.ReleaseComObject(recipient); } } } if (txbCC.Text != string.Empty) { recipient = recipients.Add(txbCC.Text); if (recipient != null) { try { recipient.Type = (int)Outlook.OlMailRecipientType.olCC; recipient.Resolve(); } finally { Marshal.ReleaseComObject(recipient); } } } if (txbBCC.Text != string.Empty) { recipient = recipients.Add(txbBCC.Text); if (recipient != null) { try { recipient.Type = (int)Outlook.OlMailRecipientType.olBCC; recipient.Resolve(); } finally { Marshal.ReleaseComObject(recipient); } } } newMail.Subject = txbSubject.Text; newMail.Body = txbMail.Text; newMail.Send(); } finally { Marshal.ReleaseComObject(recipients); } } Marshal.ReleaseComObject(newMail); newMail = null; MessageBox.Show("The message has been sent successfully.", "Send Mail", MessageBoxButtons.OK, MessageBoxIcon.Information); } catch (Exception err) { if (newMail != null) { Marshal.ReleaseComObject(newMail); } MessageBox.Show(err.Message, err.Source, MessageBoxButtons.OK, MessageBoxIcon.Error); } } } finally { if (tbtnMode.Pushed) { securityManager1.DisableOOMWarnings = false; securityManager1.Disconnect(outlookApp); } if (outlookApp != null) { Marshal.ReleaseComObject(outlookApp); } } } } break; case "Security": if (tbtnMode.Pushed) { tbtnMode.ImageIndex = 2; } else { tbtnMode.ImageIndex = 1; } break; } }