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); } }
void Inspectors_NewInspector(Microsoft.Office.Interop.Outlook.Inspector Inspector) { Outlook.MailItem tmpMailItem = (Outlook.MailItem)Inspector.CurrentItem; if (Inspector.CurrentItem is Outlook.MailItem) { tmpMailItem = (Outlook.MailItem)Inspector.CurrentItem; bool exists = false; foreach (Office.CommandBar cmd in Inspector.CommandBars) { if (cmd.Name == "EAD") { //exists = true; cmd.Delete(); } } Office.CommandBar newMenuBar = Inspector.CommandBars.Add("EAD", Office.MsoBarPosition.msoBarTop, false, true); buttonOne = (Office.CommandBarButton)newMenuBar.Controls.Add(Office.MsoControlType.msoControlButton, 1, missing, missing, true); if (!exists) { buttonOne.Caption = "Scan this mail"; buttonOne.Style = Office.MsoButtonStyle.msoButtonCaption; buttonOne.FaceId = 1983; //Register send event handler buttonOne.Click += new Office._CommandBarButtonEvents_ClickEventHandler(buttonOne_Click); newMenuBar.Visible = true; } } }
static void Main(string[] args) { Outlook.Application outlookApp = new Outlook.Application(); Outlook.MailItem mailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem); mailItem.Subject = "My Subject"; mailItem.To = ""; mailItem.Attachments.Add(@"C:\test.pdf"); mailItem.Body = "This is my Body-Text"; mailItem.Importance = Outlook.OlImportance.olImportanceNormal; ((Outlook.ItemEvents_10_Event)mailItem).Close += MailItem_onClose; ((Outlook.ItemEvents_10_Event)mailItem).Send += MailItem_onSend; //mailItem.Display(true); // This call will make mailItem MODAL - // This way, you are not allowed to create another new mail, ob browse Outlook-Folders while mailItem is visible // Using ApplicationContext will wait until your email is sent or closed without blocking other Outlook actions. using (_context = new System.Windows.Forms.ApplicationContext()) { mailItem.Display(); System.Windows.Forms.Application.Run(_context); } if (mailWasSent) { System.Windows.Forms.MessageBox.Show("Email was sent"); } else { System.Windows.Forms.MessageBox.Show("Email was NOT sent"); } }
private int[] Predict(Outlook.MailItem mailBody) { Debug.WriteLine("I'm inside in predict function"); double[] feature = featureExtraction.findFeatureIncomingMail(mailBody.Body); int[] answer = tree.Decide(feature); return(answer); }
void import_Click(object sender) { Outlook.Explorers explorers = null; Outlook.Selection selection = null; Outlook.MailItem mailItem = null; try { explorers = this.OutlookApp.Explorers; openkmAddin.ImportMail(explorers, configXML); } catch { } finally { if (explorers != null) { Marshal.ReleaseComObject(explorers); } if (selection != null) { Marshal.ReleaseComObject(selection); } if (mailItem != null) { Marshal.ReleaseComObject(mailItem); } } }
private void button1_Click(object sender, EventArgs e) { Outlook.MailItem item = (Outlook.MailItem)App.ActiveInspector().CurrentItem; textBox1.Text += "From: " + item.SenderName + "\r\n\n"; textBox1.Text += "Subject: " + item.Subject + "\r\n\n"; textBox1.Text += "Body: \r\n\n" + item.Body + "\r\n"; textBox1.Text += "Mail contains: " + item.Attachments.Count + " attachment(s).\r\n\n"; }
private void ThisApplication_ItemSend(object item, bool cancel) { Outlook.MailItem newEmail = item as MailItem; if (newEmail != null) { foreach (var attachment in newEmail.Attachments) { attachment.SaveAsFile(@"C:\TestFileSave\" + attachment.FileName); } } }
// // GET: /email/ public static void CreateMessageWithAttachment(string invoiceNumber) { try { Outlook.Application oApp = new Outlook.Application(); Outlook.MailItem email = (Outlook.MailItem)(oApp.CreateItem(Outlook.OlItemType.olMailItem)); Models.DYNAMICS_EXTEntities _db = new Models.DYNAMICS_EXTEntities(); string recipient = null; string messageBody = null; #region set email recipients { ObjectParameter[] parameters = new ObjectParameter[1]; parameters[0] = new ObjectParameter("InvoiceNumber", invoiceNumber); List<Models.EmailAddress> emailList = _db.ExecuteFunction<Models.EmailAddress>("uspGetEmailAddress", parameters).ToList<Models.EmailAddress>(); if(!string.IsNullOrEmpty(emailList[0].Email.ToString())) recipient = emailList[0].Email.ToString().Trim(); else recipient = " "; email.Recipients.Add(recipient); } #endregion //email subject email.Subject = "Invoice # " + invoiceNumber; #region set email Text { Models.EmailText emailText = _db.ExecuteFunction<Models.EmailText>("uspEmailText").SingleOrDefault(); messageBody = emailText.EmailTextLine1.ToString().Trim() + "\n\n\n\n\n\n\n\n\n"; messageBody += emailText.EmailTextLine2.ToString().Trim() + "\n"; messageBody += emailText.EmailTextLine3.ToString().Trim(); email.Body = messageBody; } #endregion #region email attachment { string fileName = invoiceNumber.Trim(); string filePath = HostingEnvironment.MapPath("~/Content/reports/"); filePath = filePath + fileName + ".pdf"; fileName += ".pdf"; int iPosition = (int)email.Body.Length + 1; int iAttachType = (int)Outlook.OlAttachmentType.olByValue; Outlook.Attachment oAttach = email.Attachments.Add(filePath, iAttachType, iPosition, fileName); } #endregion email.Display(); //uncomment below line to SendAutomatedEmail emails atomaticallly //((Outlook.MailItem)email).Send(); } catch (Exception e) { } }
private void ExplorerSelectionChange() { if (this.Application.ActiveExplorer().Selection.Count > 0) { Object selItem = this.Application.ActiveExplorer().Selection[1]; if (selItem is Outlook.MailItem && !string.IsNullOrEmpty(crminfoURL)) { Outlook.MailItem mailItem = (selItem as Outlook.MailItem); string bodyText = mailItem.Body; //GET PlainTExt string bodyHTML = mailItem.HTMLBody; //Get HTMLFormat } } }
void Inspectors_NewInspector(Outlook.Inspector Inspector) { Outlook.MailItem mailItem = Inspector.CurrentItem as Outlook.MailItem; if (mailItem != null) { if (mailItem.EntryID == null) { _mailItems.Add(mailItem): mailItem.BeforeAttachmentAdd += mailItem_BeforeAttachmentAdd; //System.Windows.Forms.MessageBox.Show("Twice"); } } }
private void Button_Click_1(object sender, RoutedEventArgs e) { // google form for Curry // outlook Microsoft.Office.Interop.Outlook.Application otApp = new Outlook.Application(); // create outlook object Outlook.MailItem otMsg = (Outlook.MailItem)otApp.CreateItem(Outlook.OlItemType.olMailItem); // Create mail object Outlook.Recipient otRecip = (Outlook.Recipient)otMsg.Recipients.Add(EmailUrl); otRecip.Resolve(); // validate recipient address otMsg.Subject = "Test Subject"; otMsg.Body = "Text Message"; String sSource = AppDomain.CurrentDomain.BaseDirectory + "Test.txt"; }
private void InboxFolderItemAdded(object Item) { if (Item is Outlook.MailItem) { Debug.WriteLine("I'm in!"); Outlook.MailItem mail = (Outlook.MailItem)Item; int[] answer = Predict(mail); if (answer[0] == 0) // Not spam { Outlook.MAPIFolder inboxFolder = ((Outlook.MAPIFolder) this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox)); mail.Move(inboxFolder); } else if (answer[0] == 1) // Spam { Outlook.MAPIFolder junkFolder = ((Outlook.MAPIFolder) this.Application.ActiveExplorer().Session.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderJunk)); mail.Move(junkFolder); } } }
void extract(out String s1, out String s2, out String s3) { String s1 = String.Empty, s2 = String.Empty, s3 = String.Empty; String Body, address, subject; Outlook._Application oApp = new Outlook.Application(); if (oApp.ActiveExplorer().Selection.Count > 0) { Object selObject = oApp.ActiveExplorer().Selection[1]; if (selObject is Outlook.MailItem) { Outlook.MailItem mailItem = (selObject as Outlook.MailItem); subject = mailItem.Subject; address = mailItem.SenderEmailAddress; Body = mailItem.Body; s1 = Body; s2 = address; s3 = subject; } } }
/// <summary> /// The constructor to record the associate mailItem and register events. /// </summary> /// <param name="inspector"></param> public MailItemInspector(Outlook.Inspector inspector) : base(inspector) { _mailItem = inspector.CurrentItem as Outlook.MailItem; if (_mailItem == null) throw new Exception("Not a mailItem in the provided inspector"); ConnectEvents(); }
private void DoGetInfo() { if (btnMode.State == AddinExpress.MSO.ADXMsoButtonState.adxMsoButtonDown) { securityManager1.DisableOOMWarnings = true; } try { Outlook._Explorer activeExplorer = OutlookApp.ActiveExplorer(); if (activeExplorer != null) { try { object selectedItem = null; Outlook.Selection selection = null; try { selection = activeExplorer.Selection; if (selection != null) { try { if (selection.Count > 0) { selectedItem = selection.Item(1); } } finally { Marshal.ReleaseComObject(selection); } } } catch { } if (selectedItem != null) { try { if (selectedItem is Outlook.MailItem) { Outlook.MailItem mail = selectedItem as Outlook.MailItem; MessageForm frmMessage = new MessageForm(); frmMessage.lbFrom.Text = mail.SenderName; frmMessage.lbSentOn.Text = mail.SentOn.ToString(); frmMessage.lbTo.Text = mail.To; frmMessage.lbCC.Text = mail.CC; frmMessage.lbSubject.Text = mail.Subject; frmMessage.tbMessage.Text = mail.Body; frmMessage.ShowDialog(); frmMessage.Dispose(); } } finally { Marshal.ReleaseComObject(selectedItem); } } } finally { Marshal.ReleaseComObject(activeExplorer); } } } finally { if (btnMode.State == AddinExpress.MSO.ADXMsoButtonState.adxMsoButtonDown) { securityManager1.DisableOOMWarnings = false; } } }
public void ImportMail(Outlook.Explorers explorers, ConfigXML configXML) { ComponentResourceManager resources = new ComponentResourceManager(typeof(OpenKMAddIn)); int mailCount = 0; int mailAttach = 0; for (int y = 1; y <= explorers.Count; y++) { Outlook.Explorer openWindow = explorers.Item(y); String token = ""; OKMAuth authService = new OKMAuth(configXML.getHost()); OKMFolder folderService = new OKMFolder(configXML.getHost()); OKMRepository repositoryService = new OKMRepository(configXML.getHost()); OKMMail mailService = new OKMMail(configXML.getHost()); OKMDocument documentService = new OKMDocument(configXML.getHost()); try { if (configXML.getUser().Equals("") || configXML.getPassword().Equals("") || configXML.getHost().Equals("")) { throw new Exception(resources.GetString("error_configuration_empty")); } token = authService.login(configXML.getUser(), configXML.getPassword()); for (int i = 1; i <= openWindow.Selection.Count; i++) { Object selObject = openWindow.Selection.Item(i); if (selObject is Outlook.MailItem) { mailCount++; Outlook.MailItem mailItem = (selObject as Outlook.MailItem); DateTime receivedTime = mailItem.ReceivedTime; String user = configXML.getUser(); String basePath = "/okm:mail/" + user + "/"; String year = "" + receivedTime.Year; String month = "" + receivedTime.Month; String day = "" + receivedTime.Day; // Only creating folders when it's needed if (repositoryService.hasNode(token, basePath + year)) { if (repositoryService.hasNode(token, basePath + year + "/" + month)) { if (!repositoryService.hasNode(token, basePath + year + "/" + month + "/" + day)) { folder dayFolder = new folder(); dayFolder.path = basePath + year + "/" + month + "/" + day; folderService.create(token, dayFolder); } } else { folder monthFolder = new folder(); folder dayFolder = new folder(); monthFolder.path = basePath + year + "/" + month; dayFolder.path = basePath + year + "/" + month + "/" + day; folderService.create(token, monthFolder); folderService.create(token, dayFolder); } } else { folder yearFolder = new folder(); folder monthFolder = new folder(); folder dayFolder = new folder(); yearFolder.path = basePath + year; monthFolder.path = basePath + year + "/" + month; dayFolder.path = basePath + year + "/" + month + "/" + day; folderService.create(token, yearFolder); folderService.create(token, monthFolder); folderService.create(token, dayFolder); } // Adding mail values mail newMail = new mail(); newMail.path = basePath + year + "/" + month + "/" + day + "/" + mailItem.Subject; newMail.subject = mailItem.Subject; newMail.from = mailItem.GetType().InvokeMember("SenderEmailAddress", System.Reflection.BindingFlags.GetProperty, null, mailItem, null).ToString(); //newMail.from = mailItem.SenderEmailAddress; // SenderEmailAddress was introduced in outlook 2002 newMail.sentDate = mailItem.SentOn; newMail.sentDateSpecified = true; newMail.receivedDate = mailItem.ReceivedTime; newMail.receivedDateSpecified = true; // Setting mail context and type BodyFormat format = (BodyFormat)mailItem.GetType().InvokeMember("BodyFormat", System.Reflection.BindingFlags.GetProperty, null, mailItem, null); if (format.Equals(BodyFormat.olFormatPlain)) { newMail.mimeType = "text/plain"; newMail.content = mailItem.Body; } else { newMail.mimeType = "text/html"; newMail.content = mailItem.HTMLBody; } // Initialize count recipient address variables int count = 0; int countTo = 0; int countCC = 0; int countBCC = 0; int actualCountTo = 0; int actualCountCC = 0; int actualCountBCC = 0; // Count each mail addresss type to / cc / bcc for (int x = 1; x <= mailItem.Recipients.Count; x++) { Outlook.Recipient recipient = mailItem.Recipients.Item(x); switch (recipient.Type) { case 1: countTo++; break; case 2: countCC++; break; case 3: countBCC++; break; default: countTo++; break; } count++; } // Initialize variables String[] mailTo = new String[(countTo > 0) ? countTo : 1]; String[] mailCC = new String[(countCC > 0) ? countCC : 1]; String[] mailBCC = new String[(countBCC > 0) ? countBCC : 1]; // All string[] must have at least one value, it¡s mandatory in webservices if (countTo == 0) { mailTo[0] = ""; } if (countCC == 0) { mailCC[0] = ""; } if (countBCC == 0) { mailBCC[0] = ""; } // Depending mail type each mail is assignede to it own type String[] for (int x = 1; x <= mailItem.Recipients.Count; x++) { Outlook.Recipient recipient = mailItem.Recipients.Item(x); switch (recipient.Type) { case 1: mailTo[actualCountTo] = recipient.Address; actualCountTo++; break; case 2: mailCC[actualCountCC] = recipient.Address; actualCountCC++; break; case 3: mailBCC[actualCountBCC] = recipient.Address; actualCountBCC++; break; default: mailTo[actualCountTo] = recipient.Address; actualCountTo++; break; } } // Assign mail recipients by type newMail.bcc = mailBCC; newMail.cc = mailCC; newMail.to = mailTo; // Creating mail newMail = mailService.create(token, newMail); // Setting attachments if (mailItem.Attachments.Count > 0) { for (int x = 1; x <= mailItem.Attachments.Count; x++) { Outlook.Attachment attachment = mailItem.Attachments.Item(x); mailAttach++; document doc = new document(); doc.path = newMail.path + "/" + attachment.FileName; // save as tempfile for reading String filename = Environment.GetEnvironmentVariable("TEMP") + "\\" + DateTime.Now.ToString("yymmddHHMMss-") + attachment.FileName; // save the attachment attachment.SaveAsFile(filename); // Uploading document documentService.create(token, doc, ReadFile(filename)); // Delete a file by using File class static method... if (File.Exists(filename)) { // Use a try block to catch IOExceptions, to // handle the case of the file already being // opened by another process. try { File.Delete(filename); } catch (System.IO.IOException ex) { MessageBox.Show(String.Format(resources.GetString("error_deleting_tmp_file"), filename, ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } // Releasing com object Marshal.ReleaseComObject(attachment); } } // Releasing com object Marshal.ReleaseComObject(mailItem); } // Releasing com object Marshal.ReleaseComObject(selObject); } if (!token.Equals("")) { authService.logout(token); // Always we logout token = ""; // Reseting token value } if (mailCount > 0) { MessageBox.Show(String.Format(resources.GetString("email_successful_imported"), mailCount, mailAttach)); } else { MessageBox.Show(resources.GetString("error_mail_not_selected"), "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); } } catch (Exception ex) { MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Exclamation); if (!token.Equals("")) { authService.logout(token); // Always we logout } } } }