Пример #1
0
        public void SendOutlook(string template, string subject, string to, string cc, string bcc, string body, DataTable dt, List <string> attachments)
        {
            Outlook.Application app = new Outlook.Application();
            app.ActiveWindow();
            Outlook.MailItem mail;
            mail = app.CreateItemFromTemplate(template.Contains(@":\")
                                ? template
                                : System.IO.Directory.GetCurrentDirectory() + '\\' + template) as Outlook.MailItem;
            mail.Subject = String.IsNullOrEmpty(subject) ? (String.IsNullOrEmpty(mail.Subject) ? "Untitled" : mail.Subject) : subject;
            mail.To      = String.IsNullOrEmpty(to) ? mail.To : to;
            mail.CC      = String.IsNullOrEmpty(cc) ? mail.CC : cc;
            mail.BCC     = String.IsNullOrEmpty(bcc) ? mail.BCC : bcc;
            attachments.ForEach(x => mail.Attachments.Add(x));
            if (String.IsNullOrEmpty(mail.To) && String.IsNullOrEmpty(mail.CC) && String.IsNullOrEmpty(mail.BCC))
            {
                throw new System.Exception("Error, there is no recepients specified");
            }

            mail.HTMLBody = mail.HTMLBody.Replace("{message}", body);
            mail.HTMLBody = mail.HTMLBody.Replace("{table}", dt != null ? (dt.Rows.Count > 0 ? GetHTMLTable(dt) : "") : "");
            mail.Send();
            app.GetNamespace("MAPI").SendAndReceive(true);

            var releaseResult = Marshal.ReleaseComObject(app);
        }
Пример #2
0
 /*
  * Uses the email and opens an outlook window with subject, body, to address, and attachments
  * filled in.
  */
 public void openOutlookWindow()
 {
     try
     {
         Outlook.Application oApp      = new Outlook.Application();
         Outlook._MailItem   oMailItem = (Outlook._MailItem)oApp.CreateItem(Outlook.OlItemType.olMailItem);
         oMailItem.To      = toAddress;
         oMailItem.Subject = subject;
         oMailItem.Body    = body;
         oMailItem.CC      = cc;
         if (attach1 != null)
         {
             oMailItem.Attachments.Add(attach1);
         }
         if (attach2 != null)
         {
             oMailItem.Attachments.Add(attach2);
         }
         oMailItem.Display(true);
         oApp.ActiveWindow();
     }catch (System.Exception ex)
     {
         MessageBox.Show("Outlook Error: " + ex.Message, "Error with outlook window", MessageBoxButtons.OK, MessageBoxIcon.Error);
         Console.WriteLine("Error with outlook." + ex.Message);
     }
 }