How to send mail with configuration by using Collaboration Data Objects for Windows Library in Visual Basic .NET (313776)



The information in this article applies to:

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

This article was previously published under Q313776
For a Microsoft Visual Basic 6.0 version of this article, see 161833.

IN THIS TASK

SUMMARY

This step-by-step article describes how to use Microsoft Collaboration Data Objects (CDO) for Windows Library to send an e-mail message in Visual Basic .NET.

back to the top

Create a sample to send an e-mail message with configuration

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the Microsoft CDO for Windows 2000 Library and the Microsoft ActiveX Data Objects 2.5 Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. Click Microsoft CDO for Windows 2000 Library, and then click Select.
    4. Click Microsoft ActiveX Data Objects 2.5 Library, and then click Select.
    5. Click OK. If you are prompted to generate wrappers for the libraries that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:
    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 = 2   ' CDO.CdoConfiguration.cdoSMTPServer 
    
            oField = oFields("http://schemas.microsoft.com/cdo/configuration/smtpserver")
            oField.Value = "smarthost"  ' TODO:
    
            oFields.Update()
            oMsg.Configuration = iConfg
    
    
            oMsg.TextBody = "Hello, how are you doing?"
            oMsg.Subject = "Test SMTP"
            oMsg.From = "from@microsoft.com"   ' TODO:
            oMsg.To = "to@microsoft.com"       ' TODO:
    
            oMsg.Send()
    
            oMsg = Nothing
            iConfg = Nothing
            oFields = Nothing
            oField = Nothing
        End Sub
    
    End Module
    					
  6. Modify the code where you see the TODO comments.
  7. Press F5 to build and to run the application.
  8. Verify that the e-mail message is sent and received.
back to the top

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site: back to the top

Modification Type:MajorLast Reviewed:9/8/2005
Keywords:kbHOWTOmaster KB313776 kbAudDeveloper