asp.net mailmessage的配置

时间:2010-12-05 00:29:47

标签: asp.net

有人可以帮助我吗?我准备部署我的Web应用程序了。我有一个联系表单,我希望用户给我发消息。我点击提交时创建了smtp,我得到的错误信息无法发送。  该应用程序仍然在我的本地机器上也许这就是为什么。但我只是想知道这段代码是否适合我的表单:

Imports System.Net.Mail


Partial Class contact
    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 > 300 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 mailServerName As String = "SMTP.relay-hosting.secureserver.net"
        Dim message As MailMessage = New MailMessage(from, "collins@collinsegbe.com", "feedback", body)
        Dim mailClient As SmtpClient = New SmtpClient

        mailClient.Host = mailServerName
        mailClient.Send(message)
        message.Dispose()
    End Sub
End Class

此代码行显示错误:mailClient.Send(message) 我将感谢任何人的帮助

2 个答案:

答案 0 :(得分:1)

protected void Button9_Click(object sender, EventArgs e)
{
    {
        var fromAddress = new MailAddress("rusty109.stud@gmail.com");
        var toAddress = new MailAddress(TextBox13.Text);
        const string fromPassword = "commando1";
        string subject = TextBox14.Text;
        string body = TextBox12.Text;

        var smtp = new SmtpClient
        {
            Host = "smtp.gmail.com",
            Port = 587,
            EnableSsl = true,
            DeliveryMethod = SmtpDeliveryMethod.Network,
            UseDefaultCredentials = false,
            Credentials = new NetworkCredential(fromAddress.Address, fromPassword),
            Timeout = 20000
        };
        using (var message = new MailMessage(fromAddress, toAddress)
        {
            Subject = subject,
            Body = body
        })
        {
            smtp.Send(message);
        }
    }
}

我的旧代码,当我想要发送电子邮件时,如果您愿意,可以使用gmail帐户,其中没有任何内容...设置为使用此代码:) 使用System.Net.Mail; 使用System.Net;

答案 1 :(得分:0)

您需要通过将SmtpClient的{​​{1}}属性设置为Credential类的实例来添加SMTP服务器的用户名和密码。