-
Aug 3rd, 2020, 12:52 PM
#1
Thread Starter
New Member
Value of type 'String' cannot be converted to 'MailMessage'.
Hi all
I can't solve this problem: Value of type 'String' cannot be converted to 'MailMessage'.
Here is my code:
Code:
Sub send_mail()
Dim email As New MailMessage()
Dim strBody As String = ""
Dim emails As String = ""
Try
Dim SMTP As New SmtpClient("mail.xxx.xo")
SMTP.UseDefaultCredentials = True
SMTP.Credentials = New System.Net.NetworkCredential("xxxxx@aaaaa.xo", "xxxxxx")
email.From = New MailAddress("xxxxx@aaaaa.xo")
If String.IsNullOrEmpty(emails) Then
emails = "zzz.aaa@xdert.io"
Else
emails = TextBox4.Text
End If
emails = TextBox4.Text
email.To.Add(emails)
email.Subject = TextBox2.Text
strBody = TextBox1.Text & TextBox5.Text & TextBox6.Text & TextBox7.Text & TextBox8.Text
strBody = "bla bla bla" & vbCrLf
strBody += "bla "
strBody += TextBox1.Text & vbCrLf
strBody += TextBox2.Text & vbCrLf
strBody += TextBox5.Text & vbCrLf
strBody += TextBox6.Text & vbCrLf
strBody += TextBox7.Text & vbCrLf
strBody += TextBox8.Text & vbCrLf
email.Body = strBody
SMTP.Send(emails)
MsgBox("mail send")
Catch ex As SmtpException
MsgBox(ex.StatusCode & vbCrLf & ex.Message & ex.ToString, vbCritical, "SMTP Error!")
End Try
End Sub
When I compile, here is my problem : SMTP.Send(emails)
Last edited by si_the_geek; Aug 3rd, 2020 at 01:03 PM.
Reason: added Code tags
-
Aug 3rd, 2020, 01:03 PM
#2
Re: Value of type 'String' cannot be converted to 'MailMessage'.
That is because you have a typo... and the fact you have two variables with similar names means it wasn't obvious.
These are the relevant variables:
Code:
Dim email As New MailMessage()
Dim emails As String = ""
You were accidentally using the wrong one. Instead of this:
...it should have been:
-
Aug 3rd, 2020, 01:04 PM
#3
Re: Value of type 'String' cannot be converted to 'MailMessage'.
This is a perfect example of why you should be using descriptive and easily distinguishable variable names.
emails is defined as a string variable (one that shouldn't even need to exist based on your code, by the way)
email is defined as a MailMessage
-
Aug 3rd, 2020, 08:13 PM
#4
Re: Value of type 'String' cannot be converted to 'MailMessage'.
Originally Posted by OptionBase1
This is a perfect example of why you should be using descriptive and easily distinguishable variable names.
emails is defined as a string variable (one that shouldn't even need to exist based on your code, by the way)
email is defined as a MailMessage
Couldn't agree more. If email refers to a MailMessage object then the only sensible use for emails would be for an array or collection of MailMessage objects. If that emails variable is actually a String containing email addresses then, at the very least, it should be emailAddresses. Other options would be things like recipients.
-
Aug 4th, 2020, 01:08 AM
#5
Thread Starter
New Member
Re: Value of type 'String' cannot be converted to 'MailMessage'.
Stupid mistake
Thank you for all your help!
Tags for this Thread
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
|