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

ASP.NET在注册时邮箱验证代码
Random random = new Random();
  string checkCode = "";
  for (int i = 0; i <= 4; i++)
  {
  int num = random.Next(1, 10);
  checkCode += num.ToString();
  }
  string validataCode = FormsAuthentication.HashPasswordForStoringInConfigFile(checkCode, "md5");
  user.validateCode = validataCode;
  string strSmtpServer = "smtp.163.com";
  string strFrom = "";
  string strFromPass = ""
  string strto = user.email;
  string strSubject = "激活账号";
  StringBuilder strBody = new StringBuilder();
  strBody.Append("点击下面链接激活账号,24小时生效,否则重新注册账号,链接只能使用一次,请尽快激活!</br>");
  strBody.Append("<a href='http://localhost:2493/web/Operate.aspx?userName=" + user.userName + "&validateCode=" + user.validateCode + "'>点击这里</a></br>");
  strBody.Append("如未能激活请点击下面链接:<a href='http://localhost:2493/web/Operate.aspx?userName=" + user.userName + "&validateCode=" + user.validateCode + "'>http://localhost:2493/web/Operate.aspx?userName=" + user.userName + "&validateCode=" + user.validateCode + "</a></br>");
  SendSMTPEMail(strSmtpServer, strFrom, strFromPass, strto, strSubject, strBody.ToString());

 
  //发送邮件
  public void SendSMTPEMail(string strSmtpServer, string strFrom, string strFromPass, string strto, string strSubject, string strBody)
  {
  System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient(strSmtpServer);
  client.UseDefaultCredentials = false;
  client.Credentials =
  new System.Net.NetworkCredential(strFrom, strFromPass);
  client.DeliveryMethod = System.Net.Mail.SmtpDeliveryMethod.Network;

  System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage(strFrom, strto, strSubject, strBody);
  message.BodyEncoding = System.Text.Encoding.UTF8;
  message.IsBodyHtml = true;
  client.Send(message);
  }

代码运行是会报错:参数“from”不能是空字符串。
参数名: from

这是什么情况???

------解决方案--------------------
發送郵件的郵箱為空
------解决方案--------------------
163邮件服务器需要对发件人进行身份验证后才能发送,现在不是那个SMTP都能随便用的,加上可以163认可的发件人账号和密码才行。
------解决方案--------------------
如果选择了163的SMTP服务器,“smtp.163.com”,发件人必须是登录用户的163 email用户地址。