为什么我得到一个对象引用错误?[复制]
-
10-12-2019 - |
题
简单地说,我有一个表单,当用户提交电子邮件时,会发送给他们感谢他们的兴趣,另一封电子邮件会发送给管理员,说有人注册了。第一个MailMessage(msgMail)发送得很好,所以我编码了第二个MailMessage(msgMail2),完全相同,但只是改变了电子邮件的文本。先谢谢你!
当我把它们都放在页面上时,我会得到一个:
对象引用未设置为对象的实例。
源错误:(编辑:为了保持简洁,我只是把源错误,它指向第285行)
Line 283: msgMail2.Subject = "ZProgramsMatch Insurance Quote Request";
Line 284:
Line 285: msgMail2.Body = "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">" +
Line 286: "<html xmlns="http://www.w3.org/1999/xhtml" >" +
Line 287: "<head><title>Untitled Page</title></head><body>" +
下面是两个MailMessages的代码:(我正在删除基本文本以保持简短)
MailMessage msgMail = new MailMessage("info@zprogramsmatch.com", txtEmail.Text);
msgMail.IsBodyHtml = true;
msgMail.Subject = "...";
msgMail.Body = "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">" +
"<html xmlns="http://www.w3.org/1999/xhtml" >" +
"<head><title>Untitled Page</title></head><body>";
if (sImage.Length > 5)
{
msgMail.Body += "<img src='" + sImage + "' />";
}
msgMail.Body += "...";
SmtpClient smtp = new SmtpClient();
try
{
smtp.Send(msgMail);
}
catch
{
}
msgMail = null;
MailMessage msgMail2 = new MailMessage("...", "...");
msgMail2.IsBodyHtml = true;
msgMail2.Subject = "...";
msgMail2.Body = "<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">" +
"<html xmlns="http://www.w3.org/1999/xhtml" >" +
"<head><title>Untitled Page</title></head><body>" +
"Email: " + Session["Email"].ToString() + "<br />" +
"Name: " + txtFirstName.Text + " " + txtLastName.Text + "<br />" +
"Title: " + txtTitle.Text + "<br />" +
"Company: " + txtCompany.Text + "<br />" +
"Address 1: " + txtAddress1.Text + "<br />" +
"Address 2: " + txtAddress2.Text + "<br />" +
"City: " + txtCity.Text + "<br />" +
"State: " + ddlState.SelectedValue + "<br />" +
"Zip Code: " + txtZip.Text + "<br />" +
"Phone Number: (" + txtPhoneArea.Text + ")" + txtPhonePre.Text + "." + txtPhoneNo.Text + "<br />" +
"Cell Number: (" + txtCellArea.Text + ")" + txtCellPre.Text + "." + txtCellNo.Text + "<br />" +
"Preferred Contact Method: " + ddlContractType.SelectedValue + "<br />" +
"Best Time to Contact: " + ddlContactTime.SelectedValue + "<br />" +
"Name of Insured: " + txtNameOfInsured.Text + "<br />" +
"Additional Info: " + txtComments.Text + "<br />" +
"</body></html>";
smtp.Send(msgMail2);
解决方案
最明显的罪犯是 Session["Email"]
(在线上 "Email: " + Session["Email"].ToString() + "<br />" +
).您确定会话数据中存在与此密钥关联的值吗?
否则真的可能是 任何 在该语句中访问的控件中,尽管根据我的经验,它的可能性较小。
其他提示
是身体弦上的东西。尝试将整个body字符串存储在一个变量中,并将其写入控制台或其他内容。然后将body设置为变量。
编辑:或者为了快速验证它,只需将正文设置为空字符串即可。如果它确实有效,那就是你的问题。
不隶属于 StackOverflow