Microsoft KB Archive/313803

= How to send attachments in an e-mail message by using Visual Basic .NET =

Article ID: 313803

Article Last Modified on 7/20/2005

-

APPLIES TO


 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Visual Basic .NET 2002 Standard Edition

-



This article was previously published under Q313803



SUMMARY
This article describes how to use the Microsoft Outlook Object Library to send attachments in an e-mail message by using Microsoft Visual Basic .NET.



MORE INFORMATION
To send attachments in an e-mail message by using Visual Basic .NET, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. In the Visual Basic Projects types list, click Console Application.

By default, the Module1.vb file is created. If Office Outlook 2003 is installed on your development computer, add a reference to the Microsoft Outlook 11.0 Object Library. If Outlook 2002 is installed on your development computer, add a reference to the Microsoft Outlook 10.0 Object Library. To do so, follow these steps:  On the Project menu, click Add Reference. Click the COM tab, locate Microsoft Outlook 11.0 Library or Microsoft Outlook 10.0 Object Library, and then click Select. In the Add References dialog box, click OK.</li> If you are prompted to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the code window, replace the code with the following: Module Module1

Sub Main ' Create an Outlook application. Dim oApp As Outlook._Application oApp = New Outlook.Application

' Create a new MailItem. Dim oMsg As Outlook._MailItem oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem) oMsg.Subject = &quot;Send Attachment Using OOM in Visual Basic .NET&quot; oMsg.Body = &quot;Hello World&quot; & vbCr & vbCr

' TODO: Replace with a valid e-mail address. oMsg.To = &quot;user@example.com&quot;

' Add an attachment ' TODO: Replace with a valid attachment path. Dim sSource As String = &quot;C:\Temp\Hello.txt&quot; ' TODO: Replace with attachment name Dim sDisplayName As String = &quot;Hello.txt&quot;

Dim sBodyLen As String = oMsg.Body.Length Dim oAttachs As Outlook.Attachments = oMsg.Attachments Dim oAttach As Outlook.Attachment oAttach = oAttachs.Add(sSource,, sBodyLen + 1, sDisplayName)

' Send oMsg.Send

' Clean up       oApp = Nothing oMsg = Nothing oAttach = Nothing oAttachs = Nothing End Sub

End Module </li> Search for the TODO text string in the code, and then modify the code for your environment.</li> Press the F5 key to build and to run the program.</li> Make sure that the e-mail message and the attachment have been sent.</li></ol>

Additional query words: send attachments e-mail OOM

Keywords: kbhowto kbxml kbcode KB313803

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.