将 Outlook 联系人项目附加到Email邮件

本主题介绍如何在发送邮件之前以编程方式将 Microsoft Outlook 项目的副本(如联系人、日历项目或其他电子邮件)附加到电子邮件。

提供者: Ken Getz, MCW Technologies, LLC

若要将一个或多个文件或 Outlook 项目附加到邮件,可以使用代表传出邮件的 MailItem 对象的 Attachments 属性,并为每个附件调用 Attachments 对象的 Add (Object、 Object、 Object) 方法。 使用 Add 方法可以指定文件名以及要如何关联附件。 若要附加 Outlook 项目(如本主题的示例代码中显示的联系人项目),请将 Add 方法的 Type 参数指定为 Outlook.olAttachmentType.olEmbeddedItem 枚举值。

SendMailItem本主题后面的代码示例中的示例过程接受以下内容:

  • 对 Outlook Application 对象的引用。

  • 包含邮件的主题和正文的字符串。

  • 包含邮件收件人 SMTP 地址的字符串的泛型列表。

  • 一个包含发件人 SMTP 地址的字符串。

创建新邮件项后,代码将遍历所有收件人地址,并将每个地址添加到邮件的 Recipients 集合中。 代码调用 Recipients 对象的 ResolveAll () 方法后,它将设置邮件项目的 SubjectBody 属性。 接下来,代码将创建新的 Outlook ContactItem 对象,并将此新联系人项目作为附件添加到邮件中,并将 Outlook.olAttachmentType.olEmbeddedItem 值指定为 Add 方法调用的参数。 在实际发送电子邮件之前,必须指定要从中发送电子邮件的帐户。 查找此信息的一种方法是使用发件人的 SMTP 地址。 函数 GetAccountForEmailAddress 接受包含发件人 SMTP 电子邮件地址的字符串,并返回相应 Account 对象的引用。 此方法将发件人的 SMTP 地址与为会话配置文件定义的每个已配置电子邮件帐户的 SmtpAddress 进行比较。 application.Session.Accounts 返回当前配置文件的 帐户 集合,以及所有帐户(包括 Exchange、IMAP 和 POP3 帐户)的跟踪信息,其中每个帐户都可以与不同的传递存储相关联。 具有与发件人 SMTP 地址匹配的关联的 SmtpAddress 属性值的 Account 对象是用于发送电子邮件的帐户。 标识适当的帐户后,代码完成方法是将邮件项目的 SendUsingAccount 属性设置为该 Account 对象,然后调用 Send () 方法。 下面的托管代码示例是使用 C# 和 Visual Basic 编写的。 若要运行需调入组件对象模型 (COM) 的 .NET Framework 托管代码示例,您必须使用可定义托管接口并将其映射到对象模型类型库中的 COM 对象的互操作程序集。 对于 Outlook,您可以使用 Visual Studio 和 Outlook 主互操作程序集 (PIA)。 在您运行适用于 Outlook 2013 的托管代码示例之前,请确保您已安装了 Outlook 2013 PIA 并且已添加了对 Visual Studio 中的 Microsoft Outlook 15.0 对象库组件的引用。 应使用适用于 Visual Studio) 的 Office 开发人员工具在 Outlook 外接程序 (类中使用以下代码示例 ThisAddIn 。 代码中的 应用程序对象必须是由 提供的受信任 Outlook ThisAddIn.Globals对象。 有关使用 Outlook PIA 开发托管 Outlook 解决方案的详细信息,请参阅欢迎使用 MSDN 上的 Outlook 主互操作程序集参考 。 以下代码演示如何以编程方式将联系人项目的副本附加到邮件。 若要演示此功能,请在 Visual Studio 中创建名为 EmbedOutlookItemAddIn的新托管 Outlook 外接程序,并将 ThisAddIn.vb 或 ThisAddIn.cs 文件的内容替换为此处所示的示例代码。 ThisAddIn_Startup修改过程并相应地更新电子邮件地址。 调用 SendMailWithAttachments 过程中包含的 SMTP 地址必须与以前在 Outlook 中配置的某个传出电子邮件帐户的 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 支持和反馈,获取有关如何接收支持和提供反馈的指南。