如何在vb.net中发送电子邮件时使用进度条?

时间:2009-12-26 07:50:50

标签: vb.net

如何在发送电子邮件时在vb.net中添加进度条?

3 个答案:

答案 0 :(得分:3)

在调用SmtpClient.SendAsync()之前,将ProgressBar.Visible属性设置为True。在SmtpClient.SendCompleted事件的事件处理程序中将其设置为False。 PB必须将其Style属性设置为Marquee。

您无法提供准确的进度信息,StmpClient和MailMessage类都没有一个事件可以告诉您完成了多少工作。

答案 1 :(得分:2)

只需在您的smpt客户端代码的每一步之后放置此代码,并在每个步骤后增加值,

progressbar1.value = 10

以下代码可以帮助您..

包含: 3个文本框(TB_subject,TB_name,TB_cmt) 1个按钮(btn_submit) 1个进度条(Progressbar1) 和 3个标签。

例如: -

代码:

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("youremail@gmail.com")
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

答案 2 :(得分:0)

您可以使用计时器控件并使处理程序中的进度条移动。您可以在进度条达到最大值时将其重置为零。这不会反映实际进度,但它会给用户一些观看内容,并显示应用程序没有被锁定。