// Event Handler for Send Button public void OnSendButton(Office.IRibbonControl control) { Explorer explorer = Globals.ThisAddIn.Application.ActiveExplorer(); Microsoft.Office.Interop.Outlook.AddressEntry addrEntry = Globals.ThisAddIn.Application.Session.CurrentUser.AddressEntry; string currentUserEmail = ""; string userFullName = ""; if (addrEntry.Type == "EX") { Microsoft.Office.Interop.Outlook.ExchangeUser currentUser = Globals.ThisAddIn.Application.Session.CurrentUser. AddressEntry.GetExchangeUser(); currentUserEmail = currentUser.PrimarySmtpAddress; userFullName = currentUser.FirstName + " " + currentUser.LastName + " (" + currentUser.PrimarySmtpAddress + ")"; } else { currentUserEmail = addrEntry.Address; userFullName = addrEntry.Name + " (" + addrEntry.Address + ")"; } MailItem mailItem = null; if (explorer != null && explorer.Selection != null && explorer.Selection.Count > 0) { object item = explorer.Selection[1]; if (item is MailItem) { mailItem = item as MailItem; } } if (mailItem != null) { string PR_TRANSPORT_MESSAGE_HEADERS = "http://schemas.microsoft.com/mapi/proptag/0x007D001E"; Microsoft.Office.Interop.Outlook.PropertyAccessor olPA = mailItem.PropertyAccessor; string headers = olPA.GetProperty(PR_TRANSPORT_MESSAGE_HEADERS); string senderEmail = ""; if (mailItem.SenderEmailType == "EX") { // Exchange email address string[] directoryParts = mailItem.SenderEmailAddress.Split('='); if (directoryParts.Length > 1) { senderEmail = directoryParts[directoryParts.Length - 1].ToLower(); } } else { senderEmail = mailItem.SenderEmailAddress; int successCode = AddPhishingEmail(senderEmail); if (successCode > 0) { MessageBox.Show("'" + senderEmail + "' was successfully added to the list of phishing email addresses and forwarded to central IS (phishing.uoregon.edu)."); string subject = "Phishing Email added to Phishing Email List"; string messageBody = "Phishing Email added to Phishing Email List '" + senderEmail + "'.<br />"; messageBody += "Added By: " + userFullName + " at " + DateTime.Now.ToString() + "<br /><br />"; messageBody += "Headers: <br />" + headers; Microsoft.Office.Interop.Outlook.MailItem eMail = (Microsoft.Office.Interop.Outlook.MailItem) Globals.ThisAddIn.Application.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); eMail.Subject = subject; eMail.To = PHISHING_EMAIL; eMail.Body = messageBody; eMail.HTMLBody = messageBody; eMail.Attachments.Add(mailItem); eMail.Importance = Microsoft.Office.Interop.Outlook.OlImportance.olImportanceHigh; ((Microsoft.Office.Interop.Outlook._MailItem)eMail).Send(); } } } }
public void Send(IRibbonControl control, string subject, OlImportance importance) { try { string[] idParts = control.Id.Split(new[] { StaticHelper.SplitSequence }, StringSplitOptions.RemoveEmptyEntries); var outlookApp = new Microsoft.Office.Interop.Outlook.Application(); MailItem newEmail = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem); // Get the recipients string[] recipients = StaticHelper.GetRecipients(idParts[0], control.Id); newEmail.To = recipients[0]; newEmail.CC = recipients[1]; newEmail.BCC = recipients[2]; newEmail.Subject = subject; newEmail.Importance = importance; string from = StaticHelper.GetApplicationSetting("MasterEmailAccount"); if (string.IsNullOrEmpty(from)) { from = StaticHelper.GetFromAccount(idParts[0]); } if (!string.IsNullOrEmpty(from)) { // Retrieve the account that has the specific SMTP address. Account account = GetAccountForEmailAddress(outlookApp, from); if (account != null) { // Use this account to send the e-mail. newEmail.SendUsingAccount = account; } } else { // See if we can determine which email account to send from based on the current folder. User may have multiple mail accounts. try { Microsoft.Office.Interop.Outlook.AddressEntry addrEntry = null; // Get the Store for CurrentFolder. Microsoft.Office.Interop.Outlook.Folder folder = outlookApp.ActiveExplorer().CurrentFolder as Microsoft.Office.Interop.Outlook.Folder; if (folder != null) { Microsoft.Office.Interop.Outlook.Store store = folder.Store; Microsoft.Office.Interop.Outlook.Accounts accounts = outlookApp.Session.Accounts; // Enumerate accounts to find account.DeliveryStore for store. foreach (Microsoft.Office.Interop.Outlook.Account account in accounts) { if (account.DeliveryStore.StoreID == store.StoreID) { addrEntry = account.CurrentUser.AddressEntry; break; } } newEmail.Sender = addrEntry; } } catch { // Intentionally swallow for now } } var context = control.Context as Inspector; if (context != null) { Inspector inspector = context; // This handles sharepoint posts if (inspector.CurrentItem is Microsoft.Office.Interop.Outlook.PostItem) { PostItem m = inspector.CurrentItem as PostItem; if (string.IsNullOrEmpty(m.Subject)) { string standardSuffix = StaticHelper.GetStandardSuffix(idParts[0]); m.Subject = !string.IsNullOrEmpty(standardSuffix) ? newEmail.Subject.Replace(standardSuffix, string.Empty) : newEmail.Subject; } else { string standardSuffix = StaticHelper.GetStandardSuffix(idParts[0]); if (!string.IsNullOrEmpty(standardSuffix)) { newEmail.Subject = newEmail.Subject.Replace(standardSuffix, string.Empty); } m.Subject = newEmail.Subject + m.Subject; } return; } if (inspector.CurrentItem is MailItem) { MailItem m = inspector.CurrentItem as MailItem; m.Importance = newEmail.Importance; if (!string.IsNullOrEmpty(from)) { // Retrieve the account that has the specific SMTP address. Account account = GetAccountForEmailAddress(outlookApp, from); if (account != null) { // Use this account to send the e-mail. m.SendUsingAccount = account; } } if (string.IsNullOrEmpty(m.Subject)) { m.Subject = newEmail.Subject; } else { string standardSuffix = StaticHelper.GetStandardSuffix(idParts[0]); if (!string.IsNullOrEmpty(standardSuffix)) { newEmail.Subject = newEmail.Subject.Replace(standardSuffix, string.Empty); } m.Subject = newEmail.Subject + m.Subject; } } } else { string html = StaticHelper.GetTemplate(control.Id); if (!string.IsNullOrEmpty(html)) { ////newEmail.HTMLBody += html; ////MailItem newEmail2 = (MailItem)outlookApp.CreateItem(OlItemType.olMailItem); ////Inspector objSigDoc = newEmail2.GetInspector; ////string s = objSigDoc.ToString(); } newEmail.Display(); } } catch (System.Exception ex) { StaticHelper.LogMessage(MessageType.Error, ex.ToString()); throw; } }