Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[TUT] Sending Emails [VB]
#1
This tutorial is going to show you how to make a program which can send emails.
I've created this for a console application, but that's not necessary.



First we need to import this:

Code:
Imports System.Net.Mail



Now we start by making a Sub, all code will be located in the same sub.

Code:
Private Sub SendEmail(ByVal MailFrom As String, ByVal MailPass As String, _
                      ByVal MailTo As String, ByVal MailSubj As String, _
                      ByVal MailText As String, ByVal MailFromName As String, _
                      ByVal MailToName As String, _
                      Optional ByVal MailAttach As String = "")


Here we got:

MailFrom Your mail address
MailPass Your password
MailTo the person who you want to send the mail to's email address
MailSubj The subject of the email
MailText the text in the email
MailFromName the name which is showed as the sender
MailToName the name which is showed as the target
MailAttach files to attach with the email, sepperated with commas


Now we need to test all the variables, first:

Code:
If MailSubj = "" Then
            MailSubj = "No Subject"
        End If

If there was an empty string as Subject we replaced it with "No Subject".

Then we need to test the email addresses:

Code:
If MailTo = "" Then
            Console.WriteLine("Error.....You must enter a Email Adress as target...")
            Exit Sub
        End If

        Dim MailServer As String = ""

        If MailFrom = "" Then
            Console.WriteLine("Error.....You must enter a Email Adress as sender...")
            Exit Sub
        ElseIf MailFrom.ToLower.EndsWith("@gmail.com") Then
            MailServer = "smtp.gmail.com"
        ElseIf MailFrom.ToLower.EndsWith("@hotmail.com") Then
            MailServer = "smtp.live.com"
        Else
            Console.WriteLine("Error.....Unsupported host...")
            Exit Sub
        End If

First we tested if the Target's Email wasn't an empty string, the we tested the Sender's email and searched for the right MailServer (I only know gmails and hotmails. You can send to everyone though).

Then we check if the password exists:

Code:
If MailPass = "" Then
            WriteLine("Error.....You must enter your password...")
            Exit Sub
        End If


Now we're creating two variables as mail addresses, we got Try,catch blocks if the email addresses shouldn't be valid:


Code:
Dim mTo As System.Net.Mail.MailAddress
        Try
            Dim AdressTo As New System.Net.Mail.MailAddress(MailTo, MailToName)
            mTo = AdressTo
        Catch
            Console.WriteLine("Error.....Invailed Target Adress")
            Exit Sub
        End Try
        Dim mFrom As System.Net.Mail.MailAddress
        Try
            Dim AdressFrom As New System.Net.Mail.MailAddress(MailFrom, MailFromName)
            mFrom = AdressFrom
        Catch
            Console.WriteLine("Error.....Invailed Sender Adress")
            Exit Sub
        End Try




Now with a simple code we create the message and adds the subject and the text:

Code:
Dim NEWMail As New MailMessage(mFrom, mTo)

        NEWMail.Body = MailText
        NEWMail.Subject = MailSubj




Now we are going to add attachments, if their are any:


Code:
If MailAttach <> "" Then
            Dim Attach As String = ""
            For Chars As Integer = 0 To MailAttach.Length - 1
                If MailAttach(Chars) = "," Or (Chars = MailAttach.Length - 1 And Attach <> "") Then
                    Try
                        NEWMail.Attachments.Add(New Attachment(Attach))
                        Attach = ""
                    Catch
                        Console.WriteLine("Couldn't add the attachment")
                        Exit Sub
                    End Try
                Else
                    Attach &= MailAttach(Chars)
                End If
            Next

        End If


We just split the string to different attachment and then tried to add them as an attachment to the email.

We need to create a SmtpClient, It's it which sends the mail:

Code:
Dim client As New SmtpClient(MailServer, 587)

        client.EnableSsl = True
        client.Credentials = New Net.NetworkCredential(MailFrom, MailPass)



Now we only need to add the actual sending:



Code:
Console.WriteLine("Sending email...")
        Try
            client.Send(NEWMail)
        Catch
            Console.WriteLine("Error.....Failed to send the Email...")
            Exit Sub
        End Try
        Console.WriteLine("Email was sent sucessfully...")



That's it!! I will show an example how to use this:


Code:
Sub Main()
        SendEmail("MyMail@hotmail.com", "Password", "YourMail@hotmail.com", "Test Subject", "This is a text", "Me", "You", "C:\Test.txt,C:\Test2.txt")
    End Sub
[Image: 20r9vh4.jpg]
#2
Ripped from --> http://forum.codecall.net/vb-tutorials/1...mails.html
[Image: IKILLUNEW.jpg]
#3
Thanks but ripped
#4
Please do not leech or Rip tutorials without giving any credit to the original author.
At the top will be the same place you hang from.


Possibly Related Threads…
Thread Author Replies Views Last Post
  [TUT] How to make a simple WebBrowser in VB 2010! [TUT] - [ Pictures] Statics 95 62,011 10-07-2012, 06:56 AM
Last Post: a99
  [TUT]Auto-Update System[TUT] HB Virus 3 2,432 01-07-2012, 02:21 PM
Last Post: Mastermrz
  [TUT]Enable and Disable TaskManger in vb.net [TUT] HB Virus 4 3,180 12-19-2011, 10:10 AM
Last Post: euverve
  Windows Messages - SendMessage (Finding/Sending Windows Messages) - By Ace AceInfinity 8 2,127 08-01-2011, 02:28 AM
Last Post: AceInfinity
  [TuT] VB6 Message Box Spammer [TuT] HakkuR* 30 7,572 06-06-2011, 04:43 PM
Last Post: Black Demon

Forum Jump:


Users browsing this thread: 1 Guest(s)