简体   繁体   English

在邮件标题中发现无效字符:';'

[英]An invalid character was found in the mail header: ';'

error on this line "message.To.Add(strCommandText);"这一行的错误“message.To.Add(strCommandText);” when I try to take the email data from the database to send an email.当我尝试从数据库中获取电子邮件数据以发送电子邮件时。


public partial class beforeLogin_Auto_StaffPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {公共部分类 beforeLogin_Auto_StaffPage : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) {

}
protected void btnApprove_Click(object sender, EventArgs e)
{
    string approve = "Approve";

    string strConnectionString = ConfigurationManager.ConnectionStrings["ChadBankConnectionString"].ConnectionString;
    SqlConnection myConnection = new SqlConnection(strConnectionString);

    myConnection.Open();

    string strCommandText3 = "UPDATE AutoLoan SET autoStatus ='" + approve + "'WHERE userID= '" + Session["userID"] + "';";
    SqlCommand myCommand3 = new SqlCommand(strCommandText3, myConnection);
    myCommand3.ExecuteNonQuery();

    string strCommandText = "SELECT custEmail From Customer WHERE userID= '" + Session["userID"] + "';";
    SqlCommand myCommand = new SqlCommand(strCommandText, myConnection);
    myCommand.ExecuteNonQuery();

    string strCommandText1 = "SELECT loginName From Customer WHERE userID= '" + Session["userID"] + "';";
    SqlCommand myCommand1 = new SqlCommand(strCommandText1, myConnection);
    myCommand1.ExecuteNonQuery();

    NetworkCredential myCred = new NetworkCredential("CHADBank2013@gmail.com", "627726627");
    System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
    message.To.Add(strCommandText);
    message.From = new MailAddress("CHADBank2013@gmail.com");
    message.Subject = "CHAD Bank: Auto Loan Application Approved";
    message.Body = "Hi " + strCommandText1 + "," + Environment.NewLine + Environment.NewLine +
        "Your application for your auto loan are approved, payment will start on the next month";
    SmtpClient client = new SmtpClient("smtp.gmail.com");
    client.Port = 587;
    client.Credentials = myCred;
    client.EnableSsl = true;
    client.Send(message);

    myConnection.Close();

}
protected void gvDetail_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow row = gvDetail.SelectedRow;

    Session["userID"] = row.Cells[10].Text;

}

} }

You are adding not email but query into your recipients list.您添加的不是电子邮件,而是查询到您的收件人列表。 I assume you want to retrieve email address from your query:我假设您想从查询中检索电子邮件地址:

string strCommandText = "SELECT custEmail From Customer WHERE userID= '" + Session["userID"] + "';";
SqlCommand myCommand = new SqlCommand(strCommandText, myConnection);
var email = myCommand.ExecuteScalar();

//rest of the code
message.To.Add(email);

also you should use parametrized queries.您还应该使用参数化查询。

Environment: MSSQL / .NET (v4.5+)环境:MSSQL/.NET (v4.5+)

Been hunting down the poster's error for a bit today.今天一直在追查海报的错误。 And while I know this may not be relevant for the poster, it may be relevant for another visitor with the same error.虽然我知道这可能与海报无关,但它可能与另一个出现相同错误的访问者相关。

If you copied a database, and changed the login user name/pw/spid/ID/ whatever, and you get this error in the page - before you spend a lot of time looking through all of the email lists - make sure you cycle the app pool and clear relevant caches.如果您复制了一个数据库,并更改了登录用户名/密码/spid/ID/ 任何内容,并且您在页面中收到此错误 - 在您花费大量时间查看所有电子邮件列表之前 - 请确保您循环应用程序池并清除相关缓存。

I hope this saves someone some time.我希望这可以节省一些时间。

grrr...呜呜呜……

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM