The Smtp Client

Send Email Walkthrough: for C# and VB NET Students

 

In the last lesson, you designed the user form to send emails. In this lesson, you'll set up use the SmtpClient class so that you contact your email server.

Double click your BtnSend button to create the code stub.

For C# users, add the following import statements to the top of your code:

using System.Net;
using System.Net.Mail;
using System.Net.Sockets;
using System.IO;

They will all be greyed out, as we haven't used them yet. You can also get rid of the following, as you won't need them:

using System.Data;
using System.Drawing;
using System.Linq;

For VB Net users, add the following to the top of your code, above the Class line:

Imports System.Net
Imports System.Net.Mail
Imports System.Net.Sockets
Imports System.IO
Imports System.Text

The point about adding these import lines is so that we don't have to write long lines of code like System.Net.Mail.SmtpClient. Because the namespaces have been added to the top, you can just write SmtpClient instead.

The first thing to do is to get all the text from the textboxes. So double click your button and set up the following variables in your code:

VB Net

Dim emailTo As String = txtEmailTo.Text.Trim()
Dim emailFrom As String = txtEmailFrom.Text.Trim()
Dim emailSubject As String = txtEmailSubject.Text.Trim()
Dim emailBody As String = txtEmailBody.Text.Trim()

C#

string emailTo = txtEmailTo.Text.Trim();
string emailFrom = txtEmailFrom.Text.Trim();
string emailSubject = txtEmailSubject.Text.Trim();
string emailBody = txtEmailBody.Text.Trim();

You can add error checking, if you like (and you should). We'll leave it out, though, for convenience's sake.

To send emails, there is a class called SmtpClient. This is in the Net.Mail namespace. So, in VB Net, add the following to your code

Dim smtpServer As New SmtpClient

For C#, add this:

SmtpClient smtpServer = new SmtpClient();

This sets up an object of type SmtpClient that we've called smtpServer.

The SmtpClient needs a few things from you: a host address, a logon username, and a logon password. There are also a few optional extras you can specify.

If you're with an ISP, the Host address is the one they gave you to set up your email account. The Host you need is the address for Outgoing Mail (SMTP Server). In the Image below, it's smtp.my_isp.net. Take note of your smtp port number, as well, which is 465 in the image below.

The settings page for an email client showing the smtp details

If your server requires authentication, and it probably does these days, then note down the username and password, because you'll need them shortly. But these are usually the same as for the Incoming Mail Server, in the image above.

You'll also need the port number for outgoing and incoming mail. The default is Port 25 for sending emails and 110 for receiving them, but yours may be different. If you're not sure about any of these, contact whoever provides your email. If you've got a website then you probably have email with it. Contact your hosting company for the smtp details, your password and username, as well as the port numbers for outgoing and incoming emails.

OK, now that we have all the details, we can set up our SmtpClient. Add the Host with this line of code (add a semicolon at the end if you're coding in C#):

smtpServer.Host = "smtp.my_isp.net"

Obviously, you should substitute smtp.my_isp.net for whatever your Host is.

You can also set a TimeOut (again, semicolon on the end for C#):

smtpServer.Timeout = 60

The TimeOut property is in seconds and is the length of time that Visual Studio will wait before giving up sending the email.

If your Port number is not 25, then you can set it with this:

smtpServer.Port = 545

Port 25 is the default, so you don't need this line, unless it's not 25. But it's best to set it anyway, in case Microsoft change the default.

You also set a delivery method for your email:

VB Net

smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network

C#

smtpServer.DeliveryMethod = SmtpDeliveryMethod.Network;

There are only two other options: Pickup directory from IIS, and Specified Pickup directory. We're assuming that you want to send your email through the Network directly to a SMTP server.

We also need to specify our SMTP logon credentials. Set up two string variables for this:

VB Net

Dim username As String = "my_username"
Dim password As String = "my_password"

C#

string username = " my_username ";
string password = " my_password ";

Again, specify your own Authentication details here. (You could add these to the Settings tab of the My Projects or Properties page, like you did for the database section. That way, you're not typing your authentication details directly in the code.)

The SmtpClient object we created has a Credentials property. This needs a NetworkCredential object. You set it up like this:

VB Net

smtpServer.Credentials = New NetworkCredential( username, password )

C#

smtpServer.Credentials = new NetworkCredential(username, password);

In between the round brackets of NetworkCredential, you need your username and password to log on to your SMTP server.

Your code should now look something like ours:

VB Net

Visual Basic NET code showing the use of the SmtpClient Class

C#

C# NET code showing the use of the SmtpClient Class

Now that we have an SmtpClient set up, we can create a new mail message. We'll do that on the next lesson.

Create a New Mail Nessage >>

Back to the C# NET Contents Page

Back to the VB NET Contents Page