Lolo
Lolo

Reputation: 59

vb asp.net contact form

Have created a simple contact form for use on client's website and can get the email (i'm using my gmail account temporarily) but when it is sent it shows it is from me no matter what I put in the email field. Please help! Probably something completely ignorant but please help! I want the email to be derived from the txtEmail.text field. Is there something I need to add to my web.config or on GoDaddy side of things? Thanks. Below is my code. I am aware I need to include try catch and clear the fields, but that will come after I get this to work!! And yes I did include my real credentials.

Imports System.Net.Mail.MailAddress
Imports System.Net.Mail.MailMessage
Imports System.Net.NetworkCredential
Imports System.Net.Mail



Partial Class Default2

Inherits System.Web.UI.Page

Protected Sub btnSubmit_Click1(ByVal sender As Object, ByVal e As System.EventArgs)   Handles          btnSubmit.Click



    Dim Msg As New MailMessage()

    ' Sender e-mail address.

    Msg.From = New MailAddress(txtEmail.Text)


    ' Recipient e-mail address.

    Msg.To.Add("[email protected]")


    Msg.Subject = txtSubject.Text

    Msg.Body = "Sent From:" & txtName.Text + Environment.NewLine + "Email:" & txtEmail.Text +    Environment.NewLine + txtMessage.Text

    ' your remote SMTP server IP.

    Dim smtp As New SmtpClient()

    smtp.Host = "smtp.gmail.com"

    smtp.Port = 25

    smtp.Credentials = New System.Net.NetworkCredential("user", "pass")

    smtp.EnableSsl = True

    smtp.Send(Msg)

    'Msg = null;
    lbltexts.Visible = True




End Sub



End Class

Upvotes: 0

Views: 469

Answers (2)

Prakash
Prakash

Reputation: 100

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles 
Button2.Click
    Dim MyMessage As New MailMessage

    Try
        MyMessage.From = New MailAddress(TextBox2.Text)
        MyMessage.To.Add("[email protected]")
        MyMessage.Subject = TextBox3.Text + body
        MyMessage.Body = TextBox4.Text
        Dim SMTP As New SmtpClient("smtp.gmail.com")
        SMTP.Port = 587
        SMTP.EnableSsl = True
        SMTP.Credentials = New System.Net.NetworkCredential("[email protected]", "Password")
        SMTP.Send(MyMessage)
        MessageBox.Show("Success")
    Catch ex As Exception
        MessageBox.Show("failed To Send")
    End Try
End Sub

Try this Code for Send Email For Your Specified Mail Id

Upvotes: 0

pmcoltrane
pmcoltrane

Reputation: 3112

Your code looks correct, but you're sending it through Gmail. Their SMTP server is likely rewriting the FROM field so that it matches the email address you're using for authentication.

You might want to see this question.

Alternatively, you can send your mail through an SMTP server that doesn't do this.

Upvotes: 1

Related Questions