在给定帐户的 SMTP 地址的情况下发送电子邮件 (Outlook)

本主题介绍了如何创建电子邮件并通过 Microsoft Outlook 帐户发送它,前提是此帐户的简单邮件传输协议 (SMTP) 地址给定。

| MVP 徽标

|Helmut Obertanner 提供了以下代码示例。 Helmut 是 Microsoft 最有价值专业人员 ,在 Microsoft Visual Studio 和 Microsoft Office Outlook.|

下面的托管代码示例是使用 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 主互操作程序集参考 。 以下代码示例包含 SendEmailFromAccount 类的 SampleGetAccountForEmailAddress 方法,作为 Outlook 外接程序项目的一部分实现。 每个项目添加到 Outlook PIA,基于 Microsoft.Office.Interop.Outlook命名空间的引用。 方法 SendEmailFromAccount 接受作为输入参数,参数为受信任的 Application 对象,以及表示主题、正文、以分号分隔的收件人列表和电子邮件帐户的 SMTP 地址的字符串。 SendEmailFromAccount 创建 MailItem 对象,并使用给定参数初始化 ToSubjectBody 属性。 若要查找要从中发送电子邮件的 Account 对象, SendEmailFromAccount 请调用 GetAccountForEmailAddress 方法,该方法将给定的 SMTP 地址与当前配置文件的每个帐户的 SmtpAddress 属性匹配。 匹配的 Account 对象返回到 SendEmailFromAccount,然后用此 Account 对象初始化 MailItemSendUsingAccount 属性,并发送 MailItem。 以下是 C# 代码示例。

using System; 
using System.Text; 
using Outlook = Microsoft.Office.Interop.Outlook; 
 
namespace OutlookAddIn1 
{ 
    class Sample 
    { 
        public static void SendEmailFromAccount(Outlook.Application application, string subject, string body, string to, string smtpAddress) 
        { 
 
            // Create a new MailItem and set the To, Subject, and Body properties. 
            Outlook.MailItem newMail = (Outlook.MailItem)application.CreateItem(Outlook.OlItemType.olMailItem); 
            newMail.To = to; 
            newMail.Subject = subject; 
            newMail.Body = body; 
 
            // Retrieve the account that has the specific SMTP address. 
            Outlook.Account account = GetAccountForEmailAddress(application, smtpAddress); 
            // Use this account to send the email. 
            newMail.SendUsingAccount = account; 
            newMail.Send(); 
        } 
 
 
        public static 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; 
                } 
            } 
            throw new System.Exception(string.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)); 
        } 
 
    } 
}

以下是 Visual Basic 代码示例。

Imports Outlook = Microsoft.Office.Interop.Outlook 
 
Namespace OutlookAddIn2 
    Class Sample 
         
        Shared Sub SendEmailFromAccount(ByVal application As Outlook.Application, _ 
            ByVal subject As String, ByVal body As String, ByVal recipients As String, ByVal smtpAddress As String) 
 
            ' Create a new MailItem and set the To, Subject and Body properties. 
            Dim newMail As Outlook.MailItem = DirectCast(application.CreateItem(Outlook.OlItemType.olMailItem), Outlook.MailItem) 
            newMail.To = recipients 
            newMail.Subject = subject 
            newMail.Body = body 
 
            ' Retrieve the account that has the specific SMTP address. 
            Dim account As Outlook.Account = GetAccountForEmailAddress(application, smtpAddress) 
            ' Use this account to send the email. 
            newMail.SendUsingAccount = account 
            newMail.Send() 
        End Sub 
 
        Shared 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 
            Dim account As Outlook.Account 
            For Each account In accounts 
                ' When the email address matches, return the account. 
                If account.SmtpAddress = smtpAddress Then 
                    Return account 
                End If 
            Next 
            Throw New System.Exception(String.Format("No Account with SmtpAddress: {0} exists!", smtpAddress)) 
        End Function 
 
    End Class 
End Namespace

支持和反馈

有关于 Office VBA 或本文档的疑问或反馈? 请参阅 Office VBA 支持和反馈,获取有关如何接收支持和提供反馈的指南。