[英]Specified argument was out of the range of valid values. Parameter name: value
My model and controller
Specified argument was out of the range of valid values.指定的参数超出了有效值的范围。 Parameter name: value Description: An unhandled exception occurred during the execution of the current web request.
参数名称:值 说明:当前web请求执行过程中出现未处理的异常。 Please review the stack trace for more information about the error and where it originated in the code.
请查看堆栈跟踪以获取有关错误及其在代码中的来源的更多信息。
Exception Details: System.ArgumentOutOfRangeException: Specified argument was out of the range of valid values.异常详细信息:System.ArgumentOutOfRangeException:指定的参数超出了有效值的范围。 Parameter name: value
参数名称:值
My EmailModel我的电子邮件模型
public class EmailModel
{
[Required,Display(Name= "Name")]
public string FromName { get; set; }
[Required, Display(Name = "Email")]
public string FromEmail { get; set; }
public string Subject { get; set; }
public string Body { get; set; }
}
My IdentityConfig.cs我的 IdentityConfig.cs
public async Task<bool> SendMailAsync(IdentityMessage message)
{
//private config set up
var GmailUsername = WebConfigurationManager.AppSettings["username"];
var GmailPasword = WebConfigurationManager.AppSettings["password"];
var host = WebConfigurationManager.AppSettings["host"];
int port = Convert.ToInt32(WebConfigurationManager.AppSettings["port"]);
var from = new MailAddress(WebConfigurationManager.AppSettings["emailfrom"], "MyBugTracker");
//Email object set up
var email = new MailMessage(from, new MailAddress(message.Destination))
{
Subject = message.Subject,
Body = message.Body,
IsBodyHtml = true,
};
//SMTP set up
using (var smtp = new SmtpClient()
{
Host = host,
Port = port,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new NetworkCredential(GmailUsername, GmailPasword)
})
{
try
{
await smtp.SendMailAsync(email);
return true;
}
catch (Exception e)
{
Console.WriteLine(e.Message);
return false;
}
};
}
}
This is my AppSettings I'm using a localhost这是我的 AppSettings 我正在使用本地主机
<appSettings>
<add key ="username" value="mygmail"/>
<add key="password" value="myPassword"/>
<add key="emailfrom" value="s@gmail.com"/>
<add key="host" value=" smtp.gmail.com"/>
<add key="port " value="587"/>
</appSettings>
This is my error这是我的错误
I try to change the port number to 465 but does not work我尝试将端口号更改为 465 但不起作用
The issue comes from the space in the appSettings file for the port key.问题来自端口密钥的 appSettings 文件中的空间。 You have specified "port " as a key with a space instead of "port".
您已将“端口”指定为带有空格而不是“端口”的键。 This is why the arguments is out of range when you try to instantiate the SmtpClient.
这就是当您尝试实例化 SmtpClient 时 arguments 超出范围的原因。 If you change your appSettings and remove the space and leave the code as it is.
如果您更改 appSettings 并删除空格并保持代码不变。 It Will work.
它会工作。
If this resolves your issue.如果这解决了您的问题。 Please chose this as the ideal answer.
请选择这个作为理想的答案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.