为什么我收到对象引用错误?

时间:2012-03-09 19:59:00

标签: c# asp.net smtp mailmessage

  

可能重复:
  What is a NullReferenceException in .NET?

简单地说,我有一个表单,当用户提交电子邮件时,会向他们发送电子邮件,感谢他们的兴趣,另一封电子邮件发送给管理员,说有人报名。第一个MailMessage(msgMail)发送正常,所以我编写了第二个MailMessage(msgMail2),完全相同,只是更改了电子邮件的文本。提前谢谢!

当我在页面上同时显示它们时,我得到了:

  

对象引用未设置为对象的实例。

源错误:(编辑:保持简洁我只是把源错误,指向第285行)

Line 283:        msgMail2.Subject = "ZProgramsMatch Insurance Quote Request";
Line 284:
Line 285:        msgMail2.Body = "<!DOCTYPE html PUBLIC &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>" +
Line 286:        "<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot >" +
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 &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>" +
    "<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot >" +

    "<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 &quot;-//W3C//DTD XHTML 1.0 Transitional//EN&quot; &quot;http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd&quot;>" +
    "<html xmlns=&quot;http://www.w3.org/1999/xhtml&quot >" +
    "<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);

2 个答案:

答案 0 :(得分:1)

这是身体字符串中的内容。尝试将整个正文字符串存储在变量中并将其写入控制台或其他内容。然后将主体设置为变量。

编辑:或者快速验证它只是将主体设置为空字符串。如果它确实有效,那就是你的问题。

答案 1 :(得分:1)

最明显的罪犯是Session["Email"](在"Email: " + Session["Email"].ToString() + "<br />" +行中)。您确定会话数据中是否存在与此键相关联的值?

否则它实际上可能是您在该语句中访问的任何控件,但根据我的经验,它不太可能。

相关问题