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 "-//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("[email protected]", 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);
順便說一句,你的HTML是錯誤的;你不應該使用'"'。 – SLaks 2012-03-09 20:00:33
我從別人那裏繼承了這個網站,所以我試圖通過這個方法來解決這個問題。有什麼更好的使用? – Peter 2012-03-09 20:03:46
'「'。 – SLaks 2012-03-09 20:05:06