/// <summary> ///Refere to where the code was taken from ///https://www.add-in-express.com/creating-addins-blog/2011/09/08/outlook-fill-recipients-programmatically/ /// </summary> /// <param name="mail"></param> /// <returns></returns> private bool AddRecipients(Outlook.MailItem mail) { bool retValue = false; Outlook.Recipients recipients = null; Outlook.Recipient recipientTo = null; Outlook.Recipient recipientCC = null; Outlook.Recipient recipientBCC = null; try { recipients = mail.Recipients; // first, we remove all the recipients of the e-mail while (recipients.Count != 0) { recipients.Remove(1); } // now we add new recipietns to the e-mail foreach (IEmailAddress address in this.ToAddesses) { recipientTo = recipients.Add((((EmailAddress)address).Address)); recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo; } foreach (IEmailAddress address in this.CcAddresses) { recipientTo = recipients.Add((((EmailAddress)address).Address)); recipientTo.Type = (int)Outlook.OlMailRecipientType.olCC; } foreach (IEmailAddress address in this.BccAddress) { recipientTo = recipients.Add((((EmailAddress)address).Address)); recipientTo.Type = (int)Outlook.OlMailRecipientType.olBCC; } retValue = recipients.ResolveAll(); } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (recipientBCC != null) { Marshal.ReleaseComObject(recipientBCC); } if (recipientCC != null) { Marshal.ReleaseComObject(recipientCC); } if (recipientTo != null) { Marshal.ReleaseComObject(recipientTo); } if (recipients != null) { Marshal.ReleaseComObject(recipients); } } return(retValue); }
private Boolean sendMail() { try { // Create the Outlook application. Outlook.Application oApp = new Outlook.Application(); // Create a new mail item. Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); Outlook.Inspector oInspector = oMsg.GetInspector; // Set HTMLBody. //add the body of the email oMsg.HTMLBody = string.Format(GetLabel("Mail.0001"), txFor.Text, txFor.Text, txReason.Text, FormUser.FirstName + " " + FormUser.LastName); //Add an attachment. //String sDisplayName = "MyAttachment"; //int iPosition = (int)oMsg.Body.Length + 1; //int iAttachType = (int)Outlook.OlAttachmentType.olByValue; //now attached the file //Outlook.Attachment oAttach = oMsg.Attachments.Add(@"C:\\fileName.jpg", iAttachType, iPosition, sDisplayName); //Subject line oMsg.Subject = string.Format(GetLabel("Mail.0002"), txFor.Text); // Add a recipient. Outlook.Recipients oRecips = (Outlook.Recipients)oMsg.Recipients; //oRecips.Add(mailAlias); //oRecips.ResolveAll(); // Change the recipient in the next line if necessary. //Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(mailAlias); string[] alias = mailAlias.Split(new string[] { "," }, StringSplitOptions.None); foreach (String recipient in alias) { Outlook.Recipient oRecip = (Outlook.Recipient)oRecips.Add(recipient); if (!oRecip.Resolve()) { oRecips.Remove(oRecips.Count); } } //oRecip.Resolve(); // Send. oMsg.Send(); // Clean up. //oRecip = null; oRecips = null; oMsg = null; oApp = null; return(true); }//end of try block catch (Exception ex) { mailError = ex.Message; return(false); } }
void ItemSend_BeforeSend(object item, ref bool cancel) { ThisRibbonCollection ribbonCollection = Globals.Ribbons[Globals.ThisAddIn.Application.ActiveInspector()]; Outlook.MailItem mailItem = (Outlook.MailItem)item; // Check to see if track button is enabled. if (ribbonCollection.GetNotifyRibbon.trackToggleBtn.Checked) { if (mailItem != null) { Outlook.Recipients recips = mailItem.Recipients; string[] emailAdrses = new String[recips.Count]; int[] emailTypes = new int[recips.Count]; int arrayItr = 0; // Lets add email addresses and their types in new arrays, // modify the address and remove current recipients. while (recips.Count > 0) { Outlook.Recipient recip = recips[1]; if (recip.Address.Split('@')[1].ToLower().Contains("getnotify.com")) { emailAdrses[arrayItr] = recip.Address; } else { emailAdrses[arrayItr] = recip.Address + ".getnotify.com"; } emailTypes[arrayItr] = recip.Type; arrayItr++; recips.Remove(1); } // Add new recipients using the arrays we populated before. for (int i = 0; i < emailAdrses.Length; i++) { // We use recipient's address as its name. Outlook.Recipient newRecip = recips.Add(emailAdrses[i]); newRecip.AddressEntry.Address = emailAdrses[i]; newRecip.Type = emailTypes[i]; } } } cancel = false; }
/// <summary> /// Method for creation of Outlook Mail Message. /// </summary> /// <param name="fromAddress">Represents e-mail address that is used for sending e-mail</param> /// <param name="toList">Array of the e-mail adresses that will be used in To: field of the e-mauk message</param> /// <param name="ccList">Array of the e-mail adresses that will be used in Cc: field of the e-mauk message</param> /// <param name="bccList">Array of the e-mail adresses that will be used in Bcc: field of the e-mauk message</param> /// <param name="attachName">This parameter represents list of the attachment names how it will be displayed in e-mail</param> /// <param name="attachLocation">This parameter represents location of the attachment</param> /// <returns>Outlook._MailItem object</returns> /// public static Outlook._MailItem CreateOutlookEmail(String fromAddress, List <String> toList, List <String> ccList, List <String> bccList, List <String> attachments, String attachLocation) { Outlook.Application application = new Outlook.Application(); Outlook.MailItem mail = application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; mail.SendUsingAccount = OutlookManage.GetAccount(application, fromAddress); if (toList != null) { mail.Subject = "Please ignore this message. This is created for E2E test purposes. E-mail to: " + toList[0]; mail.Body = "This is body of-email from Exchange account to " + toList[0]; } else { mail.Subject = "Please ignore this message. This is created for E2E test purposes. E-mail to empty To List"; mail.Body = "This is body of-email from Exchange account to Empty to List"; } if (!attachments.Count.Equals(0)) { foreach (String attachName in attachments) { Console.WriteLine("Add attachment to e-mail"); int iPosition = (int)mail.Body.Length + 1; int iAttachType = (int)Outlook.OlAttachmentType.olByValue; mail.Attachments.Add(attachLocation, iAttachType, iPosition, attachName); } } Outlook.Recipients recipients = null; Outlook.Recipient recipientTo = null; Outlook.Recipient recipientCC = null; Outlook.Recipient recipientBCC = null; try { recipients = mail.Recipients; while (recipients.Count != 0) { recipients.Remove(1); } if (toList != null) { foreach (String toAddress in toList) { recipientTo = recipients.Add(toAddress); recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo; } } if (ccList != null) { foreach (String ccAddress in ccList) { recipientCC = recipients.Add(ccAddress); recipientCC.Type = (int)Outlook.OlMailRecipientType.olCC; } } if (bccList != null) { foreach (String bccAddress in bccList) { recipientCC = recipients.Add(bccAddress); recipientCC.Type = (int)Outlook.OlMailRecipientType.olBCC; } } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (recipientBCC != null) { Marshal.ReleaseComObject(recipientBCC); } if (recipientCC != null) { Marshal.ReleaseComObject(recipientCC); } if (recipientTo != null) { Marshal.ReleaseComObject(recipientTo); } if (recipients != null) { Marshal.ReleaseComObject(recipients); } } return(mail); }
/// <summary> /// Method for creation of Outlook Mail Message with empty subject. /// </summary> /// <param name="fromAddress">Represents e-mail address that is used for sending e-mail</param> /// <param name="toList">Array of the e-mail adresses that will be used in To: field of the e-mauk message</param> /// <param name="ccList">Array of the e-mail adresses that will be used in Cc: field of the e-mauk message</param> /// <param name="bccList">Array of the e-mail adresses that will be used in Bcc: field of the e-mauk message</param> /// <param name="attachName">This parameter represents name of the attachment how it will be displayed in e-mail</param> /// <param name="attachLocation">This parameter represents location of the attachment</param> /// <returns>Outlook._MailItem object</returns> /// public static Outlook._MailItem CreateOutlookEmailWithEmptySubject(String fromAddress, List <String> toList, List <String> ccList, List <String> bccList, List <String> attachments, String attachLocation) { Outlook.Application application = new Outlook.Application(); Outlook.MailItem mail = application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem; mail.SendUsingAccount = OutlookManage.GetAccount(application, fromAddress); mail.Subject = null; mail.Body = "Body"; Outlook.Recipients recipients = null; Outlook.Recipient recipientTo = null; Outlook.Recipient recipientCC = null; Outlook.Recipient recipientBCC = null; try { recipients = mail.Recipients; while (recipients.Count != 0) { recipients.Remove(1); } if (toList != null) { foreach (String toAddress in toList) { recipientTo = recipients.Add(toAddress); recipientTo.Type = (int)Outlook.OlMailRecipientType.olTo; } } if (ccList != null) { foreach (String ccAddress in ccList) { recipientCC = recipients.Add(ccAddress); recipientCC.Type = (int)Outlook.OlMailRecipientType.olCC; } } if (bccList != null) { foreach (String bccAddress in bccList) { recipientCC = recipients.Add(bccAddress); recipientCC.Type = (int)Outlook.OlMailRecipientType.olBCC; } } } catch (Exception ex) { System.Windows.Forms.MessageBox.Show(ex.Message); } finally { if (recipientBCC != null) { Marshal.ReleaseComObject(recipientBCC); } if (recipientCC != null) { Marshal.ReleaseComObject(recipientCC); } if (recipientTo != null) { Marshal.ReleaseComObject(recipientTo); } if (recipients != null) { Marshal.ReleaseComObject(recipients); } } if (!attachments.Count.Equals(0)) { foreach (String attachName in attachments) { String sSource = attachLocation; String sDisplayName = attachName; int iPosition = (int)mail.Body.Length + 1; int iAttachType = (int)Outlook.OlAttachmentType.olByValue; mail.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName); } } return(mail); }