Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Jas91

Thread: Class delays the running of the program

  1. #1
    gididaf is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2010
    Posts
    8
    Reputation
    0

    Class delays the running of the program

    Hello everyone!
    I found on the web a class that send a mail with gmail account:

    Code:
    Public Class MailSender
    
        Public Sub SendMail()
            Try
                Dim SmtpServer As New System.Net.Mail.SmtpClient()
                Dim mail As New System.Net.Mail.MailMessage()
    
                SmtpServer.Credentials = New Net.NetworkCredential("[my gmail]@gmail.com", "[my password]")
                SmtpServer.Port = 587
                SmtpServer.Host = "smtp.gmail.com"
                SmtpServer.EnableSsl = True
    
                mail = New System.Net.Mail.MailMessage()
                mail.From = New System.Net.Mail.MailAddress("[my gmail]")
                mail.To.Add("[other mail]")
                mail.Subject = "Test Mail"
                mail.Body = "This is for testing SMTP mail from GMAIL"
                SmtpServer.Send(mail)
                MsgBox("mail send")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    
    End Class
    and it's work great!!!
    but there is one problem
    when i create a button perform the class, the whole program get stuck for a while until the mail send
    can i do something to make the class run on background?

    Code:
    Public Class frmSender
    
        Dim m As New MailSender
    
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            m.SendMail()
        End Sub
    
    End Class

  2. #2
    Jas91 is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Jul 2012
    Location
    Baghdad, Italy. OK?=
    Posts
    17
    Reputation
    13
    try to call it as an asynchronous method, like this:
    Code:
      
      Public Sub SendMail() 
           Try
                Dim SmtpServer As New System.Net.Mail.SmtpClient()  
                Dim mail As New System.Net.Mail.MailMessage()
                SmtpServer.Credentials = New Net.NetworkCredential("[my gmail]@gmail.com", "[my password]")  
                SmtpServer.Port = 587    
                SmtpServer.Host = "smtp.gmail.com"    
                SmtpServer.EnableSsl = True    
                mail = New System.Net.Mail.MailMessage()   
                mail.From = New System.Net.Mail.MailAddress("[my gmail]")     
                mail.To.Add("[other mail]")            mail.Subject = "Test Mail"     
                mail.Body = "This is for testing SMTP mail from GMAIL"   
                SmtpServer.Send(mail)   
                MsgBox("mail send")    
           Catch ex As Exception    
                MsgBox(ex.ToString)    
           End Try
      End Sub 
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim NewThread As New System.Threading.Thread(AddressOf SendMail)
            NewThread.Start()
      End Sub
    this way the code will be run on another thread and your app will keep working as active
    gididaf likes this.

  3. #3
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,176
    Reputation
    2368
    SmtpClient also has the SendAsync method.

  4. #4
    gididaf is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2010
    Posts
    8
    Reputation
    0
    Quote Originally Posted by Jas91 View Post
    try to call it as an asynchronous method, like this:
    Code:
      
      Public Sub SendMail() 
           Try
                Dim SmtpServer As New System.Net.Mail.SmtpClient()  
                Dim mail As New System.Net.Mail.MailMessage()
                SmtpServer.Credentials = New Net.NetworkCredential("[my gmail]@gmail.com", "[my password]")  
                SmtpServer.Port = 587    
                SmtpServer.Host = "smtp.gmail.com"    
                SmtpServer.EnableSsl = True    
                mail = New System.Net.Mail.MailMessage()   
                mail.From = New System.Net.Mail.MailAddress("[my gmail]")     
                mail.To.Add("[other mail]")            mail.Subject = "Test Mail"     
                mail.Body = "This is for testing SMTP mail from GMAIL"   
                SmtpServer.Send(mail)   
                MsgBox("mail send")    
           Catch ex As Exception    
                MsgBox(ex.ToString)    
           End Try
      End Sub 
    
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim NewThread As New System.Threading.Thread(AddressOf SendMail)
            NewThread.Start()
      End Sub
    this way the code will be run on another thread and your app will keep working as active

    WOW!!!!!!! It's working!!!!!!! Thank you very much!!!!!!!

  5. #5
    gididaf is offline VB.NET Forum Newbie
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2010
    Posts
    8
    Reputation
    0
    Quote Originally Posted by JohnH View Post
    SmtpClient also has the SendAsync method.
    SendAsync(message, userToken)

    what is the userToken?

  6. #6
    JohnH's Avatar
    JohnH is offline VB.NET Forum Moderator
    .NET Framework
    .NET 4.0
    Join Date
    Dec 2005
    Location
    Norway
    Posts
    14,176
    Reputation
    2368
    Quote Originally Posted by gididaf View Post
    SendAsync(message, userToken)

    what is the userToken?
    SmtpClient.SendAsync Method (MailMessage, Object) (System.Net.Mail)
    Quote Originally Posted by userToken
    A user-defined object that is passed to the method invoked when the asynchronous operation completes.
    You can use it to your liking, or pass Nothing if you don't like it.

Bookmarks

Posting Permissions

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