Results 1 to 5 of 5

Thread: Value of type 'String' cannot be converted to 'MailMessage'.

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    5

    Post 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

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,929

    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:
    Code:
     SMTP.Send(emails)
    ...it should have been:
    Code:
     SMTP.Send(email)

  3. #3
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,116

    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

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

    Re: Value of type 'String' cannot be converted to 'MailMessage'.

    Quote Originally Posted by OptionBase1 View Post
    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.

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    5

    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
  •  



Click Here to Expand Forum to Full Width