This is a migrated thread and some comments may be shown as answers.

How to attach radGrid data to mail ?

10 Answers 361 Views
Grid
This is a migrated thread and some comments may be shown as answers.
Ram
Top achievements
Rank 1
Ram asked on 19 Feb 2009, 10:43 AM
Hi,

My requirement is as follows:

I want to attach radgrid data to mail and send it across

Ho can I achieve this?

Thanks,

10 Answers, 1 is accepted

Sort by
0
Shinu
Top achievements
Rank 2
answered on 20 Feb 2009, 08:10 AM
Hi Ram,

I hope you are trying to send complete RadGrid data as an E-mail attachment. One suggestion for achieving this, export the RadGrid data to external file (Microsoft Excel/Word/PDF/CSV) and save it in a specified location. Then attach this file to mail message for sending. Check out following link for more information about exporting RadGrid.
Export to Microsoft Excel/Word/PDF/CSV
Exporting tips and tricks

Thanks,
Shinu.


0
Ram
Top achievements
Rank 1
answered on 20 Feb 2009, 08:32 AM
Thanks Shinu, for your suggestion

I don't want to export and save it, suppose I have a button called send as mail, on that button click my mail box has to open in that this grid data should be attached or displayed so that I can send it.
Is this possible
0
Mike
Top achievements
Rank 1
answered on 07 May 2013, 06:11 PM
Yes, I want the same functionality.  Is it possible or not?
0
Princy
Top achievements
Rank 2
answered on 08 May 2013, 05:10 AM
Hi,

I guess you want to extract the grid contents in an HTML format and send it via mail. Please check the following code snippet to get the Grid content in html format.

ASPX:
<telerik:RadGrid ID="RadGrid1" Skin="Black" runat="server" GridLines="None" OnNeedDataSource="RadGrid1_NeedDataSource1">
    <MasterTableView>
        <Columns>
            <telerik:GridBoundColumn DataField="Subject" HeaderText="Subject">
            </telerik:GridBoundColumn>
        </Columns>
        <RowIndicatorColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
        </RowIndicatorColumn>
        <ExpandCollapseColumn>
            <HeaderStyle Width="20px"></HeaderStyle>
        </ExpandCollapseColumn>
    </MasterTableView>
</telerik:RadGrid>
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Send email - grid" />

C#:
protected void RadGrid1_NeedDataSource1(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
{
    List<MailMessage> mList = new List<MailMessage>();
    MailMessage m1 = new MailMessage();
    m1.Subject = "One";
    mList.Add(m1);
    MailMessage m2 = new MailMessage();
    m2.Subject = "Two";
    mList.Add(m2);
    MailMessage m3 = new MailMessage();
    m3.Subject = "Three";
    mList.Add(m3);
    RadGrid1.DataSource = mList;
}
protected void Button1_Click(object sender, EventArgs e)
{
    string GridRawHtml;
    StringWriter stringWriter = new StringWriter();
    HtmlTextWriter clearWriter = new HtmlTextWriter(stringWriter);
    RadGrid1.RegisterWithScriptManager = false;
    RadGrid1.RenderControl(clearWriter);
    GridRawHtml = clearWriter.InnerWriter.ToString();
    GridRawHtml = GridRawHtml.Remove(GridRawHtml.IndexOf("<script"), GridRawHtml.LastIndexOf("</script>") - GridRawHtml.IndexOf("<script"));
    Response.Write(GridRawHtml);
}

Thanks,
Princy.
0
Mike
Top achievements
Rank 1
answered on 08 May 2013, 12:35 PM
Thank you Princy,

Your solution works, but I ended up going with this one.
http://www.telerik.com/community/forums/aspnet-ajax/grid/open-email-and-attach-grid-as-pdf-or-excel.aspx

protected void ButtonExcel_Click(object sender, System.EventArgs e)
  {
 
      ConfigureExport();
      RadGrid Grid1 = (RadGrid)Page.Master.FindControl("Main").FindControl("RadGrid1");
      foreach (GridItem commandItem in Grid1.MasterTableView.GetItems(GridItemType.CommandItem))
      {
          RadGrid1.ExportSettings.ExportOnlyData = true;
          commandItem.Visible = false;
      }
      Grid1.MasterTableView.ExportToExcel();
  }
 
  public void ConfigureExport()
  {
      RadGrid Grid1 = (RadGrid)Page.Master.FindControl("Main").FindControl("RadGrid1");
      Grid1.ExportSettings.ExportOnlyData = false;
      Grid1.ExportSettings.IgnorePaging = true;
      Grid1.ExportSettings.OpenInNewWindow = true;
  }
 
  protected void RadGrid1_GridExporting(object source, GridExportingArgs e)
  {
      if (e.ExportType == ExportType.Excel)
      {
          // The Meat and potatoes here by dumping it into a memory stream
          MemoryStream ms = new MemoryStream(new ASCIIEncoding().GetBytes(e.ExportOutput));
          SendMail("smtp.gmail.com", "FromEmail@gmail.com", "ToEmail@gmail.com", "Subject Goes Here", "Body Goes Here", false, ms, "ExcelAttachment.xls");
      }
      Response.Redirect(Request.Url.AbsoluteUri);
  }
 
  public void SendMail(string smtpAddress, string from, string to, string body, string subject, bool isHtml, Stream attachmentStream, string fileName)
  {
      // Pull settings from configuration in web.config
      SmtpClient smtpClient = new SmtpClient(Convert.ToString(ConfigurationManager.AppSettings["SERVER_EMAIL"]));
      string authType = "Basic";
      MailMessage email = new MailMessage();
      email.From = new MailAddress(from);
      email.To.Add(to);
      email.Body = body;
      email.Subject = subject;
      email.IsBodyHtml = isHtml;
      email.Attachments.Add(new Attachment(attachmentStream, fileName));
      // I actually encrypt these credentials
      NetworkCredential nc = new NetworkCredential(Convert.ToString(ConfigurationManager.AppSettings["CredentialU"]), Convert.ToString(ConfigurationManager.AppSettings["CredentialP"]));
      smtpClient.Credentials = nc.GetCredential(Convert.ToString(smtpClient), 25, authType);
      smtpClient.Send(email);
  }
0
BoddaGetta
Top achievements
Rank 1
answered on 19 May 2015, 04:43 PM

I tried your example, and when RenderControl is called, I get this exception:

 {"Control 'RadGrid1_ctl00_ctl04_ctl00' of type 'CheckBox' must be placed inside a form tag with runat=server."}

Even though the RadGrid is inside the form tag, which has runat=server.  Any ideas why?

0
Konstantin Dikov
Telerik team
answered on 22 May 2015, 12:11 PM
Hi,

If you have server controls within the grid, it is expected to receive the error in question, because the control is rendered to a HtmlTextWriter object and within that object there will be no Form element. For your scenario I would suggest that you use the approach from the Mike's response, where the OnGridExporting is used for retrieving the exported content of the grid.

Hope this helps.


Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
BoddaGetta
Top achievements
Rank 1
answered on 26 May 2015, 01:36 PM

I'm actually using the OnGridExportingapproach if I only want to email the RadGrid contents.  However, in this case I'd like to email an entire html page, which includes an image, a RadGrid, and some text.  Is this possible?  It works fine if I use an ASP GridView instead of a RadGrid, but the RadGrid looks much better and is so much easier to work with.

0
Konstantin Dikov
Telerik team
answered on 28 May 2015, 03:31 PM
Hello,

You can use the OnGridExporting approach for retrieving the grid data from the e.ExportOutput and manually include the image and the text to the memory stream.

Using the other approach, where a dummy page is created will not work with RadGrid and the previous approach. Nevertheless, you can try to add Form object to the Page and then add the RadGrid control in it, but I have to say that I am not sure if you will be able to resolve all of the requirement and errors that will come afterwords, so at the end, I highly recommend that you manually include the information directly to the memory stream.


Regards,
Konstantin Dikov
Telerik
Do you want to have your say when we set our development plans? Do you want to know when a feature you care about is added or when a bug fixed? Explore the Telerik Feedback Portal and vote to affect the priority of the items
0
BoddaGetta
Top achievements
Rank 1
answered on 28 May 2015, 04:38 PM
Thanks!  I will give it a try.
Tags
Grid
Asked by
Ram
Top achievements
Rank 1
Answers by
Shinu
Top achievements
Rank 2
Ram
Top achievements
Rank 1
Mike
Top achievements
Rank 1
Princy
Top achievements
Rank 2
BoddaGetta
Top achievements
Rank 1
Konstantin Dikov
Telerik team
Share this question
or