【发布时间】:2013-12-07 01:15:23
【问题描述】:
我正在尝试通过复选框发送邮件,我已经完成了,但是在这里我希望管理员单击复选框并按下按钮,然后管理员从转发器获取文档名称和状态的值,然后将邮件发送给用户
就像管理员以任何电子邮件 ID 发送邮件,然后当用户收到邮件文档名称时显示如下:abc 状态:拒绝
DocID DocName Uplaodedfile UserEmail DocType DepType status
1 ABC def.pdf abcdef@gmail.com pdf hr reject
2 hr hrdoc.pdf johkety@gmail.com pdf hr approve
这是电子邮件按钮代码
protected void btnSendMail_Click(object sender, EventArgs e)
{
string connStr = ConfigurationManager.ConnectionStrings["mydms"].ConnectionString;
SqlConnection mySQLconnection = new SqlConnection(connStr);
string empId = string.Empty;
DataTable dt = new DataTable();
try
{
mySQLconnection.Open();
for (int i = 0; i < Repeateremail.Items.Count; i++)
{
CheckBox checkboc = ((CheckBox)Repeateremail.Items[i].FindControl("chkSelect"));
if (checkboc != null)
{
if (checkboc.Checked == true)
{
//get Current EMAIL_ID from the DataKey
string emailId = (Label)Repeateremail.Items[i].FindControl("lbl_email")).Text;
string DocName = ((Label)Repeateremail.Items[i].FindControl("DocName")).Text;
string Status = ((Label)Repeateremail.Items[i].FindControl("Status")).Text;
//write code to send mail
SendEmailUsingGmail(emailId,DocName,Status);
dt.Clear();
dt.Dispose();
}
else if (checkboc.Checked == false)
{
}
}
}
}
catch (Exception ex)
{
emailsent.Text="Failed";
}
finally
{
empId = string.Empty;
}
}
private void SendEmailUsingGmail(string toEmailAddress,string DocName,string Status)
{
try
{
SmtpClient smtp = new SmtpClient();
smtp.Credentials = new NetworkCredential("johmm@gmail.com", "12234");
smtp.Port = 587;
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
MailMessage message = new MailMessage();
message.From = new MailAddress("johmm@gmail.com");
message.To.Add(toEmailAddress);
message.To.Add(DocName);
message.To.Add(Status);
message.Subject = "Write your email subject here";
message.Body = "write the content of the email here";
smtp.Send(message);
}
catch (Exception ex)
{
Response.Write("Error occured: " + ex.Message.ToString());
}
}
但它显示错误
在这一行
catch (Exception ex)
{
emailsent.Text="Failed";
}
`错误:对象引用未设置为对象的实例
【问题讨论】:
-
你能输出
ex中的内容吗,因为它会给你一个行号。 -
调试并查看哪一行抛出错误。可能是配置中缺少连接
-
您也不需要
dt.Clear()或dt.Dispose(),因为您没有使用它。它也可能是演员之一。 -
最有可能的错误是由于以下三行之一: string emailId = ((Label)Repeateremail.Items[i].FindControl("lbl_email")).Text;字符串 DocName = ((Label)Repeateremail.Items[i].FindControl("DocName")).Text; string Status = ((Label)Repeateremail.Items[i].FindControl("Status")).Text;
-
当我只发送电子邮件然后电子邮件发送成功但是当我编码这个 ((Label)Repeateremail.Items[i].FindControl("DocName")).Text; string Status = ((Label)Repeateremail.Items[i].FindControl("Status")).Text;还要发送文档名称和状态,它会显示错误
标签: asp.net email nullreferenceexception