How to send Chart Control in email using C# ASP.Net

965 Views Asked by At

I have written the below code to send the Gridview and Chart Image in email body message (not as an attachment) using C# ASP.Net.

    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("[email protected]", "aura");
            mail.To.Add("[email protected]");

            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 = "[email protected]";
            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 */
    }

If I remove the BarChart1 control then Gridview shows in the mail message perfectly but while I add, BarChart1 throws the below error.

Script control 'BarChart1' is not a registered script control. Script controls must be registered using RegisterScriptControl() before calling RegisterScriptDescriptors().
Parameter name: scriptControl

Can anyone please clarify me exactly whats the error.

0

There are 0 best solutions below