How to send e-mail messages by using CDOSYS and the local SMTP directory with Visual Basic .NET (313773)



The information in this article applies to:

  • Microsoft Visual Basic .NET (2003)
  • Microsoft Visual Basic .NET (2002)
  • Microsoft Collaboration Data Objects for Windows 2000

This article was previously published under Q313773

SUMMARY

This article describes how to use the Collaboration Data Objects (CDO) for Microsoft Windows 2000 library and the local SMTP directory to send an HTML e-mail message by using Microsoft Visual Basic .NET. You must run this sample on a server that has an SMTP service running to send the e-mail message.

MORE INFORMATION

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Under Project Types, click Visual Basic Projects.
  4. Under Templates, click Console Application, and then click OK.

    By default, Module1.vb is created.
  5. Add a reference to the Microsoft CDO for Windows 2000 library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. On the COM tab, click Microsoft CDO For Windows 2000 Library, and then click Select.
    3. In the Add References dialog box, click OK to accept your selections. If you receive a prompt to generate wrappers for the libraries that you selected, click Yes.
  6. Follow steps 5a through 5c to add a reference to the Microsoft ActiveX Data Objects 2.5 library.
  7. In the code window, replace all the code with:
    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
            Dim oMsg As CDO.Message = New CDO.Message()
            oMsg.From = "from@microsoft.com"
            oMsg.To = "to@microsoft.com"
            oMsg.Subject = "CdoSys HTML mail in VB.NET (pickup)"
    
            Dim sHtml As String
            sHtml = "<HTML>" & _
             "<HEAD>" & _
             "<TITLE>Simple CDOSYS Message</TITLE>" & _
             "</HEAD>" & _
             "<BODY><P>" & _
             "<h1><Font Color=Green>Simple CDOSYS HTML Message</Font></h1>" & _
             "</BODY>" & _
             "</HTML>"
    
            oMsg.HTMLBody = sHtml
    
            ' Set configuration.
            Dim iConfg As CDO.Configuration = New CDO.Configuration()
    
            Dim oFields As ADODB.Fields
            oFields = iConfg.Fields
    
            Dim oField As ADODB.Field
            oField = oFields("http://schemas.microsoft.com/cdo/configuration/sendusing")
            oField.Value = 1   ' Sets the configuration to use the local SMTP server.
    
            oFields.Update()
            oMsg.Configuration = iConfg
    
            oMsg.Send()
    
            oMsg = Nothing
            iConfg = Nothing
            oFields = Nothing
            oField = Nothing
        End Sub
    
    End Module
    					
  8. Search for "TODO" in the code, and then modify the code for your environment.
  9. Press F5 to build and to run the program.
  10. Verify that the e-mail message has been sent and received.

Modification Type:MajorLast Reviewed:5/20/2005
Keywords:kbMsg kbcode kbXML kbHOWTOmaster KB313773 kbAudDeveloper