Archive for November, 2010

[VB.Net] BackgroundWorker

Here is some code I have found on the net to send email’s via SMTP through VB.Net. Untested, so if you use please let me know if it works.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

BackgroundWorker Class

Executes an operation on a separate thread.

Sample Code

1.Create event DoWork.

Private Sub BackgroundWorker1_DoWork(ByVal sender As Object, ByVal e As DoWorkEventArgs)
'Sample how to disable button send before send email method after that enabled button.
DisableSendButton()
' to do SendEmail method
EnabledSendButton()
End Sub

2. Show how to invoke method when use BackgroundWorker control.

Private Sub DisableSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(DisableSendButton))
Else
btnSend.Enabled = False
btnCancelSend.Visible = True
End If
End Sub

Private Sub EnabledSendButton()
If Me.InvokeRequired Then
Me.Invoke(New MethodInvoker(EnabledSendButton))
Else
btnSend.Enabled = True
btnCancelSend.Visible = False
End If
End Sub

3.Start BackgroundWorker

BackgroundWorker1.RunWorkerAsync()

[VB.Net] Send Email By SMTP

Here is some code I have found on the net to send email’s via SMTP through VB.Net. Untested, so if you use please let me know if it works.

-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

Imports System.Net.Mail


Public Sub SendEmail()

    Dim client As New SmtpClient()
    Dim sendTo As New MailAddress("sendToAccount@gmail.com")
    Dim from As MailAddress = New MailAddress("from@address.com")
    Dim message As New MailMessage(from,sendTo)

    message.IsBodyHtml = True
    message.Subject = "Test SMTP"
    message.Body = "Got it!!"

    ' Use the same account in app.config to authenticate.
    Dim basicAuthenticationInfo As New System.Net.NetworkCredential("yourAccount@xxx.com", "YourPassword")


    client.Host = "smtp.xxx.com"
    client.UseDefaultCredentials = False
    client.Credentials = basicAuthenticationInfo

    '' For smtp config
    '' google use EnableSsl=True and Port = 587

    client.EnableSsl = False
    client.Port = 25

    Try

        client.Send(message)
        Console.WriteLine("SUCCESS")

    Catch ex As Exception

        Console.WriteLine("SEND FAIL")

    End Try

End Sub