在Visual Studio 2012中使用SMTP

本文关键字:SMTP 2012 Visual Studio | 更新日期: 2023-09-27 18:00:05

我在使用SMTP发送电子邮件时遇到问题,它说"等待本地主机",然后最终超时。

我的假设是SMTP在本地主机上被禁用,所以我看了一下这里的说明:http://msdn.microsoft.com/en-us/library/8b83ac7t%28v=vs.100%29.aspx但这些步骤都不适用于我的电脑(Windows 8,asp.net 4,VS 2012)

这是默认启用的吗?问题出在我的代码上?这是我的代码

protected void SendMail()
{
    MailMessage email = new MailMessage();
    email.To.Add(new MailAddress("removed"));
    email.Subject = "Contact Form";
    email.From = new MailAddress(emailTextbox.Text);
    email.Body = "From: " + nameTextbox.Text + "<br />";
    email.Body = "Message: " + commentTextbox.Text + "<br />"; 
    SmtpClient smtp = new SmtpClient();
    smtp.Host = "smtp.gmail.com";
    smtp.Port = 465;
    smtp.Credentials = new NetworkCredential("removed", "removed");
    smtp.EnableSsl = true;
    smtp.Send(email);
}
protected void Button1_Click(object sender, EventArgs e)
{
    try
    {
        SendMail();
        messageLabel.Text = "Thanks for your email, we will get back to you shortly.";
    }
    catch (Exception error) {
        messageLabel.Text = "Error " + error;
    }
}

感谢您的帮助!

在Visual Studio 2012中使用SMTP

请查看添加以下配置是否有帮助:

 smtp.UseDefaultCredentials = false;
 smtp.DeliveryMethod = SmtpDeliveryMethod.Network;