|
-
Nov 25th, 2010, 12:42 AM
#1
Thread Starter
Hyperactive Member
[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.....
-
Nov 25th, 2010, 12:58 AM
#2
Re: Threading and Sending Mail
 Originally Posted by dr_aybyd
But with threading the message was sent successfully.
Did you actually mean "without"?
-
Nov 25th, 2010, 01:03 AM
#3
Thread Starter
Hyperactive Member
Re: Threading and Sending Mail
 Originally Posted by jmcilhinney
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.....
-
Nov 25th, 2010, 01:14 AM
#4
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.
-
Nov 25th, 2010, 01:18 AM
#5
Thread Starter
Hyperactive Member
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.....
-
Nov 25th, 2010, 02:44 AM
#6
Thread Starter
Hyperactive Member
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.....
-
Nov 25th, 2010, 04:17 AM
#7
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.
-
Nov 25th, 2010, 08:46 PM
#8
Thread Starter
Hyperactive Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|