Senden einer E-Mail unter Angabe der SMTP-Adresse eines Kontos (Outlook)

In diesem Thema wird erklärt, wie Sie eine E-Mail erstellen und mit einem Microsoft Outlook-Konto bei vorgegebener SMTP-Adresse (Simple Mail Transfer Protocol) dieses Kontos senden.

| MVP-Logo

|Helmut Obertanner hat die folgenden Codebeispiele zur Verfügung gestellt. Helmut ist ein Microsoft Most Valuable Professional mit Erfahrung in Microsoft Office-Entwicklungstools in Microsoft Visual Studio und Microsoft Office Outlook.|

Die folgenden Beispiele für verwalteten Code sind in C# und Visual Basic geschrieben. Um ein verwaltetes Codebeispiel von .NET Framework auszuführen, das ein Component Object Model (COM) aufrufen muss, müssen Sie eine Interopassembly verwenden, die verwaltete Schnittstellen definiert und den COM-Objekten in der Object Model-Typbibliothek zuordnet. Für Outlook können Sie Visual Studio und die Outlook Primary Interop Assembly (PIA) verwenden. Stellen Sie sicher, dass Sie die Outlook 2013 PIA installiert und eine Referenz zur Microsoft Outlook 15.0-Objektbibliothekkomponente in Visual Studio hinzugefügt haben, bevor Sie verwaltete Codebeispiele für Outlook 2013 ausführen. Sie sollten die folgenden Codebeispiele in der ThisAddIn Klasse eines Outlook-Add-Ins (mit Office Developer Tools für Visual Studio) verwenden. Das Objekt der Anwendung im Code muss ein vertrauenswürdiges Outlook- Anwendungsobjekt sein, das von ThisAddIn.Globals bereitgestellt wird. Weitere Informationen zur Verwendung der Outlook-PIA zur Entwicklung verwalteter Outlook-Lösungen finden Sie auf MSDN unter Willkommen bei der Referenz zur primären Interopassembly von Outlook (PIA). Die folgenden Codebeispiele enthalten die SendEmailFromAccount Methoden und GetAccountForEmailAddress der -Klasse, die Sample als Teil eines Outlook-Add-In-Projekts implementiert werden. Jedes Projekt fügt einen Verweis auf die Outlook-PIA hinzu, die auf dem Microsoft.Office.Interop.Outlook-Namespace basiert. Die SendEmailFromAccount Methode akzeptiert als Eingabeargumente ein vertrauenswürdiges Application-Objekt und Zeichenfolgen, die den Betreff, den Textkörper, eine durch Semikolons getrennte Liste von Empfängern und die SMTP-Adresse eines E-Mail-Kontos darstellen. SendEmailFromAccount erstellt ein MailItem-Objekt und initialisiert die Eigenschaften To, Subject und Body mit den angegebenen Argumenten. Um das Account-Objekt zu finden, von dem aus die E-Mail gesendet werden soll, SendEmailFromAccount ruft die -Methode auf GetAccountForEmailAddress , die der angegebenen SMTP-Adresse mit der SmtpAddress-Eigenschaft jedes Kontos für das aktuelle Profil entspricht. Das entsprechende Account-Objekt wird an SendEmailFromAccountzurückgegeben, wodurch dann die SendUsingAccount-Eigenschaft des MailItem-Objekts mit diesem Account-Objekt initialisiert und das MailItem-Objekt gesendet wird. Das folgende Codebeispiel verwendet 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)); 
        } 
 
    } 
}

Das folgende Codebeispiel verwendet 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

Support und Feedback

Haben Sie Fragen oder Feedback zu Office VBA oder zu dieser Dokumentation? Unter Office VBA-Support und Feedback finden Sie Hilfestellung zu den Möglichkeiten, wie Sie Support erhalten und Feedback abgeben können.