Thursday, December 16, 2010

Send Email GridView as an Excel attachment C#, asp.net

Following is the complete code for Send Email  GridView data  as an Excel attachment, any question please post comment.

<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
            DataSourceID="SqlDataSource1">
            <Columns>
                <asp:BoundField DataField="AccessNo" HeaderText="AccessNo"
                    SortExpression="AccessNo" />
                <asp:BoundField DataField="SerialNo" HeaderText="SerialNo"
                    SortExpression="SerialNo" />
                <asp:BoundField DataField="ActivationDate" HeaderText="ActivationDate"
                    SortExpression="ActivationDate" />
                <asp:BoundField DataField="Status" HeaderText="Status"
                    SortExpression="Status" />
                <asp:BoundField DataField="StatusDate" HeaderText="StatusDate"
                    SortExpression="StatusDate" />
                <asp:BoundField DataField="OrderType" HeaderText="OrderType"
                    SortExpression="OrderType" />
                <asp:BoundField DataField="OrderNo" HeaderText="OrderNo"
                    SortExpression="OrderNo" />
            </Columns>
        </asp:GridView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
            ConnectionString="<%$ ConnectionStrings:JDE_RKConnectionString %>"
            SelectCommand="SELECT top 10 [AccessNo], [SerialNo], [ActivationDate], [Status], [StatusDate], [OrderType], [OrderNo] FROM jde_rk.dbo.a276185">
        </asp:SqlDataSource>
   
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
   
    </div>
    </form>
</body>
</html>

 


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Text;
using System.IO;

using System.Net.Mail;

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
     
   
    protected void Button1_Click(object sender, EventArgs e)
    {
        sndmailattch();
    }


    void sndmailattch()
    {
    
        StringWriter sw = new StringWriter();
        HtmlTextWriter htw = new HtmlTextWriter(sw);
        GridView1.RenderControl(htw);
        MailMessage mail = new MailMessage();
        mail.IsBodyHtml = true;
        mail.To.Add(new MailAddress("rajeevk@visli.net"));
        mail.Subject = "Inventory Report";

        System.Text.Encoding enc = System.Text.Encoding.ASCII;
        byte[] myByteArray = enc.GetBytes(sw.ToString());
        System.IO.MemoryStream memAtt = new System.IO.MemoryStream(myByteArray, false);
        mail.Attachments.Add(new Attachment(memAtt,"inventory.xls"));
        mail.Body = "Inventory report brother..";

        SmtpClient smtp = new SmtpClient();
        mail.From = new MailAddress("rajeevk@visli.net", "Rajeev Kumar");
        smtp.Host = "mail.visli.net";
       
        //smtp.UseDefaultCredentials = false;
        //smtp.Credentials = new System.Net.NetworkCredential(@"UserName", "Password");
        //smtp.EnableSsl = true;
      
        smtp.Send(mail);

    }

    public override void VerifyRenderingInServerForm(Control control)
    {
        /* Confirms that an HtmlForm control is rendered for the specified ASP.NET
           server control at run time. */
    }
}

2 comments:

Matt Penman said...

Thanks!

Unknown said...

smtp.Send(mail)

the command stop here