To authenticate to an SMTP server, you can simply perform the following steps
- Add using directives to your code to create aliases for existing namespaces and avoid having to type the fully qualified type names. The code looks similar to the following:
using ComponentPro.Net.Mail;
Imports ComponentPro.Net.Mail
- Create a new instance of the Smtp class.
- Now you can connect to the SMTP server with Connect methods. The code looks similar to the following:
// Create a new instance of the Smtp class.
Smtp client = new Smtp();
// Connect to the server.
client.Connect("myserver");
// Or you can specify the SMTP port with
// client.Connect("myserver", 25);
' Create a new instance of the Smtp class.
Dim client As New Smtp()
' Connect to the server.
client.Connect("myserver")
' Or you can specify the SMTP port with
' client.Connect("myserver", 25);
- Use your user name and password to login with one of Authenticate methods. The code looks similar to the following:
// Login to the server.
client.Authenticate("user", "password");
' Login to the server.
client.Authenticate("user", "password")
- Do your work like creating a new mail message and send it, etc. The code looks similar to the following:
// Create a new mail message.
MailMessage msg = new MailMessage();
msg.Subject = "Test Subject";
msg.BodyText = "Content";
msg.From = "from@mydomain.com";
msg.To = "to@somedomain.com";
// And send it.
client.Send(msg);
' Create a new mail message.
Dim msg As New MailMessage()
msg.Subject = "Test Subject"
msg.BodyText = "Content"
msg.From = "from@mydomain.com"
msg.To = "to@somedomain.com"
' And send it.
client.Send(msg)
- After completing your work, call the Disconnect method to close the SMTP session.
Final example code
using ComponentPro.Net.Mail;
...
// Create a new instance of the Smtp class.
Smtp client = new Smtp();
// Connect to the server.
client.Connect("myserver");
// Or you can specify the SMTP port with
// client.Connect("myserver", 25);
// Login to the server.
client.Authenticate("user", "password");
// Create a new mail message.
MailMessage msg = new MailMessage();
msg.Subject = "Test Subject";
msg.BodyText = "Content";
msg.From = "from@mydomain.com";
msg.To = "to@somedomain.com";
// And send it.
client.Send(msg);
// Close the connection.
client.Disconnect();
Imports ComponentPro.Net.Mail
...
' Create a new instance of the Smtp class.
Dim client As New Smtp()
' Connect to the server.
client.Connect("myserver")
' Or you can specify the SMTP port with
' client.Connect("myserver", 25);
' Login to the server.
client.Authenticate("user", "password")
' Create a new mail message.
Dim msg As New MailMessage()
msg.Subject = "Test Subject"
msg.BodyText = "Content"
msg.From = "from@mydomain.com"
msg.To = "to@somedomain.com"
' And send it.
client.Send(msg)
' Close the connection.
client.Disconnect()