Error.String 引用未设置为字符串的实例。参数名称:s(在 MVC4 C# 中发送电子邮件进行帐户验证时)
Error.String reference not set to an instance of a String. Parameter name: s (While Sending Email For Account Verification in MVC4 C#)
我正在做一个 MVC 应用程序,并试图向注册用户发送一封电子邮件,用于他的帐户激活和电子邮件帐户验证。
它在 LocalHost 上运行良好,但是当我将它部署到实时服务器上时,它给了我这个错误:
Error.String 引用未设置为字符串的实例。参数名称:s
以下是我为发送电子邮件而编写的代码:
var verifyUrl = string.Empty;
verifyUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/AccountVerify?I=" + NewUserID;
string body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + objuserdet.Email + "" +
", </p><p>To verify your account, please click the following link:</p>"
+ "<p><a href=\"" + verifyUrl + "\" target=\"_blank\">" + verifyUrl + ""
+ "</a></p><div>Best regards,</div><div>" + NewUserID + " Team.</div><p>Note: Do not forward "
+ "this email. The verify link is private.</p></body></html>";
string To = objuserdet.Email;
string Subject = "Account Activation";
SendEmail(To,Subject,body);
这里是电子邮件方法:
public void SendEmail(string To, string Subject, string Body)
{
try
{
MailMessage mail = new MailMessage();
// mail.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["EmailID"].ToString());
mail.From = new MailAddress("blue.naina9@gmail.com");
mail.To.Add(To.Trim());
mail.Subject = Subject.Trim();
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential BasicAuthenticationInfo = new System.Net.NetworkCredential("blue.naina9@gmail.com","*******");
smtp.Timeout = 600000;
smtp.UseDefaultCredentials = false;
smtp.Credentials = BasicAuthenticationInfo;
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
..
任何帮助将不胜感激。
谢谢
发布堆栈跟踪会有所帮助。我认为唯一会出现这种错误的地方是
mail.To.Add(To.Trim());
替换string To = objuserdet.Email;
和
if(!string.IsNullOrEmpty(objuserdet.Email)){
string To = objuserdet.Email;
}
else{
throw new Exception("Email is null!");
}
检查这是否有效!
发生此错误的原因是,由于我当前位置和部署服务器位置(即在美国)的更改,Gmail 阻止了我的应用程序对发件人电子邮件帐户的登录请求。
所以我添加了一个用于发送电子邮件的美国帐户,它的统计数据对我来说工作正常..
希望这也能帮助其他人..
我正在做一个 MVC 应用程序,并试图向注册用户发送一封电子邮件,用于他的帐户激活和电子邮件帐户验证。 它在 LocalHost 上运行良好,但是当我将它部署到实时服务器上时,它给了我这个错误:
Error.String 引用未设置为字符串的实例。参数名称:s 以下是我为发送电子邮件而编写的代码:
var verifyUrl = string.Empty;
verifyUrl = Request.Url.GetLeftPart(UriPartial.Authority) + "/Account/AccountVerify?I=" + NewUserID;
string body = "<html><head><meta content=\"text/html; charset=utf-8\" /></head><body><p>Dear " + objuserdet.Email + "" +
", </p><p>To verify your account, please click the following link:</p>"
+ "<p><a href=\"" + verifyUrl + "\" target=\"_blank\">" + verifyUrl + ""
+ "</a></p><div>Best regards,</div><div>" + NewUserID + " Team.</div><p>Note: Do not forward "
+ "this email. The verify link is private.</p></body></html>";
string To = objuserdet.Email;
string Subject = "Account Activation";
SendEmail(To,Subject,body);
这里是电子邮件方法:
public void SendEmail(string To, string Subject, string Body)
{
try
{
MailMessage mail = new MailMessage();
// mail.From = new MailAddress(System.Configuration.ConfigurationManager.AppSettings["EmailID"].ToString());
mail.From = new MailAddress("blue.naina9@gmail.com");
mail.To.Add(To.Trim());
mail.Subject = Subject.Trim();
mail.Body = Body;
mail.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient("smtp.gmail.com", 587);
System.Net.NetworkCredential BasicAuthenticationInfo = new System.Net.NetworkCredential("blue.naina9@gmail.com","*******");
smtp.Timeout = 600000;
smtp.UseDefaultCredentials = false;
smtp.Credentials = BasicAuthenticationInfo;
smtp.EnableSsl = true;
smtp.Send(mail);
}
catch (Exception ex)
{
throw ex;
}
}
..
任何帮助将不胜感激。 谢谢
发布堆栈跟踪会有所帮助。我认为唯一会出现这种错误的地方是
mail.To.Add(To.Trim());
替换string To = objuserdet.Email;
和
if(!string.IsNullOrEmpty(objuserdet.Email)){
string To = objuserdet.Email;
}
else{
throw new Exception("Email is null!");
}
检查这是否有效!
发生此错误的原因是,由于我当前位置和部署服务器位置(即在美国)的更改,Gmail 阻止了我的应用程序对发件人电子邮件帐户的登录请求。 所以我添加了一个用于发送电子邮件的美国帐户,它的统计数据对我来说工作正常.. 希望这也能帮助其他人..