vb asp.net联系表格

时间:2014-04-14 16:25:00

标签: asp.net vb.net email

已经创建了一个简单的联系表单,可以在客户的网站上使用,并且可以收到电子邮件(我暂时使用我的Gmail帐户),但是当它被发送时,无论我是什么,都显示它来自我放入电子邮件字段。请帮忙!可能是完全无知的东西,但请帮忙!我希望电子邮件来自txtEmail.text字段。我需要在web.config或GoDaddy方面添加一些内容吗?谢谢。以下是我的代码。我知道我需要包括try catch并清除字段,但是在我开始工作后会出现这种情况!是的,我确实包括了我的真实证书。

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("lchevy5@gmail.com")


    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

2 个答案:

答案 0 :(得分:1)

您的代码看起来不错,但您是通过Gmail发送的。他们的SMTP服务器可能会重写FROM字段,以便它与您用于身份验证的电子邮件地址匹配。

您可能希望看到this question

或者,您可以通过不执行此操作的SMTP服务器发送邮件。

答案 1 :(得分:0)

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("Example@gmail.com")
        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("Example@gmail.com", "Password")
        SMTP.Send(MyMessage)
        MessageBox.Show("Success")
    Catch ex As Exception
        MessageBox.Show("failed To Send")
    End Try
End Sub

尝试使用此代码发送指定邮件ID的电子邮件