通过gmail帐户在ASP.NET 3.5(VS.NET 2008)中发送邮件时遇到错误

时间:2015-04-01 19:32:21

标签: c# asp.net email gmail

我尝试过各种程序员提供的各种有用的代码。我发现1易于使用,并试图设计一个联系表格。它在VS.NET 2008中部署时工作正常。但是当我将它发布到Web服务器上时,它显示“Timed Out Error”。

我还在托管服务器(GoDaddy)中将信任级别设置为完整。 另外,在web.config文件中尝试了这个。 仍然没有工作。

以下是代码:

protected void SendMail()
{
    string fromAddress = "abc@gmail.com";// Gmail Address from where you send the mail
    string toAddress = "xyz@gmail.com";
    const string fromPassword = "password";//Password of your gmail address
    string subject = "Enquiry from Website Contact Form.";
    string body = "From: " + txtName.Text + "\n";
    body += "Email: " + txtEmail.Text + "\n";
    body += "Contact: " + txtPhone.Text + "\n";
    body += "Message: \n" + txtMessage.Text + "\n";
    lblMsgSend.Visible = true;
    try
    {
        var smtp = new System.Net.Mail.SmtpClient();
        {
            smtp.Host = "smtp.gmail.com";
            smtp.Port = 587;
            smtp.EnableSsl = true;
            smtp.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;
            smtp.Credentials = new NetworkCredential(fromAddress, fromPassword);
            smtp.Timeout = 20000;
        }
        smtp.Send(fromAddress, toAddress, subject, body);
        Response.Write("Message sent successfully.");
        lblMsgSend.Text = "Message sent successfully.";
    }
    catch (System.Net.Mail.SmtpException se)
    {
        lblMsgSend.Text = "Message sending failed. Details : " + se.Message.ToString() + se.StackTrace.ToString();
    }
    catch (Exception ex)
    {
        lblMsgSend.Text = "Message sending failed. Details : " + ex.Message.ToString() + ex.StackTrace.ToString();
    }
}

0 个答案:

没有答案