我编写了以下代码,使用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
任何人都可以请我澄清错误。