troubleshooting Question Step Into Error when emailing

Avatar of Murray Brown
Murray BrownFlag for United Kingdom of Great Britain and Northern Ireland asked on
5 Comments1 Solution18 ViewsLast Modified:
I am running the following code to test why emails are not going through. When I step into the line "     mSmtpClient.Send(mMailMessage)" I get the error shown in the image

   Protected Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click

        Dim oEmailResult As String = oSendMailHere("", "", "", "", "New Test Email Header", "This is a test email", True)
        If oEmailResult = "Email Success!" Then
            Me.Label11.Text = "Email was sent"
            Me.Label11.Text = "##NOT SENT##"
        End If

    End Sub
    Function oSendMailHere(ByVal from As String, ByVal recepient As String, ByVal bcc As String,
                                 ByVal cc As String, ByVal subject As String, ByVal body As String,
                                 ByVal blnUseHTML As Boolean) As String

        Dim oException As String
        Dim oInnerException As String
        Dim oStackTrace As String
        Dim oError As String
        Me.lblEmailError.Text = ""


            ' Instantiate a new instance of MailMessage
            Dim mMailMessage As New MailMessage()

            ' Set the sender address of the mail message
            mMailMessage.From = New MailAddress(from)
            ' Set the recepient address of the mail message
            mMailMessage.To.Add(New MailAddress(recepient))

            ' Check if the bcc value is nothing or an empty string
            If Not bcc Is Nothing And bcc <> String.Empty Then
                ' Set the Bcc address of the mail message
                mMailMessage.Bcc.Add(New MailAddress(bcc))
            End If

            ' Check if the cc value is nothing or an empty value
            If Not cc Is Nothing And cc <> String.Empty Then
                ' Set the CC address of the mail message
                mMailMessage.CC.Add(New MailAddress(cc))
            End If

            ' Set the subject of the mail message
            mMailMessage.Subject = subject
            ' Set the body of the mail message
            mMailMessage.Body = body

            ' Set the format of the mail message body as HTML
            If blnUseHTML = True Then
                mMailMessage.IsBodyHtml = True
                mMailMessage.IsBodyHtml = False
            End If

            ' Set the priority of the mail message to normal
            mMailMessage.Priority = MailPriority.Normal

            ' Instantiate a new instance of SmtpClient
            Dim mSmtpClient As New SmtpClient()
            ' Send the mail message
            oSendMailHere = "Email Success!"
        Catch ex As Exception

            oException = ex.Message
            oInnerException = ex.InnerException.ToString
            oStackTrace = ex.StackTrace
            oError = "Email Not Sent! " & oException & " Inner Exception " & oInnerException & " Stack Trace " & oStackTrace
            Me.lblEmailError.Text = oError

            oSendMailHere = "Email Not Sent! " & oError

        End Try
    End Function

Chinmay Patel
Chief Technology Ninja
Join our community to see this answer!
Unlock 1 Answer and 5 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 5 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros