How to attach radGrid data to mail ?

11 posts, 0 answers
  1. Ram
    Ram avatar
    49 posts
    Member since:
    Feb 2009

    Posted 19 Feb 2009 Link to this post

    Hi,

    My requirement is as follows:

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

    Ho can I achieve this?

    Thanks,
  2. Shinu
    Shinu avatar
    17764 posts
    Member since:
    Mar 2007

    Posted 20 Feb 2009 Link to this post

    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.


  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Ram
    Ram avatar
    49 posts
    Member since:
    Feb 2009

    Posted 20 Feb 2009 Link to this post

    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
  5. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 07 May 2013 Link to this post

    Yes, I want the same functionality.  Is it possible or not?
  6. Princy
    Princy avatar
    17421 posts
    Member since:
    Mar 2007

    Posted 08 May 2013 Link to this post

    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.
  7. Mike
    Mike avatar
    338 posts
    Member since:
    Jun 2008

    Posted 08 May 2013 Link to this post

    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);
      }
  8. BoddaGetta
    BoddaGetta avatar
    5 posts
    Member since:
    Dec 2012

    Posted 19 May 2015 in reply to Princy Link to this post

    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?

  9. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1803 posts

    Posted 22 May 2015 Link to this post

    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
  10. BoddaGetta
    BoddaGetta avatar
    5 posts
    Member since:
    Dec 2012

    Posted 26 May 2015 in reply to Konstantin Dikov Link to this post

    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.

  11. Konstantin Dikov
    Admin
    Konstantin Dikov avatar
    1803 posts

    Posted 28 May 2015 Link to this post

    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
  12. BoddaGetta
    BoddaGetta avatar
    5 posts
    Member since:
    Dec 2012

    Posted 28 May 2015 in reply to Konstantin Dikov Link to this post

    Thanks!  I will give it a try.
Back to Top
UI for ASP.NET Ajax is Ready for VS 2017