System.Net.Mail.SmtpException:地址有一个无效的主机名
本文关键字:无效 主机 有一个 地址 Net Mail SmtpException System | 更新日期: 2023-09-27 18:09:01
我已经尝试了几乎所有的东西,但它一直给我这个消息,而所有的信息都是正确的(我认为是这样)。
错误提示:
System.Net.Mail。SmtpException:地址有一个无效的主机名:yakovmora1@gmail.com。系统。ArgumentException: Unicode无效在索引9处找到代码点。参数名称:strInput atSystem.Text.Normalization。Normalize(字符串strInput, NormalizationFormnormForm)
我在ASPX.CS中的代码是:
protected void Sbutton_Click(object sender, EventArgs e)
{
string Name = Request.Form["Name"];
string email = Request.Form["email"];
string message = Request.Form["Message"];
if (CheckFields(Name, email, message, ref Label1) == true)
{
try
{
MailMessage myEmail = new MailMessage()
{
Subject = "Message from website user: [" + Name + "]",
Body = "Email: " + email + Environment.NewLine + Environment.NewLine + message,
IsBodyHtml = false,
From = new MailAddress(email, Name),
};
myEmail.To.Add(new MailAddress("yakovmora1@gmail.com", "IsraeCoreLx"));
SmtpClient server = new SmtpClient();
server.Send(myEmail);
Label1.Text = "Message Sent";
}
catch (Exception ex)
{
Label1.Text = "error sending message <br/><br/>" + ex.ToString();
}
if (Label1.Text != "Message Sent")
{
Label1.Text = "trying again";
#region trying again
try
{
MailMessage myEmail = new MailMessage()
{
Subject = "Message from website user: [" + Name + "]",
Body = "Email: " + email + Environment.NewLine + Environment.NewLine + message,
IsBodyHtml = false,
From = new MailAddress(email, Name)
};
myEmail.To.Add(new MailAddress("yakovmora1@gmail.com", "IsraeCoreLx"));
SmtpClient server = new SmtpClient();
server.Port = 25;
server.Send(myEmail);
Label1.Text = "Message Sent";
}
catch (Exception ex)
{
Label1.Text = "error sending message <br/><br/>" + ex.ToString();
}
#endregion
}
if (Label1.Text == "Message Sent")
{
CleanFields( Name, email, message ,ref Label1);
}
}
}
private void CleanFields(string Name, string email, string message, ref Label Label1)
{
Request.Form["Name"] = "";
Request.Form["email"] = "";
Request.Form["message"] = "";
}
private Boolean CheckFields(string Name, string email, string message, ref Label Label1)
{
if (Name != "")
{
if (IsEmailValid(email) == true)
{
if (message != "")
return true;
else
Label1.Text = "Your message is empty.";
}
else
{
Label1.Text = "Your email is invalid.";
}
}
else
{
Label1.Text = "Please fill in your Name.";
}
return false;
}
private Boolean IsEmailValid(string EmailAddr)
{
if (EmailAddr != null || EmailAddr != "")
{
Regex n = new Regex("(?<user>[^@]+)@(?<host>.+)");
Match v = n.Match(EmailAddr);
if (!v.Success || EmailAddr.Length != v.Length)
return false;
else
return true;
}
else
return false;
}
错误不言自明:
System.Net.Mail.SmtpException:
The address has an invalid host name: yakovmora1@gmail.com.
System.ArgumentException: Invalid Unicode code point found at index 9.
Parameter name: strInput
at System.Text.Normalization.Normalize(String strInput, NormalizationForm normForm)
看一下这个字符串:yakovmora1@gmail.com
第9个字符是1
,这对我来说似乎很好,但我看到你将它直接嵌入到你的源代码中(一个非常糟糕的主意,但没关系)。您是否使用正确的编码保存*.cs
文件?确保使用UTF-8或UTF-16 (Visual Studio>File>Advanced Save Options>Encoding)。
在发送电子邮件之前,您可以在调试器中查看邮件消息值,或者调用"ToString"方法。在这种情况下,在。net 3.5或更早的版本中,有一个错误会改变邮件消息对象中的字段值,从而导致以后的失败。
我也遇到了类似的bug,并解决了它。你可以在这里看到细节:MailMessage"From"属性分配间接导致异常
希望这对你有帮助!