![](/img/trans.png)
[英]Disappearing Reply, Reply-All, and Forward event handlers in Outlook add-in?
[英]C# Outlook Add-In Reply / Forward Button with image as signiture
我編寫了一個Outlook加載項(2010/2007),該加載項獲取當前選擇的(打開的)郵件項目,並根據當前郵件項目的主題,正文和附件創建一個新郵件項目。 我的問題是,如果簽名是圖像附件,則無法將其放置在當前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;
}
}
}
提前致謝! :)
**用解決方案進行編輯**好吧,在Dmitry向我指出了正確的方向之后,我設法找到了如何保存所有附件並將其嵌入的附件。 我還添加了一個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;
}
}
}
HTML正文通過src / cid屬性(eg <img src="cid:xyz">
)引用圖像-添加附件后,需要確保PR_ATACH_CONTENT_ID屬性(DASL名稱為http://schemas.microsoft.com/mapi/proptag/0x3712001F
)已使用Attachment.PropertyAccessor.SetProperty進行了正確設置。 使用OutlookSpy (單擊IMessage按鈕)查看帶有嵌入式HTML圖像的現有消息。
在訪問附件時,您可能希望擺脫多點符號。
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
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.