2012-10-04 82 views
2
protected void Button1_Click(object sender, EventArgs e) 
{ 
    try 
    { 
     SmtpClient sm = new SmtpClient(); 
     MailMessage ms = new MailMessage(); 
     ms.To.Add(new MailAddress(TextBox1 .Text)); 
     ms.Subject = TextBox2.Text; 
     ms.Body = TextBox3.Text; 
     ms.IsBodyHtml = true; 
     sm.Send(ms); 
    } 
    catch (Exception el) 
    { 
     Response.Write(el.Message); 
    } 
} 
+0

有完整源代码的任何解决方案? – Kiquenet

回答

1

SmtpClient从web.config获取其配置,即SMTP服务器地址和验证字段(如果需要)。在发送行上放置一个断点,检查sm对象的设置,确保SMTP参数正确,并且无论您在测试此代码的哪个位置,都可以点击服务器。

0

您需要设置的连接设置(例如对于Gmail):

SmtpClient sm = new SmtpClient("smtp.gmail.com", 587); 
sm.Credentials = new System.Net.NetworkCredential("[email protected]", "password"); 
sm.EnableSsl = true; 
0

当你不能够连接到SMTP服务器,因此超时消息出现此问题。为什么有这个问题已经出现了几种可能性:

  • 错误SMTP地址
  • SMTP拒绝
  • SMTP服务器脱机
  • 端口设置
  • SSL配置
0

请输入此在web.config文件中

<system.net> 
      <mailSettings> 
      <smtp from="[email protected]"> 
      <network host="smtp.gmail.com" port="587" userName="[email protected]" password="***** " defaultCredentials="false"/> 
       </smtp> 
      </mailSettings> 
     </system.net> 
相关问题