Results 1 to 6 of 6

Thread: SMTP errors I can't fix

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    19

    SMTP errors I can't fix

    Hi all. I'm not a beginner in VB .NET, but I'm writing my first Web application and something is not working properly. The entire code is pasted below, but to save time the relevant portions (I believe) are thus:

    Dim smtpServer As String = "smtp.gmail.com"
    .
    .
    Dim [txtBody, From_recipient1, To_recipient2, subject1] As String = [some text for each, this is my shorthand for 4 separate lines of code]
    txtBody = [some text, not important]
    .
    .
    .
    Dim message1 As New MailMessage(From_recipient1, To_recipient2, subject1, txtBody)
    message1.From = New MailAddress(recipient1)
    message1.To.Add(recipient1) 'copy email to sender
    message1.To.Add(recipient2) 'send email to recipient
    message1.Subject = msg1 'msg1 defined earlier, not shown and not relevant
    .
    .
    [THIS IS THE PART THAT THROWS AN EXCEPTION (BELOW). PARTICULARLY THE LAST LINE AND I CAN'T FIGURE OUT WHY...PLEASE HELP!!]
    .
    .
    Dim SMTP As New SmtpClient(smtpServer)
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential("thirddcahs", "thirddcahs_pswd")
    SMTP.Port = 465
    SMTP.Send(message1)

    [FULL CODE:]

    Imports System.Net.Mail

    Partial Class Registration_Form
    Inherits System.Web.UI.Page

    Dim smtpServer As String = "smtp.gmail.com"
    Dim recipient1 As String = "[email protected]"
    Dim recipient2 As String = "[email protected]"
    Dim recipient3 As String
    Dim subject1 As String = "Membership Request"
    Dim subject2 As String = "Membership Subscription Receipt"
    Dim msg1 As String = "Enrolling Member Information: "
    Dim txtBody As String = ""
    Protected Sub btnSubmit_Click(sender As Object, e As EventArgs) Handles btnSubmit.Click
    msgStat.Text = "Processing..."
    'Try
    Dim message1 As New MailMessage(From_recipient1, To_recipient2, subject1, txtBody)

    message1.From = New MailAddress(recipient1)
    message1.To.Add(recipient1)
    message1.To.Add(recipient2)
    message1.Subject = msg1

    txtBody = "First Name: " & First_Name.Text & vbCrLf & "Last Name: " & Last_Name.Text & vbCrLf
    If MI.Text <> "" Then
    txtBody += "MI: " & MI.Text & vbCrLf
    End If
    If Organization.Text <> "" Then
    txtBody += "Organization: " & Organization.Text & vbCrLf
    End If
    If Position.Text <> "" Then
    txtBody += "Position: " & Position.Text & vbCrLf
    End If
    txtBody += "Address: " & Address.Text & vbCrLf
    If Address_Cont.Text <> "" Then
    txtBody += "Address (cont.): " & Address_Cont.Text & vbCrLf
    End If
    txtBody += "City: " & City.Text & vbCrLf & "State: " & State.Text & vbCrLf & "Zip: " & Zip.Text & vbCrLf
    If Phone.Text <> "" Then
    txtBody += "Phone: " & Phone.Text & vbCrLf
    End If
    If Fax.Text <> "" Then
    txtBody += "Fax: " & Fax.Text & vbCrLf
    End If
    txtBody += "Email: " & Email.Text & vbCrLf

    message1.Body = txtBody

    Dim SMTP As New SmtpClient(smtpServer)
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential("thirddcahs", "thirddcahs_pswd")
    SMTP.Port = 465
    SMTP.Send(message1)

    msgStat.Text = "Message Sent Successfully!" & vbCrLf & "You will receive an email receipt shortly. Thank you."
    'Catch ex As Exception
    'msgStat.Text = "There was an unexpected problem. Please contact us at [email protected]." & vbCrLf & "Error: " & ex.ToString()
    'End Try
    End Sub
    End Class

  2. #2
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: SMTP errors I can't fix

    What kind of exception does it throw?

  3. #3
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: SMTP errors I can't fix

    Sometimes I wonder... why people think that the error message isn't important. No one walks into the Doc's office and says "It hurts" and expects the doc to know instantly what the issue is. At least we got code, and the line the error happens on.

    @pcesarano - a couple things 1) kudos for posting the code and the line the error happens one. Usually we need three things code - error - and line... we typically get two of the three, usually it's the code and the error. 2) please when you post code use the [code][/code] or [highlight][/highlight] tags around your code. They will preserve the formatting. you can also optionally include the language with the highlight tag ([highlight=vb.net] for example) to get pretty formatting with colors. Which ever option you use, the rest of us would appreciate it.

    Thanks.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    19

    Re: SMTP errors I can't fix

    Gotcha, I'm sorry. The line throwing the exception is "SMTP.Send(message1)", which is the last line of the short block pasted below (it's line 54, in red). After the code block I placed a link with two images--one of the Visual Studio error message (and line 54 highlighted) and one with the error message which appears on the aspx webpage when the exception is thrown. I hope this helps.

    Dim SMTP As New SmtpClient(smtpServer)
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential("thirddcahs", "thirddcahs_pswd")
    SMTP.Port = 465
    SMTP.Send(message1)

    EXCEPTION IMAGES (x2); FOLLOW LINK

    http://www.thirddcahistoricalsociety.org/Exception.html

  5. #5
    Frenzied Member
    Join Date
    Oct 2012
    Location
    Tampa, FL
    Posts
    1,187

    Re: SMTP errors I can't fix

    Quote Originally Posted by pcesarano View Post
    Gotcha, I'm sorry. The line throwing the exception is "SMTP.Send(message1)", which is the last line of the short block pasted below (it's line 54, in red). After the code block I placed a link with two images--one of the Visual Studio error message (and line 54 highlighted) and one with the error message which appears on the aspx webpage when the exception is thrown. I hope this helps.

    Dim SMTP As New SmtpClient(smtpServer)
    SMTP.EnableSsl = True
    SMTP.Credentials = New System.Net.NetworkCredential("thirddcahs", "thirddcahs_pswd")
    SMTP.Port = 465
    SMTP.Send(message1)

    EXCEPTION IMAGES (x2); FOLLOW LINK

    http://www.thirddcahistoricalsociety.org/Exception.html
    Well, the code below works for me (which is what your code looks like):

    Code:
            Try
                Dim smtpServer As New SmtpClient("smtp.gmail.com", 587)
                smtpServer.EnableSsl = True
                smtpServer.Credentials = New Net.NetworkCredential("[email protected]", "jpass")
                Dim message As New MailMessage("[email protected]", "[email protected]", "TEST SUBJECT", "TEST BODY")
                smtpServer.Send(message)
            Catch ex As Exception
                MessageBox.Show("Error Occured: " & ex.ToString)
            End Try
    But, the code only works on the condition that I have turned off two-step verification for my account. If you have two-step verification enabled, this is probably your issue. Try taking a look at this:

    https://support.google.com/mail/answer/1173270?hl=en

  6. #6

    Thread Starter
    Junior Member
    Join Date
    Aug 2011
    Posts
    19

    Re: SMTP errors I can't fix

    YOU ARE AWESOME! It's working now...thank you so much for your help, you're a lifesaver.

    Patrick

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