Results 1 to 8 of 8

Thread: [RESOLVED] Threading and Sending Mail

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Resolved [RESOLVED] Threading and Sending Mail

    I am getting error when I am putting my sendmail function into other thread.
    But without threading the message was sent successfully.
    I am getting error in this part when sending using thread.

    mail.Body = EmailBody.DocumentText
    It say: Specified cast is not valid.

    Here is my code for sending email
    EmailBody is a webbrowser in Desing MOde ON
    Code:
    Private Sub SendMail()
            Try
                Dim SmtpServer As New SmtpClient()
                Dim mail As New MailMessage()
                SmtpServer.Credentials = New  _
                Net.NetworkCredential("username", "password")
                SmtpServer.Port = 587
                SmtpServer.EnableSsl = True
                SmtpServer.Host = "pod51003.outlook.com"
                mail = New MailMessage()
                mail.From = New MailAddress("email")
                mail.To.Add("toemail")
                mail.CC.Add("CCemail")
                mail.Subject = "Test Mail"
                mail.IsBodyHtml = True
                mail.Body = EmailBody.DocumentText
                SmtpServer.Send(mail)
                TSSLstatus.Text = "Email successfully sent...."
                Me.Cursor = Cursors.Arrow
                MsgBox("mail send")
            Catch ex As Exception
                MsgBox(ex.ToString)
            End Try
        End Sub
    And this is how I start the thread and call the sendmail

    Code:
    Private Sub btnSend_Click_1(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSend.Click
            TSSLstatus.Text = "Please wait, sending email..."
            Dim t As Thread
            t = New Thread(AddressOf Me.SendMail)
            t.Start()
    End Sub
    What I am missing for?
    I need to put in other thread so that my Form is active while sending big email.
    Last edited by dr_aybyd; Nov 25th, 2010 at 01:04 AM.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Threading and Sending Mail

    Quote Originally Posted by dr_aybyd View Post
    But with threading the message was sent successfully.
    Did you actually mean "without"?
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Threading and Sending Mail

    Quote Originally Posted by jmcilhinney View Post
    Did you actually mean "without"?
    Yeah I mean without..sorry for that..edited now.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: Threading and Sending Mail

    What is 'EmailBody'? Is it a WebBrowser by any chance? I'm guessing so from the 'DocumentText' property. If it is, or any other control for that matter, then that is your issue. You can't access controls directly on any thread other than the one on which they were created. You'd have to either pass the text in when you start the thread or else use delegation to get it when you need it.

    I've got you covered in both cases. For more information, follow the CodeBank link in my signature and look for submissions with "Thread" in the title. You'll find one on each topic.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Threading and Sending Mail

    yup thats a WebBrowser drop on my form.
    Thanks..
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: Threading and Sending Mail

    I got it work now... Thanks jm
    But, can you check my codes, is that ok?
    Code:
        Private Sub GetDocumentTextHTML()
            If Me.EmailBody.InvokeRequired Then
                Me.EmailBody.Invoke(New MethodInvoker(AddressOf GetDocumentTextHTML))
            Else
                body = Me.EmailBody.DocumentText
            End If
        End Sub
    Last edited by dr_aybyd; Nov 25th, 2010 at 03:18 AM.
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,222

    Re: [RESOLVED] Threading and Sending Mail

    That's the general pattern but the way you're doing it is a little bit dodgy. Instead of using what I assume is a member variable, make GetDocumentTextHTML a function that returns the DocumentText of the WebBrowser. Then you can just do this:
    Code:
    mail.Body = GetDocumentTextHTML()
    If you have used my CodeBank submission to get that pattern then check it out again because there's an example that shows how to return data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Nov 2005
    Posts
    310

    Re: [RESOLVED] Threading and Sending Mail

    Thanks jm...ok I will check again your code bank tut...
    VB 6.0 = "Self-Study" Then
    vb.NET = "Self-Study" Then
    C# = 'on going study.....

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