How to: Send an E-mail Given the SMTP Address of an Account
Outlook Developer Reference |
This topic shows how to create an e-mail and send it from a Microsoft Outlook account, given the Simple Mail Transfer Protocol (SMTP) address of that account.
Helmut Obertanner provided the following code samples. Helmut is a Microsoft Most Valuable Professional with expertise in Microsoft Visual Studio Tools for Office and Microsoft Office Outlook. Helmut maintains a professional site at http://www.outlooksharp.de/. |
The following managed code samples are written in C# and Visual Basic. To run a .NET Framework managed code sample that needs to call into a Component Object Model (COM), you must use an interop assembly that defines and maps managed interfaces to the COM objects in the object model type library. For Outlook, you can use Microsoft Visual Studio and the Microsoft Office Outlook Primary Interop Assembly (PIA). Before you run managed code samples for Outlook 2007, ensure that you have installed the Outlook 2007 PIA and have added a reference to the Microsoft Outlook 12.0 Object Library component in Visual Studio. For more information about using the Outlook PIA to develop managed Outlook solutions, see Welcome to the Outlook 2007 Primary Interop Assembly Reference.
The following code samples contain the SendEmailFromAccount
and GetAccountForEmailAddress
methods of the Sample
class, implemented as part of an Outlook add-in project. Each project adds a reference to the Outlook PIA, which is based on the Microsoft.Office.Interop.Outlook namespace. The SendEmailFromAccount
method accepts as input arguments a trusted Application object, and strings that represent the subject, body, a semicolon-delimited list of recipients, and the SMTP address of an e-mail account. SendEmailFromAccount
creates a MailItem object and initializes the To, Subject, and Body properties with the given arguments. To find the Account object from which the e-mail should be sent, SendEmailFromAccount
calls the GetAccountForEmailAddress
method, which matches the given SMTP address with the SmtpAddress property of each account for the current profile. The matching Account object is returned to SendEmailFromAccount
, which then initializes the SendUsingAccount property of the MailItem with this Account object, and sends the MailItem.
The following is the C# code sample.
|
The following is the Visual Basic code sample.
|