It is usually not necessary to explicitly send commands to an SMTP server, since calling high-level methods causes the necessary commands to be sent. For example, calling Send to send a message causes the "EHLO", "MAIL FROM", "RCPT TO", and "DATA" commands to automatically be sent. However, there may be times when you require a command be sent to an SMTP server that is not possible to send by using a high-level method. In such a case, commands can be sent using the Connection property, which exposes the Tcp component used for the connection.
To send a command to a SMTP server, use the following steps. For the purposes of this example, the "HELP" command (which causes the SMTP server to return help information about a command) is used.
- Add the Smtp component to a new form.
- Import the namespace of the component you would like to use using "Using" (C#) or "Imports" (VB.NET). Place the following code at the top of your file.
C#
Copy Codeusing Dart.PowerTCP.Mail;Visual Basic
Copy CodeImports Dart.PowerTCP.Mail - Add a button to the form.
- Login to the server using the Smtp.Connection.Connect method. Send the greeting using Smtp.Connection.Send. Place the following code in the Click event of the button added to the form in step 3.
C#
Copy Code// Connect to the server smtp1.Connection.Connect("mail.test.com", 25); // Receive the response. (Don't have to do anything with it). smtp1.Connection.Receive(); // Send EHLO command. smtp1.Connection.Send("EHLO myserver\r\n"); // Recieve the response. (Don't have to do anything with it). smtp1.Connection.Receive();
Visual Basic
Copy Code' Connect to the server Smtp1.Connection.Connect("mail.test.com", 25) ' Receive the response. (Don't have to do anything with it). Smtp1.Connection.Receive() ' Send EHLO command. Smtp1.Connection.Send("EHLO myserver" + vbCrLF) 'Receive the response. (Don't have to do anything with it). Smtp1.Connection.Receive()
- Send the "HELP" command. For the purposes of this example help will be obtained for the "EHLO" command. Place this code directly after the code from step 5.
C#
Copy Code// Get HELP on a command. smtp1.Connection.Send("HELP EHLO\r\n"); // Receive the response into a string. string s = smtp1.Connection.Receive().ToString(); // Display the response. Debug.WriteLine(s);
Visual Basic
Copy Code' Get HELP on a command. Smtp1.Connection.Send("HELP EHLO" + vbCRLF) ' Receive the response into a string. Dim s As String = Smtp1.Connection.Receive().ToString() ' Display the response. Debug.WriteLine(s)
- Send the "QUIT" command. The Close method could be used but this method just abruptly ends the connection. Sending the "QUIT" command is a better way to end the connection.
C#
Copy Code// Send QUIT command. smtp1.Connection.Send("QUIT\r\n"); // Receive the response. smtp1.Connection.Receive();
Visual Basic
Copy Code' Send QUIT command. Smtp1.Connection.Send("QUIT" + vbCrLF) ' Receive the response. Smtp1.Connection.Receive()
- Compile and run the application.