Пример #1
0
        private void btnCreateOutlookMail_Click(object sender, EventArgs e)
        {
            using (var outlook = new STC.Automation.Office.Outlook.Application())
            {
                using (var mail = (STC.Automation.Office.Outlook.MailItem)outlook.CreateItem(STC.Automation.Office.Outlook.Enums.ItemType.MailItem))
                {
                    using (var recipient = mail.Recipients.Add("*****@*****.**"))
                    {
                        recipient.Type = (long)STC.Automation.Office.Outlook.Enums.MailRecipientType.To;
                    }

                    mail.Subject = "Test email";
                    mail.Body    = "Hello from automation!";

                    mail.Closing += Mail_Closing;
                    mail.Sending += Mail_Sending;

                    // Doing this has the same (or at least similar) effect as calling mail.Display(), in that it shows the message and prevents
                    // mail.Display(true) from making the inspector modal. This in turn means that the method used here for fielding events will
                    // not work.
                    // mail.Inspector.Activate();


                    // create temporary files for attachments as the method only accepts a filepath

                    // first attachment is just added and the attachment object disposed
                    var root = Path.Combine(Path.GetTempPath(), "STC.Automation.Office");
                    try { Directory.CreateDirectory(root); } catch { }
                    var filename = Path.Combine(root, "attached-image.jpg");
                    using (var writer = new FileStream(filename, FileMode.Create))
                        Properties.Resources.attached_image.Save(writer, System.Drawing.Imaging.ImageFormat.Jpeg);
                    mail.Attachments.Add(filename).Dispose();

                    // second attachment will be added inline in the message body
                    filename = Path.Combine(root, "embedded-image.png");
                    using (var writer = new FileStream(filename, FileMode.Create))
                        Properties.Resources.embedded_image.Save(writer, System.Drawing.Imaging.ImageFormat.Png);
                    using (var attachment = mail.Attachments.Add(filename))
                    {
                        // derived from https://stackoverflow.com/a/14052552
                        attachment.PropertyAccessor.SetProperty(Attachment.PR_ATTACH_MIME_TAG, "image/png");
                        attachment.PropertyAccessor.SetProperty(Attachment.PR_ATTACH_CONTENT_ID, "myContentId");
                    }
                    mail.HtmlBody = "<p>Hello from automation!</p><p><img src=cid:myContentId width=400 height=300></p>";

                    mail.Display(true);
                }
            }
        }
Пример #2
0
        private void btnNewOutlook_Click(object sender, EventArgs e)
        {
            using (var outlook = new STC.Automation.Office.Outlook.Application())
            {
                MessageBox.Show(outlook.Version.ToString());

                using (var myNameSpace = outlook.GetNameSpace("MAPI"))
                {
                    using (var folder = myNameSpace.GetDefaultFolder(STC.Automation.Office.Outlook.Enums.DefaultFolders.Outbox))
                        folder.Display();
                }

                CreateEmail(outlook);
                //outlook.Quit();
            }
        }
Пример #3
0
 private void CreateEmail(STC.Automation.Office.Outlook.Application outlook)
 {
     using (var msg = (STC.Automation.Office.Outlook.MailItem)outlook.CreateItem(STC.Automation.Office.Outlook.Enums.ItemType.MailItem))
     {
         using (var recipient = msg.Recipients.Add("*****@*****.**"))
             recipient.Type = (long)STC.Automation.Office.Outlook.Enums.MailRecipientType.To;
         using (var recipient = msg.Recipients.Add("*****@*****.**"))
             recipient.Type = (long)STC.Automation.Office.Outlook.Enums.MailRecipientType.CC;
         msg.Subject    = "This is the subject";
         msg.Body       = "This is the body of the email.";
         msg.Importance = STC.Automation.Office.Outlook.Enums.Importance.High;
         if (File.Exists(@"C:\test.txt"))
         {
             using (var attachment = msg.Attachments.Add(@"C:\test.txt"));
         }
         msg.Recipients.ResolveAll();
         msg.Display();
         msg.Save();
         //msg.Send();
     }
 }