修改 Outlook Email 訊息的附件

本主題描述如何以程式設計方式修改 Microsoft Outlook 電子郵件附件,而不需變更原始檔案。

提供者: Ken Getz、 MCW Technologies、LLC

在 Outlook 介面和以程式設計方式傳送含有一或多個附件的電子郵件訊息很簡單。 不過,在某些情況下,您可能希望能夠在附件附加至訊息項目之後修改附件,而不需要變更檔案系統中的原始檔案。 換句話說,您可能需要以程式設計方式存取記憶體中附件的內容。

例如,假設您的應用程式需要將具有.txt副檔名的所有附件中的文字轉換成大寫。 在受控 Outlook 增益集中,您可以輕鬆地處理 ItemSend 事件。 在該事件中,請先執行工作,再傳送訊息項目。 此案例的困難部分是擷取附件的內容,以修改每個文字檔的內容。

本主題中的範例程式碼示範如何使用 GetProperty (String) 和 SetProperty (String,Object) Attachment 介面的方法來解決此特定問題。 在每個案例中,您都會提供包含 MAPI 屬性 PidTagAttachDataBinary 的值,以取得 (,然後設定) 附件的內容。

注意PidTagAttachDataBinary屬性的命名空間標記法為 https://schemas.microsoft.com/mapi/proptag/0x37010102 。 如需在命名空間所參考的屬性上使用 PropertyAccessor 物件的詳細資訊, 請參閱依命名空間參照屬性

範例程式碼會處理訊息項目的 ItemSend 事件。 在自訂事件處理常式中,對於具有.txt副檔名的任何附件,程式碼會呼叫 ConvertAttachmentToUpperCase 方法。 ConvertAttachmentToUpperCase 會採用 Attachment 物件和 MailItem 物件作為輸入引數、擷取填入附件內容的位元組陣列、將位元組陣列轉換成字串、將字串轉換成大寫,然後將附件的內容設定為已轉換的字串做為位元組陣列。

The following managed code samples are written in C# and Visual Basic. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Visual Studio and the Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2013, ensure that you have installed the Outlook 2013 PIA and have added a reference to the Microsoft Outlook 15.0 Object Library component in Visual Studio.

您應該使用 Office Developer Tools for Visual Studio) ,在 Outlook 增益集 (類別中使用下列程式碼 ThisAddIn 範例。 程式碼中的Application物件必須是 所 ThisAddIn.Globals 提供的受信任 Outlook應用程式物件。 如需使用 Outlook PIA 開發受控 Outlook 解決方案的詳細資訊,請參 閱 MSDN 上的歡迎使用 Outlook 主要 Interop 元件參考

下列程式碼示範如何以程式設計方式修改 Outlook 電子郵件附件,而不需變更原始檔案。 若要示範這項功能,請在 Visual Studio 中建立名為 ModifyAttachmentAddIn 的新受控 Outlook 增益集。 以下列程式碼取代 ThisAddIn.cs 或 ThisAddIn.vb 中的程式碼。

注意 若要存取附件資料,必須使用 MailItem.Save 方法來儲存訊息項目。

using Outlook = Microsoft.Office.Interop.Outlook;
 
namespace ModifyAttachmentAddIn
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            Application.ItemSend += new Outlook.ApplicationEvents_11_ItemSendEventHandler(Application_ItemSend);
        }
 
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
 
 
        void Application_ItemSend(object Item, ref bool Cancel)
        {
            Outlook.MailItem mailItem = Item as Outlook.MailItem;
 
            if (mailItem != null)
            {
                var attachments = mailItem.Attachments;
                // If the attachment a text file, convert its text to all uppercase.
                foreach (Outlook.Attachment attachment in attachments)
                {

                    ConvertAttachmentToUpperCase(attachment, mailItem);
                }
            }
        }
 
        private void ConvertAttachmentToUpperCase(Outlook.Attachment attachment, Outlook.MailItem mailItem)
        {
            const string PR_ATTACH_DATA_BIN =
                "https://schemas.microsoft.com/mapi/proptag/0x37010102";
 
            // Confirm that the attachment is a text file.
            if (System.IO.Path.GetExtension(attachment.FileName) == ".txt")
            {
                // There are other heuristics you could use to determine whether the 
                // the attachment is a text file. For now, keep it simple: Only
                // run this code for *.txt.
 
                // Retrieve the attachment as an array of bytes.
                var attachmentData =
                    attachment.PropertyAccessor.GetProperty(
                    PR_ATTACH_DATA_BIN);
 
                // Convert the byte array into a Unicode string.
                string data = System.Text.Encoding.Unicode.GetString(attachmentData);
                // Convert to upper case.
                data = data.ToUpper();
                // Convert the data back to an array of bytes.
                attachmentData = System.Text.Encoding.Unicode.GetBytes(data);
 
                //Set PR_ATTACH_DATA_BIN to attachmentData.
                attachment.PropertyAccessor.SetProperty(PR_ATTACH_DATA_BIN,
                    attachmentData);
            }
        }
 
        #region VSTO generated code
 
        /// <summary>
        /// Required method for Designer support - don't modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InternalStartup()
        {
            this.Startup += new System.EventHandler(ThisAddIn_Startup);
            this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
        }
 
        #endregion
    }
}
Public Class ThisAddIn
 
 
    Private Sub ThisAddIn_Startup() Handles Me.Startup
 
    End Sub
 
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
 
    End Sub
 
    Private Sub Application_ItemSend(ByVal Item As Object, _
        ByRef Cancel As Boolean) Handles Application.ItemSend
 
        Dim mailItem As Outlook.MailItem = TryCast(Item, Outlook.MailItem)
 
        If mailItem IsNot Nothing Then
            Dim attachments = mailItem.Attachments
            For Each attachment As Outlook.Attachment In attachments
                ' If the attachment is a text file, convert to uppercase.
                ConvertAttachmentToUpperCase(attachment, mailItem)
            Next attachment
        End If
    End Sub
 
    Private Sub ConvertAttachmentToUpperCase(ByVal attachment As Outlook.Attachment, _
        ByVal mailItem As Outlook.MailItem)
        Const PR_ATTACH_DATA_BIN As String = "https://schemas.microsoft.com/mapi/proptag/0x37010102"
 
        ' Confirm that the attachment is a text file.
        If System.IO.Path.GetExtension(attachment.FileName) = ".txt" Then
 
            ' There are other heuristics you could use to determine whether the 
            ' the attachment is a text file. For now, keep it simple: Only
            ' run this code for *.txt.
 
            ' Retrieve the attachment as an array of bytes.
            Dim attachmentData = attachment.PropertyAccessor.GetProperty(PR_ATTACH_DATA_BIN)
 
            ' Convert the byte array into a Unicode string.
            Dim data As String = System.Text.Encoding.Unicode.GetString(attachmentData)
            ' Convert to upper case.
            data = data.ToUpper()
            ' Convert the data back to an array of bytes.
            attachmentData = System.Text.Encoding.Unicode.GetBytes(data)
 
            'Set PR_ATTACH_DATA_BIN to attachmentData.
            attachment.PropertyAccessor.SetProperty(PR_ATTACH_DATA_BIN, attachmentData)
         End If
    End Sub
 
End Class

另請參閱

將檔案附加至訊息項目

將 Outlook 連絡人項目附加至Email訊息

將附件的大小限制為 Outlook Email訊息

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應