Question Class delays the running of the program

gididaf

Member
Joined
Dec 30, 2010
Messages
8
Programming Experience
5-10
Hello everyone!
I found on the web a class that send a mail with gmail account:

VB.NET:
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?

VB.NET:
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
 
try to call it as an asynchronous method, like this:
VB.NET:
  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
 
SmtpClient also has the SendAsync method.
 
try to call it as an asynchronous method, like this:
VB.NET:
  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!!!!!!!
 
Back
Top