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



The information in this article applies to:

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

This article was previously published under Q313775

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.

Modification Type:MajorLast Reviewed:6/25/2005
Keywords:kbhowto kbcode kbXML kbMsg KB313775 kbAudDeveloper