Go Back   CodeCall Programming Forum > Software Development > Tutorials > VB Tutorials
Register Blogs Search Today's Posts Mark Forums Read

VB Tutorials Visual Basic Tutorials and Code

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 04-30-2009, 10:12 PM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
Sending emails

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

If you have any questions, feel free to ask.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #2 (permalink)  
Old 04-30-2009, 10:47 PM
WingedPanther's Avatar
Super Moderator
 
Join Date: Jul 2006
Age: 36
Posts: 11,435
WingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud ofWingedPanther has much to be proud of
Re: Sending emails

+rep, and very nicely done.
__________________
CodeCall Blog | CodeCall Wiki | Shareware
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 05-01-2009, 06:37 AM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: Sending emails

Impressive!

Very well written.

One question: what if you wanted this function to work with all domains?

+rep for you.
__________________
"Whenever you remember, I'll be there/
Remember how we reached that dream together" - Carrie Underwood
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 05-01-2009, 06:42 AM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
Re: Sending emails

You need to know their host address (or what to call them, like "smtp.gmail.com" or "smtp.live.com"). And then you need to add them where you test the Sender's email (see code block #4).
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 05-01-2009, 06:54 AM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
Re: Sending emails

If's just to search the web for "SMTP Host names" and you will get many result, here's a list I found:

Internet Provider SMTP Host name

America Online aol.com
AT&T Business Internet Services mail.attbi.com
CalWeb smtp.calweb.com
Comcast Cable smtp.comcast.net
cwnet.com mail.cwnet.com
Davis Community Network smtp.dcn.davis.ca.us
DirectConnect smtp.directcon.net
Earthlink smtp.earthlink.net
MSN smtp.msn.com
Mindspring smtp.mindspring.com
Mother.com mail.mother.com
Netcom smtp.netcom.com
Pipeline smtp.earthlink.net
River City Internet mail.rcip.com
Sprynet smtp.earthlink.net
SureWest smtp.surewest.net
PSINet bbr0-f1.sna.com
SBC smtp.sbcglobal.net
StarPower smtp.starpower.net
Tomato Web mail.tomatoweb.com
Verizon smtpout.verizon.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 05-01-2009, 06:56 AM
chili5's Avatar
Code Slinger
 
Join Date: Mar 2008
Posts: 7,018
chili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond reputechili5 has a reputation beyond repute
Re: Sending emails

Okay cool!

What you have is fantastic!!!!!
__________________
"Whenever you remember, I'll be there/
Remember how we reached that dream together" - Carrie Underwood
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 05-08-2009, 02:23 PM
VBnet's Avatar
Newbie
 
Join Date: May 2009
Location: Sweden :D
Posts: 18
VBnet is on a distinguished road
Send a message via MSN to VBnet
Re: Sending emails

Quote:
Originally Posted by Vswe View Post
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

If you have any questions, feel free to ask.
i can make one much better
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #8 (permalink)  
Old 05-08-2009, 03:29 PM
Vswe's Avatar
Code Slinger
 
Join Date: Apr 2009
Location: Uppsala, Sweden
Age: 16
Posts: 8,536
Vswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond reputeVswe has a reputation beyond repute
Send a message via MSN to Vswe Send a message via Skype™ to Vswe
Re: Sending emails

Ok?
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
code a program in C to grab emails shadowhound C and C++ 7 01-31-2009 02:34 AM
Sending mail to a group of people (Mailing list support/Emulation) nesrait easyContact 1 07-25-2007 09:48 PM


All times are GMT -5. The time now is 09:50 AM.


vBulletin v3.8.0 ©2010, Jelsoft Enterprises Ltd.


no new posts

LinkBacks Enabled by vBSEO 3.1.0