日期:2014-05-17  浏览次数:20477 次

发送邮件失败无法连接到远程服务器
如题,代码如下:
using System.Net.Mail;
try
  {
  MailAddress From = new MailAddress("123456@126.com");
  MailAddress To = new MailAddress(txtEmail.Text);
  string Subject = "感谢您的使用!";
  string Body = "请点击以下连接进行注册确认!<br><a href='http://localhost:1326/pingdgng/RegistorReturn.aspx?un=" + txtEmail.Text + "&up=" + pwd.Text + "'>点击此处</a>";

  // 设置邮件的发送及接收地址
  MailMessage mailObj = new MailMessage(From.Address, To.Address, Subject, Body);

  mailObj.Priority = MailPriority.High;


  //构建一个认证实例,这里是smtp服务器的地址
  SmtpClient smtp = new SmtpClient("smtp.163.com");
  switch (To.Host)
  {
  case "qq.com":
  smtp = new SmtpClient("qq.com");
  break;
  case "126.com":
  case "163.com":
  smtp = new SmtpClient("smtp.163.com");
  break;
  case "gmail":
  smtp = new SmtpClient("smtp.gmail.com");
  break;
  }

  //将认证实例赋予mailClient 这里是登陆smtp的用户名和密码
  System.Net.NetworkCredential nc = new System.Net.NetworkCredential("123456@126.com", "123456");
  smtp.Credentials = nc;
  smtp.Send(mailObj);
  Response.Write("发送成功!");
  }
  catch (Exception exp)
  {
  Response.Write(exp.Message);
  }

------解决方案--------------------
gmail的
SMTPServer.Port = 587
SMTPServer.EnableSsl = True

参考:
http://geekswithblogs.net/TakeNote/archive/2007/07/16/113974.aspx