C#Outlook加载项回复/转发按钮,图像为签名

时间:2014-08-10 13:39:30

标签: c# outlook add-in outlook-addin

我已经写了一个outlook插件(2010/2007),它获取当前所选(已打开)的mailitem并根据当前mailitem的主题,正文和附件创建一个新的邮件项目。 我的问题是,如果签名是图像附件,我不能将它放在当前mailitem的原始位置。 签名的图像放在邮件窗口的顶部(附件字段)。 我已经尝试弄乱附着位置,试图把它放在身体的尽头,但没有运气,而且我认为这不是正确的做法,因为我无法做到例如,确定每个图像放置在具有多重重放的电子邮件线程中的位置。 我试图去的地方,是制作一个像回复或转发按钮的按钮,但没有身体中的额外文字(例如:邮件被发送的时间,发件人等......)。

这是我的代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using ForwardAsNewMail;
using Outlook = Microsoft.Office.Interop.Outlook;

namespace ForwardAsNewMail
{
    public partial class ForwardAsNewMail
    {


        private void ForwardAsNewMail_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void btnNewMail_Click(object sender, RibbonControlEventArgs e)
        {
            Outlook.MailItem oldMailItem = GetMailItem(e);
            Outlook.Application outlookApp = new Outlook.Application();
            Outlook.MailItem newMailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
            newMailItem.HTMLBody = oldMailItem.HTMLBody;
            newMailItem.Subject = oldMailItem.Subject;

            #region Attahment
            // Get the count of Attachments

            int attachCount = oldMailItem.Attachments.Count;
            if (attachCount > 0)
            {
                // loop through each attachment 

                for (int idx = 1; idx <= attachCount; idx++)
                {
                    string sysPath = System.IO.Path.GetTempPath();
                    if (!System.IO.Directory.Exists(sysPath + "~test"))
                    {
                        System.IO.Directory.CreateDirectory(sysPath + "~test");
                    }
                    // get attached file and save in temp folder

                    string strSourceFileName = sysPath + "~test\\" +
                                   oldMailItem.Attachments[idx].FileName;
                    oldMailItem.Attachments[idx].SaveAsFile(strSourceFileName);
                    string strDisplayName = "Attachment";
                    int intPosition = 1;
                    int intAttachType = (int)Outlook.OlAttachmentType.olEmbeddeditem;
                    // Add the current attachment
                    newMailItem.Attachments.Add(strSourceFileName,
                                   intAttachType, intPosition, strDisplayName);
                }
            }
            #endregion

            newMailItem.Display();

        }

        ///
        /// Gets the mail item selected in the explorer view if one is selected or instance if that is the view active.
        ///
        /// The instance containing the event data.
        /// A Outlook.MailItem for the mail being viewed.
        private Microsoft.Office.Interop.Outlook.MailItem GetMailItem(RibbonControlEventArgs e)
        {
            // Check to see if a item is select in explorer or we are in inspector.
            if (e.Control.Context is Microsoft.Office.Interop.Outlook.Inspector)
            {
                Microsoft.Office.Interop.Outlook.Inspector inspector = (Microsoft.Office.Interop.Outlook.Inspector)e.Control.Context;

                if (inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem)
                {
                    return inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
                }
            }

            if (e.Control.Context is Microsoft.Office.Interop.Outlook.Explorer)
            {
                Microsoft.Office.Interop.Outlook.Explorer explorer = (Microsoft.Office.Interop.Outlook.Explorer)e.Control.Context;

                Microsoft.Office.Interop.Outlook.Selection selectedItems = explorer.Selection;
                if (selectedItems.Count != 1)
                {
                    return null;
                }

                if (selectedItems[1] is Microsoft.Office.Interop.Outlook.MailItem)
                {
                    return selectedItems[1] as Microsoft.Office.Interop.Outlook.MailItem;
                }
            }

            return null;
        }




    }
}
提前谢谢! :)

**使用解决方案进行编辑** 好吧,在德米特里向我指出正确的方向后,我设法找到了如何保存所有附件并嵌入那些嵌入的附件。 我还添加了一个IF语句,用于检查附件是否已嵌入。 所以这里是代码,如果有人感兴趣:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Office.Tools.Ribbon;
using ForwardAsNewMail;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Reflection;

namespace ForwardAsNewMail
{
    public partial class ForwardAsNewMail
    {

        private void ForwardAsNewMail_Load(object sender, RibbonUIEventArgs e)
        {

        }

        private void btnNewMail_Click(object sender, RibbonControlEventArgs e)
        {
            Outlook.MailItem oldMailItem = GetMailItem(e);
            Outlook.Application outlookApp = new Outlook.Application();
            Outlook.MailItem newMailItem = (Outlook.MailItem)outlookApp.CreateItem(Outlook.OlItemType.olMailItem);
            newMailItem.HTMLBody = oldMailItem.HTMLBody;
            newMailItem.Subject = oldMailItem.Subject;

            #region Attahment
            // Get the count of Attachments

            int attachCount = oldMailItem.Attachments.Count;
            if (attachCount > 0)
            {
                // loop through each attachment 

                for (int idx = 1; idx <= attachCount; idx++)
                {
                    string sysPath = System.IO.Path.GetTempPath();
                    if (!System.IO.Directory.Exists(sysPath + "~test"))
                    {
                        System.IO.Directory.CreateDirectory(sysPath + "~test");
                    }
                    // determain if the attachment is embeded or not (in-line or not)
                    string atchPropValue = oldMailItem.Attachments[idx].PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x3712001E");
                    if (atchPropValue == null || atchPropValue == string.Empty)
                    {
                        // get attached file and save in temp folder

                        string strSourceFileName = sysPath + "~test\\" + oldMailItem.Attachments[idx].FileName;

                        oldMailItem.Attachments[idx].SaveAsFile(strSourceFileName); // x

                        string strDisplayName = "Attachment";
                        int intPosition = oldMailItem.Attachments[idx].Index;
                        int intAttachType = (int)Outlook.OlAttachmentType.olEmbeddeditem;
                        // Add the current attachment
                        newMailItem.Attachments.Add(strSourceFileName, intAttachType, intPosition, strDisplayName);

                    }

                    else
                    {
                        Outlook.Attachment oldAttach = oldMailItem.Attachments[idx];
                        string strSourceFileName = sysPath + "~test\\" + oldAttach.FileName;
                        oldAttach.SaveAsFile(strSourceFileName);
                        // Add the current attachment
                        Outlook.Attachment newAttach = newMailItem.Attachments.Add(strSourceFileName,Missing.Value, Missing.Value, Missing.Value); // z
                        try
                        {
                            newAttach.PropertyAccessor.SetProperty(
                                @"http://schemas.microsoft.com/mapi/proptag/0x3712001F",
                                oldAttach.PropertyAccessor.GetProperty(
                                    @"http://schemas.microsoft.com/mapi/proptag/0x3712001F"));
                        }
                        catch
                        {

                        }
                    }  
                }
            }
            #endregion

            newMailItem.Display();

        }

        ///
        /// Gets the mail item selected in the explorer view if one is selected or instance if that is the view active.
        ///
        /// The instance containing the event data.
        /// A Outlook.MailItem for the mail being viewed.
        private Microsoft.Office.Interop.Outlook.MailItem GetMailItem(RibbonControlEventArgs e)
        {
            // Check to see if a item is select in explorer or we are in inspector.
            if (e.Control.Context is Microsoft.Office.Interop.Outlook.Inspector)
            {
                Microsoft.Office.Interop.Outlook.Inspector inspector = (Microsoft.Office.Interop.Outlook.Inspector)e.Control.Context;

                if (inspector.CurrentItem is Microsoft.Office.Interop.Outlook.MailItem)
                {
                    return inspector.CurrentItem as Microsoft.Office.Interop.Outlook.MailItem;
                }
            }

            if (e.Control.Context is Microsoft.Office.Interop.Outlook.Explorer)
            {
                Microsoft.Office.Interop.Outlook.Explorer explorer = (Microsoft.Office.Interop.Outlook.Explorer)e.Control.Context;

                Microsoft.Office.Interop.Outlook.Selection selectedItems = explorer.Selection;
                if (selectedItems.Count != 1)
                {
                    return null;
                }

                if (selectedItems[1] is Microsoft.Office.Interop.Outlook.MailItem)
                {
                    return selectedItems[1] as Microsoft.Office.Interop.Outlook.MailItem;
                }
            }

            return null;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

HTML body通过src / cid属性引用图像(e.g. <img src="cid:xyz">) - 添加附件后,需要确保使用Attachment正确设置PR_ATACH_CONTENT_ID属性(DASL名称http://schemas.microsoft.com/mapi/proptag/0x3712001F) .PropertyAccessor.SetProperty。使用带有OutlookSpy的嵌入式HTML图像查看现有邮件(单击IMessage按钮)。

您可能希望在访问附件时删除多点符号。

                Attachment oldAttach = oldMailItem.Attachments[idx]
                string strSourceFileName = sysPath + "~test\\" + oldAttach.FileName;
                oldAttach.SaveAsFile(strSourceFileName);
                // Add the current attachment
                Attachment newAttach = newMailItem.Attachments.Add(strSourceFileName,
                               Missing.Value, Missing.Value, Missing.Value);
                try
                {
                   newAttach.PropertyAccessor.SetProperty(
                       @"http://schemas.microsoft.com/mapi/proptag/0x3712001F",
                       oldAttach.PropertyAccessor.GetProperty(
                          @"http://schemas.microsoft.com/mapi/proptag/0x3712001F")
                }
                catch()
                {
                    //exception is raised if the property does not exist
                }