A Simple Gmail Client

In This Post I will Guide u To making a Simple Gmail Client  to send Your Emails using Gmail Account Using Vb.net

 Let me Show You how final Result will Look Like:



1. For Sending Mail Through Vb.net. This Client Uses  “Imports System.Net.Mail”.
    So First Import it to your Code




Imports System.Net.Mail

2. Layout All the Controls As Shown In the Above Figure

3. There is Progress Bar after the send Button. I have used Backgroud Worker To Send the Mail. So that When Mail Sending is in Progress the Screen Wont Stop Working.

4. Now Here is the Complete Code

First Define This In your Form

Dim msg As New MailMessage 
Dim i As Integer

Form’s Load Event

Me.ProgressBar1.MarqueeAnimationSpeed = 0

Write This to Send Button’s Click Event

msg.To.Add(Me.txtto.Text) 
msg.From = New MailAddress(Me.txtuser.Text)
msg.Subject = Me.txtsubject.Text
msg.Body = Me.txtbody.Text
'' for attachment
For i = 0 To Me.ListBox1.Items.Count - 1
msg.Attachments.Add(New Attachment(Me.ListBox1.Items(i).ToString))
Next
Me.Button1.Enabled = False
Me.Button1.Text = "Sendign.."
Me.ProgressBar1.Visible = True
Me.ProgressBar1.MarqueeAnimationSpeed = 100
Me.BackgroundWorker1.RunWorkerAsync()

Attach Button’s Click Event ( Button beside the listbox)

Dim d As New OpenFileDialog 
d.ShowDialog()
Me.ListBox1.Items.Add(d.FileName.ToString())
Me.ListBox1.Visible = True

Now its the Back Ground Worker …
Background worker’s Do work Event:

Private Sub BackgroundWorker1_DoWork(ByVal sender As System.Object, ByVal e As System.ComponentModel.DoWorkEventArgs) Handles BackgroundWorker1.DoWork 

Dim i As Integer = 0
Dim smtp As New SmtpClient
smtp.Host = "smtp.gmail.com"
smtp.EnableSsl = True
smtp.Port = 587
smtp.Timeout = 300000
smtp.Credentials = New Net.NetworkCredential(Me.txtuser.Text, Me.txtpass.Text)
'Dim ms As MailMessage
'ms = e.Argument
Try
' Label5.Text = "Sending Message......."
'Me.Button1.Text = "Sending...."
' Me.Button1.Enabled = False
'Me.BackgroundWorker1.ReportProgress(i,
smtp.Send(msg)
e.Result = "Done"
Catch ex As Exception
'Label5.Text = "Sending Failed........"
MessageBox.Show(ex.ToString())
End Try

Background Worker’s Run work Completed Event:

Private Sub BackgroundWorker1_RunWorkerCompleted(ByVal sender As System.Object, ByVal e As System.ComponentModel.RunWorkerCompletedEventArgs) Handles BackgroundWorker1.RunWorkerCompleted 
' Label5.Text = "Message Sending Complete"
Me.ProgressBar1.MarqueeAnimationSpeed = 0
Me.Button1.Text = "Send"
Me.Button1.Enabled = True
MessageBox.Show("Sent")
End Sub

And It’s Done.

For Complete Project Email me at [email protected]

 

Sandeep

a dev, an amateur photographer and a father

 

2 thoughts on “A Simple Gmail Client

  1. I was looking for more information on Gmail
    and stumbled on sparekh.com. I just want to thank
    you for the information in this article. I am also working on a gmail tutorial right
    now.

Leave a Reply

Your email address will not be published. Required fields are marked *

%d bloggers like this: