1

我有一个情况。HTML 代码是标准的,带有三个文本框和一个按钮:

<add key="cma_contact_form_email" value="somec@gmail.com"/>
<add key="cma_contact_to_address" value="some@gmail.com"/>
<add key="smtpServer" value="smtp.gmail.com" />
<add key="EnableSsl" value = "true"/>
<add key="smtpPort" value="993" />
<add key="smtpUser" value="some@gmail.com" />
<add key="smtpPass" value="pass" />

后面的代码:

protected void ImageButton_Click(Object sender, EventArgs e){
        MailMessage msg = new MailMessage();
        msg.To.Add(ConfigurationManager.AppSettings["cma_contact_form_email"]);
        msg.From = new        MailAddress(ConfigurationManager.AppSettings["cma_contact_to_address"]);

        msg.Body += "Name: " + txtName.Text + "\n";
        msg.Body += "Email: " + txtEmail.Text + "\n";
        msg.Body += "Message: \n" + txtMessage.Text + "\n";
        msg.Subject = txtName.Text;

        msg.IsBodyHtml = true;
        SmtpClient smtp = new SmtpClient();
        smtp.Host = ConfigurationManager.AppSettings["smtpServer"]; //Or Your SMTP Server Address
        smtp.Port = Convert.ToInt32(ConfigurationManager.AppSettings["smtpPort"]);

        smtp.EnableSsl = Convert.ToBoolean(ConfigurationManager.AppSettings["EnableSsl"]);
        smtp.Credentials = new System.Net.NetworkCredential(ConfigurationManager.AppSettings["smtpUser"], ConfigurationManager.AppSettings["smtpPass"]);
        //Or your Smtp Email ID and Password
        try
        {
            smtp.Send(msg);
            lblPost.Text = "Thank you, your question has been submitted to our CMA heldesk.";
        }
        catch (Exception ex)
        {

            lblPost.Text = "Error occured while sending your message. "  + ex.Message;
        }

        placeholder.Visible = false;
        msgplaceholder.Visible = true;
    }

通常这应该可以工作,但我得到一个超时错误。有谁知道钓点在哪里?谢谢!

4

2 回答 2

2

您可以在实际发送到电子邮件之前使用以下方法检查是否允许连接,并且您还应该处理SmtpException。它有一个StatusCode属性,它将告诉您 Send() 失败的原因。

你可以像这样调用下面的方法

if(TestConnection("smtpServerAddress",port))
  SendEmail();

public static bool TestConnection(string smtpServerAddress, int port)
    {
        IPHostEntry hostEntry = Dns.GetHostEntry(smtpServerAddress);
        IPEndPoint endPoint = new IPEndPoint(hostEntry.AddressList[0], port);
        using (Socket tcpSocket = new Socket(endPoint.AddressFamily, SocketType.Stream, ProtocolType.Tcp))
        {
            //try to connect and test the rsponse for code 220 = success
            tcpSocket.Connect(endPoint);
            if (!CheckResponse(tcpSocket, 220))
            {
                return false;
            }

            // send HELO and test the response for code 250 = proper response
            SendData(tcpSocket, string.Format("HELO {0}\r\n", Dns.GetHostName()));
            if (!CheckResponse(tcpSocket, 250))
            {
                return false;
            }

            // if we got here it's that we can connect to the smtp server
            return true;
        }
    }

  private static bool CheckResponse(Socket socket, int expectedCode)
{
    while (socket.Available == 0)
    {
        System.Threading.Thread.Sleep(100);
    }
    byte[] responseArray = new byte[1024];
    socket.Receive(responseArray, 0, socket.Available, SocketFlags.None);
    string responseData = Encoding.ASCII.GetString(responseArray);
    int responseCode = Convert.ToInt32(responseData.Substring(0, 3));
    if (responseCode == expectedCode)
    {
        return true;
    }
    return false;
}

邮件设置

  • Gmail SMTP 服务器地址:smtp.gmail.com
  • Gmail SMTP 用户名:example@gmail.com
  • Gmail SMTP 密码:密码
  • Gmail SMTP 端口:465
  • 需要 Gmail SMTP TLS/SSL:是
于 2013-08-07T19:14:58.257 回答
1

问题:您在设置中提到的端口是用于 IMAP,

解决方案:GMAIL SMTP的正确端口是端口 465而不是IMAP的端口 993

于 2013-08-07T19:15:11.593 回答