Results 1 to 2 of 2

Thread: Marquee progressbar while sending email

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Nov 2008
    Posts
    170

    Marquee progressbar while sending email

    Hi everyone,
    I am succesfully trying to send an email message through my windows form. The only problem is when i click the button to send mail it is not showing the prograssbar. It shows after a delay or sometimes doesn't show at all.

    Is it due to the event handler? I have no clue and all i need to first show is the progressbar when the send button is clicked and then when sending message is successful or unsuccessful then hide progressbar and display the Txtmessage.text.

    Any help!!!
    Thanx and Cheers
    irfi

    Code:
    Public Sub SendEmail()
    
            ProgressBar1.Visible = True
            
            Dim client As New SmtpClient()
            Dim sendTo As New MailAddress(To.Text)
            Dim from As MailAddress = New MailAddress("From.Text")
            Dim message As New MailMessage(from, sendTo)
    
            message.IsBodyHtml = True
            message.Subject = "Hi this is a test"
            
            'for attachement
            Dim att As Attachment = Nothing
            att = New Attachment("c:\attach.pdf")
            message.Attachments.Add(att)
    
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential(From.Text, TextBox3.Text)
            client.Port = 587
    
          client.Host = "smtp.gmail.com"
            client.UseDefaultCredentials = False
            client.Credentials = basicAuthenticationInfo
            client.EnableSsl = True
    
            Try
                client.Send(message)
    
                ProgressBar1.Visible = False
    
               TxtMessage.Text  = "Message sent succesfully!"
    
            Catch ex As Exception
    
                 ProgressBar1.Visible = False
    
                 TxtMessage.Text  = "Message Failed!" 
            End Try
    
        End Sub
    Code:
    Private Sub send_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles send.Click
    
    ProgressBar1.Visible = True
    
    Call SendEmail()

  2. #2
    Stack Overflow mod​erator
    Join Date
    May 2008
    Location
    British Columbia, Canada
    Posts
    2,824

    Re: Marquee progressbar while sending email

    Code:
    Public Sub SendEmail()
    
            ProgressBar1.Visible = True
            My.Application.DoEvents()
            
            Dim client As New SmtpClient()
            Dim sendTo As New MailAddress(To.Text)
            Dim from As MailAddress = New MailAddress("From.Text")
            Dim message As New MailMessage(from, sendTo)
    
            message.IsBodyHtml = True
            message.Subject = "Hi this is a test"
            
            'for attachement
            Dim att As Attachment = Nothing
            att = New Attachment("c:\attach.pdf")
            message.Attachments.Add(att)
    
        Dim basicAuthenticationInfo As New System.Net.NetworkCredential(From.Text, TextBox3.Text)
            client.Port = 587
    
          client.Host = "smtp.gmail.com"
            client.UseDefaultCredentials = False
            client.Credentials = basicAuthenticationInfo
            client.EnableSsl = True
    
            Try
                client.Send(message)
    
                ProgressBar1.Visible = False
    
               TxtMessage.Text  = "Message sent succesfully!"
    
            Catch ex As Exception
    
                 ProgressBar1.Visible = False
    
                 TxtMessage.Text  = "Message Failed!" 
            End Try
    
        End Sub
    But it's not asynchronous anyways, so you should probably run this on a BackgroundWorker.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width