我已经编写了下面的代码来使用 C# ASP.Net 在电子邮件正文消息(不是作为附件)中发送 Gridview 和图表图像。
StringBuilder sb = new StringBuilder();
string MailSturcture;
using (System.IO.StringWriter sw = new StringWriter())
{
using (HtmlTextWriter hw = new HtmlTextWriter(sw))
{
gvDaySummary.RenderControl(hw);
hw.WriteBreak();
BarChart1.RenderControl(hw);
System.IO.StringReader sr = new StringReader(sw.ToString());
MailMessage mail = new MailMessage();
mail.From = new MailAddress("noreply@xxx.com", "aura");
mail.To.Add("abc@xxx.com");
mail.Subject = "Daily Payment Summary";
mail.IsBodyHtml = true;
sb.AppendFormat(sw.ToString());
MailSturcture = sb.ToString();
mail.Body = MailSturcture;
System.Net.Mail.SmtpClient smtp = new SmtpClient();
smtp.Host = "smtpout.secureserver.net";
smtp.Port = 80;
System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
NetworkCred.UserName = "abc@xxx.com";
NetworkCred.Password = "xxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = NetworkCred;
smtp.EnableSsl = false;
smtp.Send(mail);
}
}
public override void VerifyRenderingInServerForm(System.Web.UI.Control control)
{
/* Verifies that the control is rendered */
}
如果我删除 BarChart1 控件,那么 Gridview 会完美地显示在邮件消息中,但是当我添加时,BarChart1 会引发以下错误。
Script control 'BarChart1' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
Parameter name: scriptControl
任何人都可以请澄清我到底是什么错误。