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

Article translations Article translations
Article ID: 313773 - View products that this article applies to.
This article was previously published under Q313773
Caution ADO and ADO MD have not been fully tested in a Microsoft .NET Framework environment. They may cause intermittent issues, especially in service-based applications or in multithreaded applications. The techniques that are discussed in this article should only be used as a temporary measure during migration to ADO.NET. You should only use these techniques after you have conducted complete testing to make sure that there are no compatibility issues. Any issues that are caused by using ADO or ADO MD in this manner are unsupported. For more information, see the following article in the Microsoft Knowledge Base:
840667 You receive unexpected errors when using ADO and ADO MD in a .NET Framework application
Expand all | Collapse all

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.

Properties

Article ID: 313773 - Last Review: November 29, 2007 - Revision: 3.2
APPLIES TO
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
  • Microsoft Collaboration Data Objects 2.0
Keywords: 
kbmsg kbcode kbxml kbhowtomaster KB313773

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com