user225269
user225269

Reputation: 10913

How To Use Progress Bar While Sending an Email in vb.net?

how can I add a progress bar in vb.net while sending an email message?

Upvotes: 1

Views: 4226

Answers (3)

Saqib
Saqib

Reputation: 2273

Just put this code after every step of your smpt client code and increase the value after each step,

progressbar1.value = 10

Below Codes May Help You..

contains: 3 Textboxes(TB_subject, TB_name, TB_cmt) 1 button (btn_submit) 1 progress bar (Progressbar1) and 3 labels.

For Example:-

Codes:

Imports System.Net.Mail
____________________________________________________________________
Public Class Form1

Private Sub btn_submit_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_submit.Click
If TB_Name.Text = "" Or TB_Subject.Text = "" Or TB_Cmt.Text = "" Then
MsgBox("Name, Subject and Comment are required fields", vbCritical, "Error")

Else



Try
Dim Mail As New MailMessage
progressbar1.value = 10         'note that the value is "10"

Mail.From = New MailAddress("youremail@gmail")
progressbar1.value = 20                        'now its "20"

Mail.To.Add("[email protected]")
progressbar1.value = 30                        '"30" and its increases.....

Mail.Subject = TB_Subject.Text & " - " & TB_Name.Text
progressbar1.value = 40

Mail.Body = TB_Cmt.Text
progressbar1.value = 50

Dim smtp As New SmtpClient("smtp.gmail.com")
progressbar1.value = 60

smtp.Port = 587 
progressbar1.value = 70

smtp.EnableSsl = True
progressbar1.value = 80

smtp.Credentials = New System.Net.NetworkCredential("YOUR GMAIL USERNAME ID HERE", "YOUR GMAIL PASSWORD HERE")
progressbar1.value = 90

smtp.Send(Mail)
progressbar1.value = 100


MsgBox("Sent Successfully", vbInformation, "Thank you")
progressbar1.value = 0                 'Reset Progress Bar.

Catch ex As Exception
MsgBox("There was an error, the program will close now!!", vbCritical, "Fatal error")
End Try
End If
End Sub

Upvotes: 2

xpda
xpda

Reputation: 15813

You can use a timer control and make the progressbar move in the handler. You can reset the progress bar back to zero when it reaches the max value. This won't reflect the actual progress, but it will give the user something to watch and show that the application is not locked up.

Upvotes: 0

Hans Passant
Hans Passant

Reputation: 941665

Just before you call SmtpClient.SendAsync(), set the ProgressBar.Visible property to True. Set it to False in an event handler for the SmtpClient.SendCompleted event. The PB must have its Style property set to Marquee.

You cannot otherwise give accurate progress info, neither the StmpClient nor the MailMessage class has an event that tells you how much of the job got done.

Upvotes: 3

Related Questions