簡體   English   中英

應用程序錯誤:指定的字符串不是電子郵件地址所需的格式

[英]Error in Application : The specified string is not in the form required for an e-mail address

當用戶單擊發送按鈕時,我試圖向用戶的帳戶發送電子郵件。 但我收到上述錯誤。 下面是我的sendClick代碼。

protected void btnsendCode_Click(object sender, EventArgs e)
{
    try
    {
        if (txtemail.Text != "")
        {
            Random rd = new Random();

            veri = rd.Next(1000, 10000);
            MailMessage mm = new MailMessage();
            mm.To.Add(new MailAddress(txtemail.Text.ToString()));

            mm.From = new MailAddress("xxx@yyy.in", "Verification Mail");
            mm.Body = "Your Verification Code is - " + veri.ToString();
            mm.IsBodyHtml = true;
            mm.Subject = "Verification mail";
            SmtpClient smcl = new SmtpClient();
            smcl.Host = "smtp.gmail.com";
            smcl.Port = 587;
            smcl.Credentials = new NetworkCredential("xxx@yyy.in", "xxx");
            //smcl.EnableSsl = true;
            smcl.Send(mm);
            Page.ClientScript.RegisterStartupScript(GetType(), "msgbox", "alert('Verification Code sent to your Email ID! Please Check your Email!!');", true);
            txtverify.Enabled = true;
            btnsendCode.Text = "Send Code Again";
            lblmsg.Visible = false;
        }
        else
        {
            lblmsg.Visible = true;
            lblmsg.Text = "Please enter Email ID!!";
            lblmsg.ForeColor = System.Drawing.Color.Yellow;
            lblmsg.BorderColor = System.Drawing.Color.Red;
            lblmsg.BorderStyle = BorderStyle.Ridge;
            lblmsg.BorderWidth = new Unit("2");
            lblmsg.Focus();
        }
    }
    catch (WebException we)
    {
        lblmsg.Visible = true;
        lblmsg.Text = we.Message.ToString();
        lblmsg.ForeColor = System.Drawing.Color.Yellow;
        lblmsg.BorderColor = System.Drawing.Color.Red;
        lblmsg.BorderStyle = BorderStyle.Ridge;
        lblmsg.BorderWidth = new Unit("2");
    }
}

堆棧跟蹤

[FormatException: 指定的字符串不是電子郵件地址所需的格式。]
System.Net.Mail.MailAddressParser.ReadCfwsAndThrowIfIncomplete(String data, Int32 index) +1475945
System.Net.Mail.MailAddressParser.ParseDomain(String data, Int32& index) +135 System.Net.Mail.MailAddressParser.ParseAddress(String data, Boolean expectMultipleAddresses, Int32& index) +99
System.Net.Mail.MailAddressParser.ParseAddress(字符串數據)+23
System.Net.Mail.MailAddress..ctor(String address, String displayName, Encoding displayNameEncoding) +220
System.Net.Mail.MailMessage..ctor() +130
d:\\inetpub\\vhosts\\marpallichande.in\\httpdocs\\Test\\events.aspx.cs:101 中的 events.btnsendCode_Click(Object sender, EventArgs e)
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9552874
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35 System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724

哪一部分我犯了錯誤需要更正?

失敗的原因是使用空構造函數而沒有匹配的郵件設置:

MailMessage mm = new MailMessage();

空構造函數依賴於:

<system.net>
  <mailSettings>
    <smtp from="report@company.com" />
  </mailSettings>
</system.net>

在您的應用程序或 web.config 文件中。 因此,要么使用需要 from 和 to 尋址的構造函數,要么將該節點添加到您的 app/web.config 文件中。

堅信這是 .Net 框架中的一個錯誤,因為我們能夠創建新的MailMessage()對象,然后稍后分配“ MailMessage() ”,但是在 .Net 4.0 和某些條件下 - 我仍然沒有完全理解——這失敗了。 我當然歡迎被糾正,但就目前而言,這似乎是一個疏忽。

所以我們的一些客戶從未遇到過這個問題,但為了解決這個問題,我們不得不將這個虛擬設置添加到 web.config 文件中。

我在獲取[FormatException: The specified string is not in the form required for an e-mail address.]也遇到了類似的問題。 我的問題是這段代碼:

  void SendMail(string destinationEmail)
    MailMessage message=new MailMessage("mytestmail@test.com",destinationEmail);
    SmtpClient mailClient = new SmtpClient("mail.test.com", 587);
    mailClient.Credentials = new System.Net.NetworkCredential("mytestmail", "pswd");
    try{
     mailClient.Send(mail);
    }
    catch (Exception ex){...}

如您所見,我只是嘗試並捕獲了僅發送部分。 但是這個問題是從 MailMessage 構造函數拋出的。 我永遠不會猜到構造函數會拋出異常。 所以我只是獨立嘗試了這段代碼:

MailMessage mail = new MailMessage("test", "test");

此代碼的結果是[FormatException: The specified string is not in the form required for an e-mail address.] 如果你試試這個:

MailMessage mail = new MailMessage("", "");    

你會得到 ArgumentException: The parameter 'from'(or 'to') cannot be an empty string.

因此,在這種情況下,您需要做的是將 MailMessage 構造部分也包含在 try catch 塊中,並確保您捕獲的異常涵蓋 MailMessage 構造函數可能拋出的異常類型,在這種特殊情況下為 FormatException。 雖然我討厭使用 Exception 類,但在這種情況下它似乎是一個完美的選擇。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM