VB.NET Feedback page problem?

dasmin5

Member
Joined
Nov 20, 2004
Messages
8
Location
O Town
Programming Experience
Beginner
I am trying to use my gmail account for sendmail. I have a feedback.aspx page in VB.NET. The email sends to my gmail account fine. But it is not sending what I typed in the comment box and it keeps having the persons email address and keeps saying "no subject".

Now my question is how to I fix this code properly to send the comments?

Here's what I have with a few changes:
VB.NET:
Imports System.Net.Mail

Partial Class Feeback
    Inherits System.Web.UI.Page

    Protected Sub CustomValidator1_ServerValidate(ByVal source As Object, ByVal args As System.Web.UI.WebControls.ServerValidateEventArgs) Handles CustomValidator1.ServerValidate
        If txtComments.Text.Length > 10 Then
            args.IsValid = False
        Else
            args.IsValid = True


        End If
    End Sub

    Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
        SendMail(txtEmail.Text, txtComments.Text)
    End Sub
    Private Sub SendMail(ByVal from As String, ByVal body As String)

        Dim SMTPServer As New SmtpClient("ASPMX.L.GOOGLE.COM")
        Dim mailServerName As String = ("ASPMX.L.GOOGLE.COM")
        Dim message As MailMessage = New MailMessage(from, "xxxxx@gmail.com")
        Dim mailClient As SmtpClient = New SmtpClient
        Dim MailMessage As New MailMessage()

        SMTPServer.Port = 25
        SMTPServer.Credentials = New System.Net.NetworkCredential("xxx@gmail.com", " xxxxxxxxxx")
        SMTPServer.EnableSsl = True

        MailMessage.To.Add("xxx@gmail.com")

        MailMessage.Subject = "GMail Test"
        MailMessage.Body = "This is the test text for Gmail email"

        mailClient.Host = mailServerName
        mailClient.Send(message)
        message.Dispose()

    End Sub
End Class
 
Last edited by a moderator:
you've instantiated two MailMessage variables, one name message and another named MailMessage. It should work if you do away with the one named MailMessage and change the appropriate lines to the following:
VB.NET:
message.To.Add("xxx@gmail.com")
message.Subject = "GMail Test"
message.Body = "This is the test text for Gmail email"
 
Trying to still get the right outcome?

It is sending the email to my inbox just fine. It it just not sending the comment that I type in to the box on the comments and ratings part.
I removed the "Dim MailMessage As New MailMessage()"

And that works but, now it is just sending this part:
MailMessage.To.Add("xxx@gmail.com")
MailMessage.Subject = "GMail Test"
MailMessage.Body = "This is the test text for Gmail email"

And it should be sending the txtComments to my email.

That is what "SendMail(txtEmail.Text, txtComments.Text)" in my code is for? Right?

I thank you truly for your help.
 
And that works but, now it is just sending this part:
MailMessage.To.Add("xxx@gmail.com")
MailMessage.Subject = "GMail Test"
MailMessage.Body = "This is the test text for Gmail email"
I'm not sure what that means.
Have you noticed that you haven't used the 'body' parameter that you are passing to the SendMail procedure? I assumed you were omitting that part for test purposes.
Did you change the three lines above to the three lines I suggested (change MailMessage to message)?
Now replace the test text with the 'body' parameter (and also utilize the 'from' parameter).
VB.NET:
message.To.Add("xxx@gmail.com")
message.From.... 'fill in the correct 
message.Subject = "GMail Test" 'probably change this too!
message.Body = body
 
Paszt is right - you need to declare what you want to send in the body.

You have mailmessage.body = "This is the text text for Gmail email"

If you change that to;

dim strBody as string = "This is the test text for Gmail email" & vbcrlf & me.txtComments.text"
mailMessage.body = strBody

that will work. You can set the body to be HTML as well, so you can use bold, italic, breaks etc.

To do this, add the line

mailMessage.IsBodyHTML = True

before .body = strBody

you can now edit your body string so that it can look like

strBody = "[b ]This is bold![/b ][br][br]" & txtComments.text & "[br][br][i ]This is italic![/i ]"

^^ I've had to add a space in the [ ] for bold and italic else it formats them on this post! When you put it on your form you just remove the spaces.
 
I've just looked at your code, and you've declared "body" and then set it to txtComments.text but you haven't used it!!
VB.NET:
Protected Sub Wizard1_FinishButtonClick(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.WizardNavigationEventArgs) Handles Wizard1.FinishButtonClick
        SendMail(txtEmail.Text, txtComments.Text)
    End Sub
    Private Sub SendMail(ByVal from As String, ByVal body As String)

        Dim mailClient As New Net.Mail.SmtpClient("ASPMX.L.GOOGLE.COM")
        mailClient.Port = 25
        mailClient.Credentials = New System.Net.NetworkCredential("xxx@gmail.com", " xxxxxxxxxx")
        mailClient.EnableSsl = True


        Dim message As New MailMessage(from, "xxxxx@gmail.com") [COLOR="SeaGreen"]'THIS IS CORRECT BECAUSE YOU HAVE SET "from"[/COLOR]
        message.To.Add("xxx@gmail.com")
        message.Subject = "GMail Test"
        [COLOR="seagreen"]'MailMessage.Body = "This is the test text for Gmail email" THIS IS WRONG BECAUSE YOU'RE NOT USING YOUR "body" YOU SET!!!!![/COLOR]
       [COLOR="Blue"] MailMessage.Body = "This is the test text for Gmail email" & vbcrlf & vbcrlf & body[/COLOR]

        mailClient.Send(message)
        message.Dispose()
    End Sub

I've edited the above so it should work
 
Isn't that pretty much what I said?:confused:

VB.NET:
[COLOR="Blue"] MailMessage.Body = "This is the test text for Gmail email" & vbcrlf & vbcrlf & body[/COLOR]

I've edited the above so it should work
This line will give you the "Reference to a non-shared member requires an object reference" error. Remember, the variable name is message.


I overlooked the fact that the variable message was instantiated using the from parameter so ignore the parts where I mentioned the from parameter.
 
you can now edit your body string so that it can look like

strBody = "[b ]This is bold![/b ][br][br]" & txtComments.text & "[br][br][i ]This is italic![/i ]"
the markup you've shown is typical forum markup as used in this forum. You would need to use HTML markup instead:
VB.NET:
strBody = "<strong>This is bold!</strong><br /><br />" & txtComments.text
 
Back
Top