2012-03-09 63 views
0

Possible Duplicate:
What is a NullReferenceException in .NET?为什么我会收到对象引用错误?

简单地说,我有一个表格,当用户提交电子邮件发送给他们,感谢他们的兴趣和另一电子邮件转到管理员说,签署了一个人。第一个MailMessage(msgMail)发送正常,所以我编码了第二个MailMessage(msgMail2),完全相同,但只是改变了电子邮件的文本。先谢谢你!

当我有他们两个在页面上我得到一个:

Object reference not set to an instance of an object.

源错误:(编辑:以保持简洁,我只是把源错误,这点到线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("[email protected]", 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); 
+0

顺便说一句,你的HTML是错误的;你不应该使用'"'。 – SLaks 2012-03-09 20:00:33

+0

我从别人那里继承了这个网站,所以我试图通过这个方法来解决这个问题。有什么更好的使用? – Peter 2012-03-09 20:03:46

+0

'“'。 – SLaks 2012-03-09 20:05:06

回答

1

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

否则它真的可能是任何您在该声明中访问的控件,尽管根据我的经验它不太可能。

+0

Yahtzee!谢谢! – Peter 2012-03-09 20:28:33

1

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

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

+0

我的确喜欢你说的话,它只是身体线中的东西!谢谢。 – Peter 2012-03-09 20:14:48

+0

你的身体文字的最后一行没有一对匹配的引号 - 这只是问题中的拼写错误,还是它的问题? – Ray 2012-03-09 20:18:38

+0

你在找什么?这可能是问题,但我没有看到它。谢谢。 – Peter 2012-03-09 20:22:47

相关问题