2012-03-09 40 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

相關問題