简体   繁体   English

System.Net.Mail.SmtpException:该地址具有无效的主机名

[英]System.Net.Mail.SmtpException: The address has an invalid host name

I have already tried almast everything but it keeps giving me this message while all the information is correct( I think so). 我已经尝试了almast的所有操作,但是在所有信息正确的情况下,它会一直向我发送此消息(我认为是正确的)。

This is the Error: 这是错误:

System.Net.Mail.SmtpException: The address has an invalid host name: yakovmora1@gmail.com‏. System.Net.Mail.SmtpException:该地址的主机名无效: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) System.ArgumentException:在索引9处找到无效的Unicode代码点。参数名称:System.Text.Normalization.Normalize(字符串strInput,NormalizationForm normForm)处的strInput

my code in ASPX.CS is: 我在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;
    }

The error is self-explanatory: 该错误是不言自明的:

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)

Taking a look at this string: yakovmora1@gmail.com 看一下这个字符串: yakovmora1@gmail.com

The 9th character is 1 , which seems fine to me, but I see that you're embedding it directly within your source-code (a very bad idea, but nevermind). 第9个字符是1 ,对我来说似乎很好,但是我看到您将其直接嵌入到源代码中(一个很糟糕的主意,但是没关系)。 Are you saving your *.cs files with the right encoding? 您是否使用正确的编码保存*.cs文件? Make sure you use UTF-8 or UTF-16 (Visual Studio > File > Advanced Save Options > Encoding ). 确保使用UTF-8或UTF-16(Visual Studio> 文件 > 高级保存选项 > 编码 )。

It is possible that before sending the email you watch the mail message value in the debugger, or call "ToString" method. 在发送电子邮件之前,您可能会在调试器中查看邮件消息的值,或者调用“ ToString”方法。 In this case, under .NET 3.5 or earlier, there is a bug which changes a field value within the mail message object that causes this failure later. 在这种情况下,在.NET 3.5或更早版本下,存在一个错误,该错误会更改邮件对象中的字段值,从而导致以后出现此故障。

I also encounter a similar bug, and resolved it. 我也遇到了类似的错误,并已解决。 You can see the details here: MailMessage "From" property assignment indirectly causes exception 您可以在此处查看详细信息: MailMessage“发件人”属性分配间接导致异常

Hope that this will help you! 希望对您有所帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 出现错误:-System.net.mail.SmtpException - Getting error:-System.net.mail.SmtpException System.Net.Mail.SmtpException:邮箱不可用 - System.Net.Mail.SmtpException: Mailbox unavailable System.Net.Mail.SmtpException:操作已超时/无法连接服务器 - System.Net.Mail.SmtpException: The operation has timed out / Unable to connect server 如何修复 System.Net.Mail.SmtpException - How Can I Fix System.Net.Mail.SmtpException system.net.mail.smtpException:无法连接到远程服务器? - system.net.mail.smtpException :Unable to connect to the remote server? 如何正确处理System.Net.Mail.SmtpException? - How to correctly handle System.Net.Mail.SmtpException? System.Net.Mail.SmtpException {“发送电子邮件失败。”} - System.Net.Mail.SmtpException {“Failure Sending Email.”} System.Net.Mail.SmtpException: 操作已超时。 asp.net 中的错误使用godaddy 托管发送邮件代码 - System.Net.Mail.SmtpException: The operation has timed out. error in asp.net send mail code using godaddy hosting System.Net.Mail.SmtpException:发送邮件失败。 无法从传输连接中读取数据 - System.Net.Mail.SmtpException: Failure sending mail. Unable to read data from the transport connection System.Net.Mail.SmtpException:系统存储不足。 服务器响应如下:4.3.1系统资源不足 - System.Net.Mail.SmtpException: Insufficient system storage. The server response was: 4.3.1 Insufficient system resources
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM