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