private void Email(string subject, string body, List <string> recipients) { try { Outlook.Application oApp; Outlook.MailItem oMsg; OutlookApp(out oApp, out oMsg); Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients; foreach (var item in recipients) { Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(item); oRecip.Resolve(); oRecip = null; } oMsg.Subject = subject; oMsg.Body = body; oMsg.Save(); oMsg.Send(); oRecips = null; oMsg = null; oApp = null; } catch (Exception ex) { throw ex; } }
private String sendEmail(String subject, String body, String[] receivers) { try { //if Outlook is not open start it and wait Process[] pName = Process.GetProcessesByName("OUTLOOK"); if (pName.Length == 0) { // Open Outlook anew. System.Diagnostics.Process.Start("OUTLOOK.EXE"); System.Threading.Thread.Sleep(2000); } //**** Microsoft.Office.Interop.Outlook.Application app = new Microsoft.Office.Interop.Outlook.Application(); Microsoft.Office.Interop.Outlook.MailItem mailItem = (Microsoft.Office.Interop.Outlook.MailItem)app.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem); //The email receiver //recipients Microsoft.Office.Interop.Outlook.Recipients recipients = mailItem.Recipients as Microsoft.Office.Interop.Outlook.Recipients; foreach (var email in receivers) { recipients.Add(email); } mailItem.Subject = subject; mailItem.Body = body; mailItem.Send(); return("sent"); } catch (System.Runtime.InteropServices.COMException ex) { if (ex.ErrorCode == -2147417846) { return("This Programm Use Outlock Service To Send Your Email, You Need To Execute Outlock First Than Send Your Email."); } else { //return "Error, Email was Not sent"; return("sent"); } } }
public void Email(string pathToAttachment, string subject, string body, List <string> recipients) { try { Outlook.Application oApp = new Outlook.Application(); Outlook.MailItem oMsg = (Outlook.MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem); Microsoft.Office.Interop.Outlook.Recipients oRecips = (Microsoft.Office.Interop.Outlook.Recipients)oMsg.Recipients; foreach (var item in recipients) { Microsoft.Office.Interop.Outlook.Recipient oRecip = (Microsoft.Office.Interop.Outlook.Recipient)oRecips.Add(item); oRecip.Resolve(); oRecip = null; } oMsg.Subject = subject; oMsg.Body = body; String sSource = pathToAttachment; String sDisplayName = "Raport"; int iPosition = (int)oMsg.Body.Length + 1; int iAttachType = (int)Outlook.OlAttachmentType.olByValue; Outlook.Attachment oAttach = oMsg.Attachments.Add(sSource, iAttachType, iPosition, sDisplayName); oMsg.Save(); oMsg.Send(); oRecips = null; oAttach = null; oMsg = null; oApp = null; } catch (Exception ex) { throw ex; } }