How to use the CDOSYS Library to send an e-mail message by using the SMTP port in Visual Basic .NET
This article was previously published under Q313775
This article has been archived. It is offered "as is" and will no longer be updated.
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
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.
To send an e-mail message by using the SMTP port, follow these steps:
- Start Microsoft Visual Studio .NET.
- On the File menu, point to New, and then click Project.
- In the Visual Basic Projects types list, click Console Application.
By default, the Module1.vb file is created.
- Add a reference to the Microsoft CDO For Windows 2000 Library. To do so, follow these steps:
- On the Project menu, click Add Reference.
- Click the COM tab, locate Microsoft CDO For Windows 2000 Library, and then click Select.
- In the Add References dialog box, click OK.
- If you are prompted to generate wrappers for the libraries that you selected, click Yes.
- Repeat step 4 to add a reference to the Microsoft ActiveX Data Objects 2.5 Library.
- In the code window, replace the code with the following:
Imports System.ReflectionModule 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 = "firstname.lastname@example.org" oMsg.To = "email@example.com" oMsg.Send() oMsg = Nothing iConfg = Nothing oFields = Nothing oField = Nothing End SubEnd Module
- Search for TODO in the code, and then modify the code for your environment.
- Press F5 to build and to run the program.
- Make sure that the e-mail message was sent and was received.
send mail CDOSYS SMTP Port
Article ID: 313775 - Last Review: 12/07/2015 08:15:43 - Revision: 3.2
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
- kbnosurvey kbarchive kbhowto kbcode kbxml kbmsg KB313775