Outlook 電子メール メッセージの添付ファイルを変更する

このトピックでは、Microsoft Outlook の電子メール添付ファイルを元のファイルを変更することなく、プログラムを使用して変更する方法について説明します。

提供元: Ken Getz、 MCW Technologies, LLC

Outlook インターフェースを使用する場合であってもプログラムを使用する場合であっても、1 つ以上の添付ファイルを含めて電子メール メッセージを簡単に送信できます。 しかし、シナリオによっては、メールに添付ファイルを添付した後に、ファイル システムの元のファイルに変更を加えることなく、その添付ファイルを変更する必要があることがあります。 つまり、メモリ内の添付ファイルの内容にプログラムを使用してアクセスする必要が生じることがあります。

たとえば、アプリケーションにおいて、拡張子が .txt のすべての添付ファイル内のテキストを大文字に変更する必要があるとします。 マネージド Outlook アドインでは、 ItemSend イベントを簡単に処理できます。 このイベントでは、メール アイテムを更新する前に作業を実行します。 このシナリオの難しい部分は、各テキスト ファイルの内容を変更するために添付ファイルの内容を取得する点です。

このトピックのサンプル コードでは、Attachment インターフェイスの GetProperty(String) メソッドと SetProperty(String, Object) メソッドを使用して、この特定の問題を解決する方法を示します。 どちらの場合も、添付ファイルの内容を取得して設定するために、MAPI プロパティ PidTagAttachDataBinary が含まれる値を指定します。

メモPidTagAttachDataBinary プロパティの名前空間表現は ですhttps://schemas.microsoft.com/mapi/proptag/0x37010102。 名前空間によって参照されるプロパティの PropertyAccessor オブジェクトを使用する方法について詳しくは、「 名前空間でプロパティを参照する」をご覧ください。

サンプル コードは、メール アイテムの ItemSend イベントを処理します。 カスタム イベント ハンドラーで、.txt拡張機能を持つ添付ファイルの場合、コードは メソッドを ConvertAttachmentToUpperCase 呼び出します。 ConvertAttachmentToUpperCaseAttachment オブジェクトと MailItem オブジェクトを入力引数として受け取り、添付ファイルの内容で埋められたバイト配列を取得し、バイト配列を文字列に変換し、文字列を大文字に変換してから、添付ファイルの内容をバイト配列として変換した文字列に設定します。

The following managed code samples are written in C# and Visual Basic. コンポーネント オブジェクト モデル (COM) に呼び出す必要がある .NET Framework マネージ コード サンプルを実行するには、マネージ インターフェイスを定義し、オブジェクト モデル タイプ ライブラリの COM オブジェクトにマップする相互運用機能アセンブリを使用する必要があります。 Outlook の場合、Visual Studio および Outlook プライマリ相互運用機能アセンブリ (PIA) を使用できます。 Outlook 2013 用のマネージ コード サンプルを実行する前に、Outlook 2013 PIA をインストールしており、Visual Studio で Microsoft Outlook 15.0 オブジェクト ライブラリ コンポーネントへの参照を追加していることを確認してください。

Outlook アドインのクラスでは ThisAddIn 、次のコード サンプルを使用する必要があります (Office Developer Tools for Visual Studio を使用)。 コードの Application オブジェクトは で提供された、信頼済み Outlook ThisAddIn.Globals オブジェクトである必要があります。 Outlook PIA を使用してマネージド Outlook ソリューションを開発する方法の詳細については、MSDN の 「Outlook プライマリ相互運用機能アセンブリ リファレンスへようこそ」を参照 してください。

以下のコードは、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 の連絡先アイテムを電子メール メッセージに添付します。

Outlook 電子メール メッセージの添付ファイルのサイズを制限する

サポートとフィードバック

Office VBA またはこの説明書に関するご質問やフィードバックがありますか? サポートの受け方およびフィードバックをお寄せいただく方法のガイダンスについては、Office VBA のサポートおよびフィードバックを参照してください。