|
-
Sep 29th, 2014, 04:23 AM
#1
Thread Starter
Junior Member
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
-
Sep 29th, 2014, 08:19 AM
#2
Re: SMTP errors I can't fix
What kind of exception does it throw?
-
Sep 29th, 2014, 08:38 AM
#3
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
-
Sep 29th, 2014, 05:32 PM
#4
Thread Starter
Junior Member
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
-
Sep 29th, 2014, 07:08 PM
#5
Re: SMTP errors I can't fix
 Originally Posted by pcesarano
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
-
Sep 29th, 2014, 09:50 PM
#6
Thread Starter
Junior Member
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|