Send an E-Mail using VB.NET

Heres your chance to share your own tutorials with the community. Just post them on here. If your lucky they may even be posted on the main site.
6 posts Page 1 of 1
Contributors
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Send an E-Mail using VB.NET
Skillful
Hello guys.

Welcome to my new tutorial.

In this tutorial I'll be teaching you how to send an E-Mail using VB.NET. I decided to choose this topic as a tutorial because I made an E-Mail sender for yesterday's Mad March challenge lol.

OK so lets start! :)

First up open Visual Studio and create a new project. Name it anything that you want.

Next when the form loads make a GUI similar to this.
Image

Name your From TextBox as txtFrom, Server TextBox as txtServer, Username TextBox as txtUsername, Password TextBox as txtPassword, To TextBox as txtTo, Subject TextBox as txtSubject, Message RichTextBox as rtbmessage, ... button as btnBrowse, Send Button as btnsend, Include HTML CheckBox as CheckBox1 and Include Attachment CheckBox as CheckBox2.

Next Right Click on the Form --> View Code --> Add this code under Public Form1.
Code: Select all
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim ofd As New OpenFileDialog
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtattachment.Text = ofd.FileName
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim from As New MailAddress(txtFrom.Text)
        Dim toaddress As New MailAddress(txtTo.Text)
        Dim mail1 As New MailMessage(from, toaddress)
        Dim att = txtattachment.Text
        If chkHTML.Checked = True Then
            mail1.IsBodyHtml = True
            mail1.Body = rtbmessage.Text
            mail1.Subject = txtSubject.Text
            If chkAttachment.Checked Then
                mail1.Attachments.Add(New Attachment(att))
            End If
            Dim tehclient As New SmtpClient(txtServer.Text)
            Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
            tehclient.Credentials = tehcredentials
            tehclient.EnableSsl = True
            Try
                tehclient.Send(mail1)
                MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        Else
            mail1.IsBodyHtml = False
            mail1.Body = rtbmessage.Text
            mail1.Subject = txtSubject.Text
            If chkAttachment.Checked Then
                mail1.Attachments.Add(New Attachment(att))
            End If
            Dim tehclient As New SmtpClient(txtServer.Text)
            Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
            tehclient.Credentials = tehcredentials
            tehclient.EnableSsl = True
            Try
                tehclient.Send(mail1)
                MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex2 As Exception
                MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAttachment.CheckedChanged
        If chkAttachment.Checked Then
            btnBrowse.Enabled = True
            txtattachment.Enabled = True
        Else
            btnBrowse.Enabled = False
            txtattachment.Enabled = False
        End If
    End Sub
Now you will get a few errors. To fix those just add this at the top of Public Class Form1.
Code: Select all
Imports System.Net.Mail
So now the Final Code should look something like this -
Code: Select all
Imports System.Net.Mail
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBrowse.Click
        Dim ofd As New OpenFileDialog
        If ofd.ShowDialog = Windows.Forms.DialogResult.OK Then
            txtattachment.Text = ofd.FileName
        End If
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
        Dim from As New MailAddress(txtFrom.Text)
        Dim toaddress As New MailAddress(txtTo.Text)
        Dim mail1 As New MailMessage(from, toaddress)
        Dim att = txtattachment.Text
        If chkHTML.Checked = True Then
            mail1.IsBodyHtml = True
            mail1.Body = rtbmessage.Text
            mail1.Subject = txtSubject.Text
            If chkAttachment.Checked Then
                mail1.Attachments.Add(New Attachment(att))
            End If
            Dim tehclient As New SmtpClient(txtServer.Text)
            Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
            tehclient.Credentials = tehcredentials
            tehclient.EnableSsl = True
            Try
                tehclient.Send(mail1)
                MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex As Exception
                MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        Else
            mail1.IsBodyHtml = False
            mail1.Body = rtbmessage.Text
            mail1.Subject = txtSubject.Text
            If chkAttachment.Checked Then
                mail1.Attachments.Add(New Attachment(att))
            End If
            Dim tehclient As New SmtpClient(txtServer.Text)
            Dim tehcredentials As New System.Net.NetworkCredential(txtUsername.Text, txtPassword.Text)
            tehclient.Credentials = tehcredentials
            tehclient.EnableSsl = True
            Try
                tehclient.Send(mail1)
                MessageBox.Show("Message sent successfully!", "Success!", MessageBoxButtons.OK, MessageBoxIcon.Information)
            Catch ex2 As Exception
                MessageBox.Show("There was some problem sending your message. Please try again.", "Error!", MessageBoxButtons.OK, MessageBoxIcon.Error)
            End Try
        End If

    End Sub

    Private Sub CheckBox2_CheckedChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles chkAttachment.CheckedChanged
        If chkAttachment.Checked Then
            btnBrowse.Enabled = True
            txtattachment.Enabled = True
        Else
            btnBrowse.Enabled = False
            txtattachment.Enabled = False
        End If
    End Sub
End Class
What the code under the btnBrowse click event does -
Declares new OpenfileDialog. Shows the OpenFileDialog and if the user clicks the OK button, displays the path of the file selected in txtAttachment.

What the code under the btnSend click event does -
Declares from as a New MailAddress that is your from address in the txtFrom textbox.
Declares toaddress as a New MailAddress that is your to address in the txtTo textbox.
Declares mail1 as a new MailMessage and the (from, toaddress) is the from address and the To address.
Declares att and assigns it the value of txtAttachment.Text
Checks if the chkHTML is checked or not.
If it is checked then declares the body of the message to be in the HTML format.
Then sets the mail1 message body as the rtbmessage.Text.
Sets the mail1 subject as txtSubject.Text.

If the chkHTML is not checked then does the same but does not declare the body of the message to be in HTML format.

Declares tehclient as a New SMTPClient which is txtServer.Text.
Declares tehcredentials as New System.Net.NetworkCredential which is your username(txtUsername.Text) and your password (txtPassword.Text)
Sets the credentials of tehclient as tehcredentials.
Enables SSL
Sends the message! :D


And that's it you have created and understood how to create an E-Mail Sender! :D

Hope you liked it! :)


Regards,
Skillful
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
M1z23R
VIP - Donator
VIP - Donator
Posts: 622
Joined: Tue Sep 28, 2010 4:55 pm

Re: Send an E-Mail using VB.NET
M1z23R
To make multi attachments:

Add listview to form. Set it's view to details.
Add 'Browse' or 'Add' button and inside put this :
Code: Select all

Dim opfd as new openfiledialog
opfd.showdialog
if opfd.filename ="" then exit sub

dim lvi as new listviewitem
lvi.text = opfd.safefilename
lvi.tag = opfd.filename
listview.items.add(lvi)
I placed it like that so you see your file name without it's full location.

Now, to add attachments to mail use this:
Code: Select all
For each lvi as listviewitem in listview.items
Dim att as new net.attachment(lvi.tag)
'Dim mail1 As New MailMessage(from, toaddress)
'This is already declared like in code above by skillful
'So mail1 is mailmessage
mail1.attachments.add(att)
Next

'Send mail
User avatar
Skillful
Skillful Coders
Skillful Coders
Posts: 969
Joined: Tue Nov 16, 2010 10:07 am

Re: Send an E-Mail using VB.NET
Skillful
Thanks for the updates M1z23R. cooll;
I'll update the post when I get home.
Instead of LOL use this -
LSIBMHBIWFETALOL

Which means -
Laughing silently in between my head because it wasn't funny enough to actually laugh out loud!
User avatar
Shim
VIP - Donator
VIP - Donator
Posts: 882
Joined: Wed Dec 14, 2011 5:02 am

Re: Send an E-Mail using VB.NET
Shim
thank you skillful i really needed this ;-)
Find my programs on Softpedia
User avatar
MrAlicard
VIP - Donator
VIP - Donator
Posts: 54
Joined: Thu Aug 05, 2010 4:08 pm

Re: Send an E-Mail using VB.NET
MrAlicard
Thanks. Ok I can send e-mail,but how to receive e-mail? :P
User avatar
mandai
Coding God
Coding God
Posts: 2585
Joined: Mon Apr 26, 2010 6:51 pm

Re: Send an E-Mail using VB.NET
mandai
but how to receive e-mail?
You would need to implement an SMTP server to directly receive emails, or if the messages are stored elsewhere you could access them with a POP3/IMAP client.
6 posts Page 1 of 1
Return to “Tutorials”