How to use the CDOSYS Library to send an e-mail message by using the SMTP port in Visual Basic .NET

Article translations Article translations
Article ID: 313775 - View products that this article applies to.
This article was previously published under Q313775
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 Windows 2000 (CDOSYS) Library to send an e-mail message by using the Simple Mail Transfer Protocol (SMTP) port in Microsoft Visual Basic .NET.

MORE INFORMATION

To send an e-mail message by using the SMTP port, 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. Add a reference to the Microsoft CDO For Windows 2000 Library. To do so, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab, locate Microsoft CDO For Windows 2000 Library, 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. Repeat step 4 to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.
  6. In the code window, replace the code with the following:
    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
            Dim oMsg As CDO.Message = New CDO.Message()
            Dim iConfg As CDO.Configuration
            Dim oFields As ADODB.Fields
            Dim oField As ADODB.Field
     
            iConfg = oMsg.Configuration
            oFields = iConfg.Fields
    
            oField = oFields("http://schemas.microsoft.com/cdo/configuration/sendusing")
            oField.Value = CDO.CdoSendUsing.cdoSendUsingPort
    
           ' TODO: Replace with the port that you want to use . It must be the same
           ' port that the SMTP server is using.
           oField = oFields("http://schemas.microsoft.com/cdo/configuration/smtpserverport")
            oField.Value = 25
    
            ' TODO: Replace with your SMTP server.
            oField = oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver")
            oField.Value = "<SMTP Server>"
    
            oFields.Update()
            oMsg.Configuration = iConfg
    
            oMsg.TextBody = "Test message body."
            oMsg.Subject = "Test SMTP Message Send using port"
            oMsg.From = "from@example.com"
            oMsg.To = "to@example.com"
    
            oMsg.Send()
    
            oMsg = Nothing
            iConfg = Nothing
            oFields = Nothing
            oField = Nothing
        End Sub
    
    End Module
    					
  7. Search for TODO in the code, and then modify the code for your environment.
  8. Press F5 to build and to run the program.
  9. Make sure that the e-mail message was sent and was received.

Properties

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

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