示例#1
0
            static bool SendEmailWithOutlook(string filename, string message)
            {
                var msgFile = Path.GetFileNameWithoutExtension(filename) + ".msg";

                Microsoft.Office.Interop.Outlook.Application outlook = new Microsoft.Office.Interop.Outlook.Application();
                MailItem mi = outlook.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olMailItem);

                mi.To   = "*****@*****.**";
                mi.Body = message;
                mi.Attachments.Add(filename);
                mi.Subject = "SvcPerf Report";
                Inspector inspector = mi.GetInspector;

                inspector.Activate();
                return(true);
            }
示例#2
0
        /// <summary>
        /// Export the file to the supplied inspector
        /// </summary>
        /// <param name="inspector">Inspector</param>
        /// <param name="currentItem">Item</param>
        /// <param name="tmpFile"></param>
        /// <param name="attachmentName"></param>
        /// <returns></returns>
        private static bool ExportToInspector(Inspector inspector, Item currentItem, string tmpFile, string attachmentName)
        {
            if (currentItem == null)
            {
                LOG.Warn("No current item.");
                return(false);
            }
            OlObjectClass itemClass     = currentItem.Class;
            bool          isMail        = OlObjectClass.olMail.Equals(itemClass);
            bool          isAppointment = OlObjectClass.olAppointment.Equals(itemClass);

            if (!isMail && !isAppointment)
            {
                LOG.Warn("Item is no mail or appointment.");
                return(false);
            }
            MailItem mailItem = null;

            try {
                if (isMail)
                {
                    //mailItem = COMWrapper.Cast<MailItem>(currentItem);
                    mailItem = (MailItem)currentItem;
                    if (mailItem.Sent)
                    {
                        LOG.WarnFormat("Item already sent, can't export to {0}", currentItem.Subject);
                        return(false);
                    }
                }

                // Make sure the inspector is activated, only this way the word editor is active!
                // This also ensures that the window is visible!
                inspector.Activate();
                bool isTextFormat = false;
                if (isMail)
                {
                    isTextFormat = OlBodyFormat.olFormatPlain.Equals(mailItem.BodyFormat);
                }
                if (isAppointment || !isTextFormat)
                {
                    // Check for wordmail, if so use the wordexporter
                    // http://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx
                    // Earlier versions of Outlook also supported an Inspector.HTMLEditor object property, but since Internet Explorer is no longer the rendering engine for HTML messages and posts, HTMLEditor is no longer supported.
                    if (inspector.IsWordMail() && inspector.WordEditor != null)
                    {
                        try {
                            if (WordExporter.InsertIntoExistingDocument(inspector.WordEditor.Application, inspector.WordEditor, tmpFile, null, null))
                            {
                                LOG.Info("Inserted into Wordmail");

                                // check the format afterwards, otherwise we lose the selection
                                //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
                                //	LOG.Info("Changing format to HTML.");
                                //	currentMail.BodyFormat = OlBodyFormat.olFormatHTML;
                                //}
                                return(true);
                            }
                        } catch (Exception exportException) {
                            LOG.Error("Error exporting to the word editor, trying to do it via another method", exportException);
                        }
                    }
                    else if (isAppointment)
                    {
                        LOG.Info("Can't export to an appointment if no word editor is used");
                        return(false);
                    }
                    else
                    {
                        LOG.Info("Trying export for outlook < 2007.");
                    }
                }
                // Only use mailitem as it should be filled!!
                LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat);

                string contentID;
                if (outlookVersion.Major >= OUTLOOK_2007)
                {
                    contentID = Guid.NewGuid().ToString();
                }
                else
                {
                    LOG.Info("Older Outlook (<2007) found, using filename as contentid.");
                    contentID = Path.GetFileName(tmpFile);
                }

                // Use this to change the format, it will probably lose the current selection.
                //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
                //	LOG.Info("Changing format to HTML.");
                //	currentMail.BodyFormat = OlBodyFormat.olFormatHTML;
                //}

                bool inlinePossible = false;
                if (OlBodyFormat.olFormatHTML.Equals(mailItem.BodyFormat))
                {
                    // if html we can try to inline it
                    // The following might cause a security popup... can't ignore it.
                    try {
                        IHTMLDocument2 document2 = inspector.HTMLEditor as IHTMLDocument2;
                        if (document2 != null)
                        {
                            IHTMLSelectionObject selection = document2.selection;
                            if (selection != null)
                            {
                                IHTMLTxtRange range = selection.createRange();
                                if (range != null)
                                {
                                    // First paste, than attach (otherwise the range is wrong!)
                                    range.pasteHTML("<BR/><IMG border=0 hspace=0 alt=\"" + attachmentName + "\" align=baseline src=\"cid:" + contentID + "\"><BR/>");
                                    inlinePossible = true;
                                }
                                else
                                {
                                    LOG.DebugFormat("No range for '{0}'", inspector.Caption);
                                }
                            }
                            else
                            {
                                LOG.DebugFormat("No selection for '{0}'", inspector.Caption);
                            }
                        }
                        else
                        {
                            LOG.DebugFormat("No HTML editor for '{0}'", inspector.Caption);
                        }
                    } catch (Exception e) {
                        // Continue with non inline image
                        LOG.Warn("Error pasting HTML, most likely due to an ACCESS_DENIED as the user clicked no.", e);
                    }
                }

                // Create the attachment (if inlined the attachment isn't visible as attachment!)
                using (Attachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName)) {
                    if (outlookVersion.Major >= OUTLOOK_2007)
                    {
                        // Add the content id to the attachment, this only works for Outlook >= 2007
                        try {
                            PropertyAccessor propertyAccessor = attachment.PropertyAccessor;
                            propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID);
                        } catch {
                        }
                    }
                }
            } catch (Exception ex) {
                LOG.WarnFormat("Problem while trying to add attachment to Item '{0}' : {1}", inspector.Caption, ex);
                return(false);
            }
            try {
                inspector.Activate();
            } catch (Exception ex) {
                LOG.Warn("Problem activating inspector: ", ex);
                return(false);
            }
            LOG.Debug("Finished!");
            return(true);
        }
        /// <summary>
        /// Export the file to the supplied inspector
        /// </summary>
        /// <param name="inspector"></param>
        /// <param name="tmpFile"></param>
        /// <param name="attachmentName"></param>
        /// <returns></returns>
        private static bool ExportToInspector(Inspector inspector, string tmpFile, string attachmentName)
        {
            Item currentItem = inspector.CurrentItem;
            if (currentItem == null) {
                LOG.Warn("No current item.");
                return false;
            }
            OlObjectClass itemClass = currentItem.Class;
            bool isMail = OlObjectClass.olMail.Equals(itemClass);
            bool isAppointment = OlObjectClass.olAppointment.Equals(itemClass);
            if (!isMail && !isAppointment) {
                LOG.Warn("Item is no mail or appointment.");
                return false;
            }
            MailItem mailItem = null;
            try {
                if (isMail) {
                    //mailItem = COMWrapper.Cast<MailItem>(currentItem);
                    mailItem = (MailItem)currentItem;
                    if (mailItem.Sent) {
                        LOG.WarnFormat("Item already sent, can't export to {0}", currentItem.Subject);
                        return false;
                    }
                }

                // Make sure the inspector is activated, only this way the word editor is active!
                // This also ensures that the window is visible!
                inspector.Activate();

                // Check for wordmail, if so use the wordexporter
                // http://msdn.microsoft.com/en-us/library/dd492012%28v=office.12%29.aspx
                // Earlier versions of Outlook also supported an Inspector.HTMLEditor object property, but since Internet Explorer is no longer the rendering engine for HTML messages and posts, HTMLEditor is no longer supported.
                if (inspector.IsWordMail() && inspector.WordEditor != null) {
                    if (WordExporter.InsertIntoExistingDocument(inspector.WordEditor, tmpFile)) {
                        LOG.Info("Inserted into Wordmail");

                        // check the format afterwards, otherwise we lose the selection
                        //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
                        //	LOG.Info("Changing format to HTML.");
                        //	currentMail.BodyFormat = OlBodyFormat.olFormatHTML;
                        //}
                        return true;
                    }
                } else if (isAppointment) {
                    LOG.Info("Can't export to an appointment if no word editor is used");
                    return false;
                } else {
                    LOG.Info("Trying export for word < 2007.");
                }
                // Only use mailitem as it should be filled!!
                LOG.InfoFormat("Item '{0}' has format: {1}", mailItem.Subject, mailItem.BodyFormat);

                string contentID;
                if (outlookVersion.Major >= 12) {
                    contentID = Guid.NewGuid().ToString();
                } else {
                    LOG.Info("Older Outlook (<2007) found, using filename as contentid.");
                    contentID = Path.GetFileName(tmpFile);
                }

                // Use this to change the format, it will probably lose the current selection.
                //if (!OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
                //	LOG.Info("Changing format to HTML.");
                //	currentMail.BodyFormat = OlBodyFormat.olFormatHTML;
                //}

                bool inlinePossible = false;
                if (OlBodyFormat.olFormatHTML.Equals(mailItem.BodyFormat)) {
                    // if html we can try to inline it
                    // The following might cause a security popup... can't ignore it.
                    try {
                        IHTMLDocument2 document2 = inspector.HTMLEditor as IHTMLDocument2;
                        if (document2 != null) {
                            IHTMLSelectionObject selection = document2.selection;
                            if (selection != null) {
                                IHTMLTxtRange range = selection.createRange();
                                if (range != null) {
                                    // First paste, than attach (otherwise the range is wrong!)
                                    range.pasteHTML("<BR/><IMG border=0 hspace=0 alt=\"" + attachmentName + "\" align=baseline src=\"cid:" + contentID + "\"><BR/>");
                                    inlinePossible = true;
                                } else {
                                    LOG.DebugFormat("No range for '{0}'", inspector.Caption);
                                }
                            } else {
                                LOG.DebugFormat("No selection for '{0}'", inspector.Caption);
                            }
                        } else {
                            LOG.DebugFormat("No HTML editor for '{0}'", inspector.Caption);
                        }
                    } catch (Exception e) {
                        // Continue with non inline image
                        LOG.Warn("Error pasting HTML, most likely due to an ACCESS_DENIED as the user clicked no.", e);
                    }
                }

                // Create the attachment (if inlined the attachment isn't visible as attachment!)
                Attachment attachment = mailItem.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible ? 0 : 1, attachmentName);
                if (outlookVersion.Major >= 12) {
                    // Add the content id to the attachment, this only works for Outlook >= 2007
                    try {
                        PropertyAccessor propertyAccessor = attachment.PropertyAccessor;
                        propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID);
                    } catch {
                    }
                }
            } catch (Exception ex) {
                LOG.WarnFormat("Problem while trying to add attachment to Item '{0}' : {1}", inspector.Caption, ex);
                return false;
            }
            LOG.Debug("Finished!");
            return true;
        }
示例#4
0
        private static bool ExportToInspector(Inspector inspector, string tmpFile, string subject)
        {
            Item currentMail = inspector.CurrentItem;

            if (currentMail == null)
            {
                LOG.Debug("No current item.");
                return(false);
            }
            if (!OlObjectClass.olMail.Equals(currentMail.Class))
            {
                LOG.Debug("Item is no mail.");
                return(false);
            }
            try {
                if (currentMail.Sent)
                {
                    LOG.Debug("Item already sent");
                    return(false);
                }

                // Make sure the inspector is activated, only this way the word editor is active!
                // This also ensures that the window is visible!
                inspector.Activate();

                // Check for wordmail, if so use the wordexporter
                if (inspector.IsWordMail() && inspector.WordEditor != null)
                {
                    if (WordExporter.InsertIntoExistingDocument(inspector.WordEditor, tmpFile))
                    {
                        LOG.Debug("Inserted into Wordmail");
                        return(true);
                    }
                }
                else
                {
                    LOG.Debug("Wordmail editor is not supported");
                }

                LOG.DebugFormat("Email '{0}' has format: {1}", currentMail.Subject, currentMail.BodyFormat);

                string contentID;
                if (outlookVersion.Major >= 12)
                {
                    contentID = Guid.NewGuid().ToString();
                }
                else
                {
                    LOG.Info("Older Outlook (<2007) found, using filename as contentid.");
                    contentID = Path.GetFileName(tmpFile);
                }

                bool inlinePossible = false;
                if (OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat))
                {
                    // if html we can try to inline it
                    // The following might cause a security popup... can't ignore it.
                    try {
                        IHTMLDocument2 document2 = inspector.HTMLEditor as IHTMLDocument2;
                        if (document2 != null)
                        {
                            IHTMLSelectionObject selection = document2.selection;
                            if (selection != null)
                            {
                                IHTMLTxtRange range = selection.createRange();
                                if (range != null)
                                {
                                    // First paste, than attach (otherwise the range is wrong!)
                                    range.pasteHTML("<BR/><IMG border=0 hspace=0 alt=\"" + subject + "\" align=baseline src=\"cid:" + contentID + "\"><BR/>");
                                    inlinePossible = true;
                                }
                                else
                                {
                                    LOG.DebugFormat("No range for '{0}'", inspector.Caption);
                                }
                            }
                            else
                            {
                                LOG.DebugFormat("No selection for '{0}'", inspector.Caption);
                            }
                        }
                        else
                        {
                            LOG.DebugFormat("No HTML editor for '{0}'", inspector.Caption);
                        }
                    } catch (Exception e) {
                        LOG.Warn("Error pasting HTML, most likely due to an ACCESS_DENIED as the user clicked no.", e);
                        // Continue with non inline image
                    }
                }

                // Create the attachment (if inlined the attachment isn't visible as attachment!)
                Attachment attachment = currentMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible?0:1, subject);
                if (outlookVersion.Major >= 12)
                {
                    // Add the content id to the attachment
                    try {
                        PropertyAccessor propertyAccessor = attachment.PropertyAccessor;
                        propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID);
                    } catch {
                    }
                }
            } catch (Exception ex) {
                LOG.DebugFormat("Problem while trying to add attachment to  MailItem '{0}' : {1}", inspector.Caption, ex);
                return(false);
            }
            LOG.Debug("Finished!");
            return(true);
        }
示例#5
0
        private static bool ExportToInspector(Inspector inspector, string tmpFile, string subject)
        {
            Item currentMail = inspector.CurrentItem;
            if (currentMail == null) {
                LOG.Debug("No current item.");
                return false;
            }
            if (!OlObjectClass.olMail.Equals(currentMail.Class)) {
                LOG.Debug("Item is no mail.");
                return false;
            }
            try {
                if (currentMail.Sent) {
                    LOG.Debug("Item already sent");
                    return false;
                }

                // Make sure the inspector is activated, only this way the word editor is active!
                // This also ensures that the window is visible!
                inspector.Activate();

                // Check for wordmail, if so use the wordexporter
                if (inspector.IsWordMail() && inspector.WordEditor != null) {
                    if (WordExporter.InsertIntoExistingDocument(inspector.WordEditor, tmpFile)) {
                        LOG.Debug("Inserted into Wordmail");
                        return true;
                    }
                } else {
                    LOG.Debug("Wordmail editor is not supported");
                }

                LOG.DebugFormat("Email '{0}' has format: {1}", currentMail.Subject, currentMail.BodyFormat);

                string contentID;
                if (outlookVersion.Major >=12 ) {
                    contentID = Guid.NewGuid().ToString();
                } else {
                    LOG.Info("Older Outlook (<2007) found, using filename as contentid.");
                    contentID = Path.GetFileName(tmpFile);
                }

                bool inlinePossible = false;
                if (OlBodyFormat.olFormatHTML.Equals(currentMail.BodyFormat)) {
                    // if html we can try to inline it
                    // The following might cause a security popup... can't ignore it.
                    try {
                        IHTMLDocument2 document2 = inspector.HTMLEditor as IHTMLDocument2;
                        if (document2 != null) {
                            IHTMLSelectionObject selection = document2.selection;
                            if (selection != null) {
                                IHTMLTxtRange range = selection.createRange();
                                if (range != null) {
                                    // First paste, than attach (otherwise the range is wrong!)
                                    range.pasteHTML("<BR/><IMG border=0 hspace=0 alt=\"" + subject + "\" align=baseline src=\"cid:"+ contentID +"\"><BR/>");
                                    inlinePossible = true;
                                } else {
                                    LOG.DebugFormat("No range for '{0}'", inspector.Caption);
                                }
                            } else {
                                LOG.DebugFormat("No selection for '{0}'", inspector.Caption);
                            }
                        } else {
                            LOG.DebugFormat("No HTML editor for '{0}'", inspector.Caption);
                        }
                    } catch (Exception e) {
                        LOG.Warn("Error pasting HTML, most likely due to an ACCESS_DENIED as the user clicked no.", e);
                        // Continue with non inline image
                    }
                }

                // Create the attachment (if inlined the attachment isn't visible as attachment!)
                Attachment attachment = currentMail.Attachments.Add(tmpFile, OlAttachmentType.olByValue, inlinePossible?0:1, subject);
                if (outlookVersion.Major >=12) {
                    // Add the content id to the attachment
                    try {
                        PropertyAccessor propertyAccessor = attachment.PropertyAccessor;
                        propertyAccessor.SetProperty(PropTag.ATTACHMENT_CONTENT_ID, contentID);
                    } catch {
                    }
                }
            } catch (Exception ex) {
                LOG.DebugFormat("Problem while trying to add attachment to  MailItem '{0}' : {1}", inspector.Caption, ex);
                return false;
            }
            LOG.Debug("Finished!");
            return true;
        }