This small article is just going to cover the Normal basic features of a email client to send mail messages. This post shows how to Send a email using Visual Basic 2005, VB 2008, and VB.NET 2010.
Edit: I suggest you also check out the other articles i’ve make that expands on this article about sending your email messages with advanced features. Check out this one, and another one will be done shortly.
Note: I do want to thank the author of a article at The Code Project for which I got the info for using gMail as the smtp host.
Maybe when I get time I will do some research and make a article on adding more mail features.
To send a mail message through .NET 05, VB 08, Visual Basic 2010 you will want to head to the System.Net.Mail Namespace which contains the important Classes for setting up and sending mail messages. The main class to use is the SMTPClient class. Other classes that will be used is MailMessage and Attachment. To get started Three(Objects) needs to be made to…
- the SMTPClient Class
- the MailMessage Class
- and the Attachment Class
The following code will first setup the System.Net.Mail.MailMessage Class.
' 'This will contain the actual message to send. Dim message As System.Net.Mail.MailMessage
The next class is the SMTPClient…
' 'The Simple Mail Tranfer Protocol client with the Host and Port number to use. You will want to change these settings to what you need to use. Dim smtp As New System.Net.Mail.SmtpClient("smtp.gmail.com", 587)
The code above has created and setup the SMTP Client. Googles gMail actually requires 587 to be the port number from what I understand. You can leave out the Port number if your SMTP doesn’t require a specific port. Also remember if you are not using the GMail to send your messages then you need to change the Host parameter to the Host you are going to use to send your mails. Example: “mysmtp.host.com”
The final Class is the Attachment class…
' 'Will contain the attachment info to send with the message. Dim attach As System.Net.Mail.Attachment ' 'Setup the mail message using 4x Textbox controls that contains the From/To/Subject/Message-Body info. 'You just need to set it up the way you want it to be… Message = New System.Net.Mail.MailMessage(txtFrom.Text, txtTo.Text, txtSubject.Text, txtMessage.Text)
The code below will check a attachment to make sure the file exists..
'Need to make sure the attachment exists before adding it to the message. If My.Computer.FileSystem.FileExists(txtAttachment.Text) Then Message.Attachments.Add(attach)
Set the Secure Socket Layer to be ‘TRUE’
' 'Use Secure Socket Layer to Encrypt the connection for sending the mail. 'This needs be set to “True” if you plan on using GMail or Hotmail or Yahoo as your host. smtp.EnableSsl = True
And now your can set up your Credentials for your Login info. (Username and Password)
' 'Setup your account information for authorization… smtp.Credentials = New System.Net.NetworkCredential("yourUsername@yourmail.com", "password")
Now that the Mail setup is completed. The only thing left to do is Send your message.
Try ' 'Send the mail message that was setup. smtp.Send(Message) ' 'Usually it was successful in sending the message if it makes it to here 'Otherwise it should throw a error message from the Catch exception below. 'You need to remember that after this point its up to the Host to do the rest. MessageBox.Show("Well, the mail message appears to have been asuccess!", _ "Successful?",MessageBoxButtons.OK, MessageBoxIcon.Information) Catch exc As Net.Mail.SmtpException MessageBox.Show(exc.StatusCode.ToString, " Something Happened?", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try
Well all of this code in the article works just fine for me to send messages with attachments. If using VB.NET 02/03 you may have to do some modification. I don’t think .NET 1.0/1.1 has the System.Net.Mail namespace and class. I will have to take a look when I can to see the diferrences and will then post them. Please leave a comment if you know if/what is different so I can update this article. Hopefully it does for your too Take care…