Archive

Archive for June, 2017

Send Selected Data from Gridview into Email in ASP.Net

June 29, 2017 Leave a comment

Dears,

Try this example to Send Selected Data from Gridview into Email


<%@ 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="gv_Category" runat="server" AutoGenerateColumns="False" CellPadding="4"
DataKeyNames="CategoryID" DataSourceID="sds_Category" EnableModelValidation="True"
ForeColor="#333333">
<AlternatingRowStyle BackColor="White" />
<Columns>
<asp:TemplateField>
<ItemTemplate>
<asp:CheckBox ID="chkSelect" runat="server" />
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category ID" InsertVisible="False" SortExpression="CategoryID">
<ItemTemplate>
<asp:Label ID="lblCategoryID" runat="server" Text='<%# Bind("CategoryID") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Category Name" SortExpression="CategoryName">
<ItemTemplate>
<asp:Label ID="lblCategoryName" runat="server" Text='<%# Bind("CategoryName") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Description" SortExpression="Description">
<ItemTemplate>
<asp:Label ID="lblDescription" runat="server" Text='<%# Bind("Description") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#2461BF" />
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#EFF3FB" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
</asp:GridView>
<asp:SqlDataSource ID="sds_Category" runat="server" ConnectionString="<%$ ConnectionStrings:NorthwindConnectionString %>"
SelectCommand="SELECT [CategoryID], [CategoryName], [Description] FROM [Categories]">
</asp:SqlDataSource>
</div>
<asp:Button ID="btn_SendEmail" runat="server" onclick="btn_SendEmail_Click"
Text="Send Email" />
</form>
</body>
</html>

In Code behind :


using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Net.Mail;
using System.Net;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using System.IO;
using System.Text;
using System.Net.Mime;

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

}
protected void btn_SendEmail_Click(object sender, EventArgs e)
{
string sAttchent = string.Empty;
string attachmentPath = string.Empty;

MailMessage mm = new MailMessage();
mm.From = new MailAddress("From@gmail.com");
mm.To.Add("To@gmail.com");

string sContent = string.Empty;
sContent = "
<table border='1'>
<tr>
<td>Category Name</td>
<td>Category Description</td>
<tr>";
foreach (GridViewRow row in gv_Category.Rows)
{
CheckBox chkSelect = (CheckBox)row.FindControl("chkSelect");
if (chkSelect.Checked)
{
int rowIndex = row.RowIndex;
string CategoryID = Convert.ToString(gv_Category.DataKeys[rowIndex].Values["CategoryID"]);
Label lblCategoryName = (Label)row.FindControl("lblCategoryName");
Label lblDescription = (Label)row.FindControl("lblDescription");
sContent += "
<tr>
<td>" + lblCategoryName.Text + "</td>
<td>" + lblDescription.Text + "</td>
<tr>";
string sCategoryName = string.Empty;
string sCategoryImage = string.Empty;
SqlCommand cmd = new SqlCommand();
cmd.CommandText = "SELECT * FROM Categories WHERE CategoryID = " + CategoryID;
cmd.Connection = new SqlConnection(ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
for (int i = 0; i < dt.Rows.Count; i++)
{
MemoryStream memoryStream = new MemoryStream();
byte[] contentAsBytes = (byte[])dt.Rows[i]["Picture"];
memoryStream.Write(contentAsBytes, 78, contentAsBytes.Length - 78);
// Set the position to the beginning of the stream.
memoryStream.Seek(0, SeekOrigin.Begin);
// Create attachment
ContentType contentType = new ContentType();
contentType.MediaType = "image/png";
contentType.Name = dt.Rows[i]["CategoryName"].ToString();
Attachment attachment = new Attachment(memoryStream, contentType);
// Add the attachment
mm.Attachments.Add(attachment);
}
}
}
}

mm.Subject = "Category Details ";
mm.IsBodyHtml = true;
mm.Body = "this content is in the body
" + sContent + "</table>
";
SmtpClient smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
NetworkCredential credential = new NetworkCredential();
credential.UserName = "sender@gmail.com";
credential.Password = "xxxxxx";
smtp.UseDefaultCredentials = true;
smtp.Credentials = credential;
smtp.Port = 587;
smtp.EnableSsl = true;
smtp.Send(mm);
}
}

In Web.Config add Connection String

 <connectionStrings>
<add name="NorthwindConnectionString" connectionString="Data Source=YASSER-PC;Initial Catalog=Northwind;Integrated Security=True" providerName="System.Data.SqlClient"/>
</connectionStrings>

Hope this helps

Good Luck.

Categories: ASP.Net