Wednesday, December 3, 2008

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

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.


 

To send attachments in an e-mail message by using Visual Basic .NET, follow these steps:

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. In the Visual Basic Projects types list, click Console Application.
    By default, the Module1.vb file is created.
  4. 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:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate Microsoft Outlook 10.0 Library (for Office 2002) or Microsoft Outlook 11.0 Object Library (for Office 2003) or Microsoft Outlook 12.0 Object Library (for Office 2007), and then click Select.
    3. In the Add References dialog box, click OK.
    4. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  5. In the code window, replace the code with the following:


 

  1. Module Module1

  2.  
  3. Sub Main()
  4. ' Create an Outlook application.
  5. Dim oApp As Outlook._Application
  6. oApp = New Outlook.Application()

  7.  
  8. ' Create a new MailItem.
  9. Dim oMsg As Outlook._MailItem
  10. oMsg = oApp.CreateItem(Outlook.OlItemType.olMailItem)
  11. oMsg.Subject = "Send Attachment Using OOM in Visual Basic .NET"
  12. oMsg.Body = "Hello World" & vbCr & vbCr

  13.  
  14. ' TODO: Replace with a valid e-mail address.
  15. oMsg.To = "user@example.com"

  16.  
  17. ' Add an attachment
  18. ' TODO: Replace with a valid attachment path.
  19. Dim sSource As String = "C:\Temp\Hello.txt"
  20. ' TODO: Replace with attachment name
  21. Dim sDisplayName As String = "Hello.txt"

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

  27.  
  28. ' Send
  29. oMsg.Send()

  30.  
  31. ' Clean up
  32. oApp = Nothing
  33. oMsg = Nothing
  34. oAttach = Nothing
  35. oAttachs = Nothing
  36. End Sub

  37.  
  38. End Module
  1. Search for the TODO text string in the code, and then modify the code for your environment.
  2. Press the F5 key to build and to run the program.
  3. Make sure that the e-mail message and the attachment have been sent.
  4. The above example will sent a mail in silent mode that is without previewing the message to the users and also without asking the users confirmation.
  5. To preview the message and to ask the user to click the "Send" button, just replace oMsg.Send() code with oMsg.Display()

Original source : http://support.microsoft.com/?kbid=313803 (Thanks to MicroSoft)

0 comments:

Post a Comment