Outlook 連絡先アイテムを Email メッセージに添付する

このトピックでは、連絡先、カレンダー アイテム、または別の電子メール メッセージなどの Microsoft Outlook アイテムのコピーを、プログラムを使用して電子メール メッセージを送信する前にメッセージにアタッチする方法を説明します。

提供元: Ken Getz、 MCW Technologies, LLC

1 つ以上のファイルまたは Outlook アイテムをメール メッセージに添付するには、送信メールを表す MailItem オブジェクトの Attachments プロパティを使用し、添付ファイルごとに Attachments オブジェクトの Add(Object、Object、Object、Object) メソッドを呼び出します。 Add メソッドでは、ファイル名と、添付ファイルに関連付ける方法を指定できます。 このトピックのコード例に示されている連絡先アイテムなどの Outlook アイテムを添付するには、 Add メソッドのType パラメーターを Outlook.olAttachmentType.olEmbeddedItem 列挙値として指定します。

SendMailItemこのトピックの後のコード例のサンプル プロシージャでは、次の内容を受け入れます。

  • Outlook Application オブジェクトへの参照。

  • メッセージの件名と本文を含む文字列。

  • メッセージの受信者の SMTP アドレスを含む文字列のリスト。

  • 送信者の SMTP アドレスを含む文字列。

新しいメール アイテムを作成した後、コードはすべての受信者アドレスをループし、それぞれをメッセージの Recipients コレクションに追加します。 Recipients オブジェクトの ResolveAll() メソッドを呼び出すと、メール アイテムの Subject プロパティと Body プロパティが設定されます。 次に、新しい Outlook ContactItem オブジェクトを作成し、この新しい連絡先アイテムを添付ファイルとしてメール メッセージに追加します。この新しい連絡先アイテムは、Add メソッド呼び出しのパラメーターとして Outlook.olAttachmentType.olEmbeddedItem 値を指定します。 実際に電子メールを送信する前に、電子メール メッセージの送信元となるアカウントを指定する必要があります。 この情報を見つける方法の 1 つに、送信者の SMTP アドレスを使用する方法があります。 関数は GetAccountForEmailAddress 、送信者の SMTP メール アドレスを含む文字列を受け取り、対応する Account オブジェクトの参照を返します。 このメソッドは、送信者の SMTP アドレスと、セッションのプロファイルに対して定義されている構成済みの電子メール アカウントごとに SmtpAddress を比較します。 application.Session.Accounts は、現在のプロファイルの Accounts コレクションを返します。Exchange、IMAP、POP3 アカウントを含むすべてのアカウントの追跡情報は、それぞれ異なる配信ストアに関連付けることができます。 送信者の SMTP アドレスに一致する SmtpAddress プロパティが関連付けられている Account オブジェクトは、電子メール メッセージを送信するのに使用するアカウントです。 適切なアカウントを識別した後、メール アイテムの SendUsingAccount プロパティを その Account オブジェクトに設定し、 Send() メソッドを呼び出して、コードが完了します。 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 プライマリ相互運用機能アセンブリ リファレンスへようこそ」を参照 してください。 次のコードは、プログラムを使用して連絡先アイテムのコピーをメール メッセージにアタッチする方法を示しています。 この機能を示すために、Visual Studio で、 という名前 EmbedOutlookItemAddInの新しいマネージド Outlook アドインを作成し、ThisAddIn.vb または ThisAddIn.cs ファイルの内容を、ここに示すコード例に置き換えます。 手順を ThisAddIn_Startup 変更し、電子メール アドレスを適切に更新します。 SendMailWithAttachments プロシージャへの呼び出しに含まれる SMTP アドレスは、以前 Outlook で構成した送信電子メール アカウントの 1 つの SMTP アドレスに対応していなければなりません。

using System;
using System.Collections.Generic;
using Outlook = Microsoft.Office.Interop.Outlook;
 
namespace EmbedOutlookItemAddIn
{
    public partial class ThisAddIn
    {
        private void ThisAddIn_Startup(object sender, System.EventArgs e)
        {
            List<string> recipients = new List<string>();
            recipients.Add("john@contoso.com");
            recipients.Add("john@example.com");
 
            // Replace the SMTP address for sending.
            SendMailItem(Application, "Outlook started", "Outlook started at " + 
                DateTime.Now, recipients, "john@contoso.com");
        }
 
        private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
        {
        }
 
        public void SendMailItem(Outlook.Application application, 
            string subject, string body, 
            List<string> recipients, string smtpAddress)
        {
 
            Outlook.MailItem newMail = 
                application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
 
            // Set up all the recipients.
            foreach (var recipient in recipients)
            {
                newMail.Recipients.Add(recipient);
            }
            if (newMail.Recipients.ResolveAll())
            {
                // Set the details.
                newMail.Subject = subject;
                newMail.Body = body;
 
                Outlook.ContactItem contact = (Outlook.ContactItem)(application.CreateItem
                    Outlook.OlItemType.olContactItem));
 
                // Create a new contact. Use an existing contact instead, 
                // if you have one to work with.
                contact.FullName = "Kim Abercrombie";
                contact.LastName = "Kim";
                contact.FirstName = "Abercrombie";
                contact.HomeTelephoneNumber = "555-555-1212";
                contact.Save();
 
                newMail.Attachments.Add(contact, Outlook.OlAttachmentType.olEmbeddeditem);
                newMail.SendUsingAccount = GetAccountForEmailAddress(application, smtpAddress);
                newMail.Send();
             }
          }
 
        public Outlook.Account GetAccountForEmailAddress(Outlook.Application application, 
            string smtpAddress)
        {
            // Loop over the Accounts collection of the current Outlook session.
            Outlook.Accounts accounts = application.Session.Accounts;
            foreach (Outlook.Account account in accounts)
            {
                // When the email address matches, return the account.
                if (account.SmtpAddress == smtpAddress)
                {
                    return account;
                }
             }
             // If you get here, no matching account was found.
             throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!",
                 smtpAddress));
        }
    }
 
    #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
        Dim recipients As New List(Of String)
        recipients.Add("john@contoso.com")
        recipients.Add("john@example.com")
     
        ' Replace the SMTP address for sending.
        SendMailItem(Application, "Outlook started",
            "Outlook started at " & DateTime.Now, recipients,
            "john@contoso.com")
    End Sub
 
    Private Sub ThisAddIn_Shutdown() Handles Me.Shutdown
 
    End Sub
 
    Private Sub SendMailItem(ByVal application As Outlook.Application, _
        ByVal subject As String, ByVal body As String, ByVal recipients As List(Of String), _
        ByVal smtpAddress As String)
 
        Dim newMail As Outlook.MailItem = _
            DirectCast(application.CreateItem(Outlook.OlItemType.olMailItem), _
            Outlook.MailItem)
 
        ' Set up all the recipients.
        For Each recipient In recipients
            newMail.Recipients.Add(recipient)
        Next
        If newMail.Recipients.ResolveAll() Then
          ' Set the details.
          newMail.Subject = subject
          newMail.Body = body
 
          Dim contact As Outlook.ContactItem =_
             DirectCast(application.CreateItem(
             Outlook.OlItemType.olContactItem), Outlook.ContactItem)
 
          ' Create a new contact. Use an existing contact instead, 
          ' if you have one to work with.
          contact.FullName = "Kim Abercrombie"
          contact.LastName = "Kim"
          contact.FirstName = "Abercrombie"
          contact.HomeTelephoneNumber = "555-555-1212"
          contact.Save()
 
          newMail.Attachments.Add(contact, Outlook.OlAttachmentType.olEmbeddeditem)
          newMail.SendUsingAccount = GetAccountForEmailAddress(application, smtpAddress)
          newMail.Send()
        End If
    End Sub
 
    Private Function GetAccountForEmailAddress(ByVal application As Outlook.Application,_
        ByVal smtpAddress As String) As Outlook.Account
        ' Loop over the Accounts collection of the current Outlook session.
        Dim accounts As Outlook.Accounts = application.Session.Accounts
        For Each account In accounts
            ' When the email address matches, return the account.
            If account.SmtpAddress = smtpAddress Then
                Return account
            End If
        Next
        ' If you get here, no matching account was found.
        Throw New System.Exception(_
            String.Format("No Account with SmtpAddress: {0} exists!", smtpAddress))
    End Function
End Class

関連項目

メール アイテムにファイルを添付する Outlook Email メッセージへの添付ファイルのサイズを制限する Outlook Email メッセージの添付ファイルを変更する

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

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