2013-04-14 20 views
0

我已經嘗試almast一切,但它不斷給我這個消息,而所有的信息是正確的(我是這麼認爲的)。System.Net.Mail.SmtpException:地址有一個無效的主機名

這是錯誤:

System.Net.Mail.SmtpException: The address has an invalid host name: [email protected]‏. System.ArgumentException: Invalid Unicode code point found at index 9. Parameter name: strInput at System.Text.Normalization.Normalize(String strInput, NormalizationForm normForm)

我在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("[email protected]‏", "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("[email protected]‏", "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; 
    } 

回答

1

的錯誤是不言自明:

System.Net.Mail.SmtpException: 
The address has an invalid host name: [email protected]‏. 

System.ArgumentException: Invalid Unicode code point found at index 9. 
Parameter name: strInput 

at System.Text.Normalization.Normalize(String strInput, NormalizationForm normForm) 

考慮看看這個字符串:[email protected]

第9個字符是1,這對我來說看起來很好,但是我發現你直接將它嵌入到你的源代碼中(這是一個非常糟糕的主意,但從未想過)。你是否用正確的編碼保存你的*.cs文件?確保使用UTF-8或UTF-16(Visual Studio>文件>高級保存選項>編碼)。

+0

你的意思是說,一個非常糟糕的主意?你想讓我寫什麼? –

+0

任何可以改變的地方,例如電子郵件地址,連接字符串,用戶名,密碼,主機名等都應該存儲在一個單獨的用戶可編輯配置文件中。您可以使用'app.config'和''來實現簡單的鍵/值存儲。 – Dai

0

這可能是發送電子郵件之前你看在調試器的郵件值,或者叫「的ToString」方法。在這種情況下,在.NET 3.5或更低版本中,存在一個錯誤,它會更改郵件消息對象中的字段值,以便稍後導致此故障。

我也遇到了類似的錯誤,並解決它。你可以看到這裏的細節: MailMessage "From" property assignment indirectly causes exception

希望這會幫助你!

相關問題