Visual Basic .NET - SMTP Mail Application

This tutorial will show you have to create an application to send mail via your VB application. You can use this as an error reporting tool or something similar.

This is one of the many tutorials that I'll create. I will do them upon request as well.

So we're aiming for this:

image: smtpmail

Code
Imports System.Net.Mail ' imports the smtp to be used
Imports MessengerAPI
Public Class Form1
Dim objMSN As New MessengerAPI.Messenger
Dim msn As New Messenger
Private Sub btnLogin_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLogin.Click

Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential(txtEmail.Text, txtPassword.Text)

'Login
smtpServer.Port = (txtPort.Text)
smtpServer.Host = (txtServer.Text)
smtpServer.EnableSsl = True
mail = New MailMessage()
MsgBox("Connected!")

End Sub

Private Sub lstEmails_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles lstEmails.Click
txtTo.Text = lstEmails.SelectedItem
End Sub

Private Sub HotmailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles HotmailToolStripMenuItem.Click
txtPort.Text = "25"
txtServer.Text = "smtp.live.com"

End Sub

Private Sub GmailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GmailToolStripMenuItem.Click
txtPort.Text = "465 "
txtServer.Text = "smtp.gmail.com"
End Sub

Private Sub YahooToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YahooToolStripMenuItem.Click
txtPort.Text = "465"
txtServer.Text = "smtp.mail.yahoo.com"
End Sub

Private Sub BTToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BTToolStripMenuItem.Click
txtPort.Text = "25"
txtServer.Text = "mail.btinternet.com"
End Sub

Private Sub VirginMediaToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles VirginMediaToolStripMenuItem.Click
txtPort.Text = "25"
txtServer.Text = "smtp.ntlworld.com"
End Sub


Private Sub AboutMeToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AboutMeToolStripMenuItem.Click
Dialog1.Show()
End Sub


Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
txtEmail.Text = ""
txtFrom.Text = ""
txtMessage.Text = ""
txtPassword.Text = ""
txtPort.Text = ""
txtServer.Text = ""
txtSubject.Text = ""
txtTo.Text = ""
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim msncontact As MessengerAPI.IMessengerContact
Dim msncontacts As MessengerAPI.IMessengerContacts = objMSN.MyContacts

' Get rid of anything in the list.
lstEmails.Items.Clear()

' Loop through all the contacts and add the SigninName to the list.
For Each msncontact In msncontacts
lstEmails.Items.Add(msncontact.SigninName)
Next

End Sub

Private Sub LinkLabel1_LinkClicked(ByVal sender As System.Object, ByVal e As System.Windows.Forms.LinkLabelLinkClickedEventArgs) Handles LinkLabel1.LinkClicked
System.Diagnostics.Process.Start("http://crossfire.nu")
End Sub

Private Sub GetMoreToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles GetMoreToolStripMenuItem.Click
System.Diagnostics.Process.Start("http://www.google.co.uk/#hl=en&source=hp&q=SMTP+Servers&btnG=Google+Search&meta=&aq=f&oq=SMTP+Servers&fp=c6c9946001627c7b")
End Sub

Private Sub CrossfirenuToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CrossfirenuToolStripMenuItem.Click
System.Diagnostics.Process.Start("http:// Crossfire.nu")
End Sub

Private Sub YouTubeAccountToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles YouTubeAccountToolStripMenuItem.Click
System.Diagnostics.Process.Start("http://youtube.com/youngie1337")
End Sub

Private Sub SaveEmailToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveEmailToolStripMenuItem.Click
Dim SaveFile As New SaveFileDialog
SaveFile.FileName = ""
SaveFile.Filter = "Text Files (*.txt)|*.txt"
SaveFile.Title = "Save Emails Message Contents to .txt"
SaveFile.ShowDialog()
Try
Dim Write As New System.IO.StreamWriter(SaveFile.FileName)
Write.Write("Email Message! " + ControlChars.NewLine + txtMessage.Text)
Write.Close()
Catch ex As Exception

End Try
End Sub

Private Sub SaveContactsToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveContactsToolStripMenuItem.Click
Dim SaveEmail As New SaveFileDialog
Dim filenumber As Integer = FreeFile()
SaveEmail.FileName = ""
SaveEmail.Filter = "Text Files (*.txt)|*.txt"
SaveEmail.Title = "Save Contact Emails To .txt"
SaveEmail.ShowDialog()
Try
Using SW As New IO.StreamWriter(SaveEmail.FileName, True)
For Each itm As String In Me.lstEmails.Items
SW.WriteLine(itm)
Next
End Using
Catch ex As Exception
End Try
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim smtpServer As New SmtpClient()
Dim mail As New MailMessage()
smtpServer.Credentials = New Net.NetworkCredential(txtEmail.Text, txtPassword.Text)

'Login
Bar.Value = 5
smtpServer.Port = (txtPort.Text)
Bar.Value = 10
smtpServer.Host = (txtServer.Text)
Bar.Value = 20
smtpServer.EnableSsl = True
Bar.Value = 27
mail = New MailMessage()
Bar.Value = 39
mail.From = New MailAddress(txtFrom.Text)
Bar.Value = 50
mail.To.Add(txtTo.Text)
Bar.Value = 60
mail.Subject = (txtSubject.Text.ToString)
Bar.Value = 80
mail.Body = txtMessage.Text
Bar.Value = 100
smtpServer.Send(mail)
MsgBox("Message was sent!")
Bar.Value = 0
End Sub

Private Sub QuitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles QuitToolStripMenuItem.Click
End
End Sub
End Class
/CODE
Comments
6
i'm 12 and what is this
Cool tutorial bro!

You provide a short source with no real tutoring.
how is that a tutorial :s where are the explanations what commands and sections do
Stop poisoning the minds of young programmers with your crap code
plz gtfo with this shit
wow you learned something from school, but please dont come to show this here.
Back to top