This example makes u enable to understand how to email server controls information like Gridview as it is.
Now I am considering that we have a web form which contains a GridView (containing Data) and a button
which will be used to send email.
At first include these namespaces to your code behind.
Now in Button Click event write this code :
GridViewToHtml() definition :
Now I am considering that we have a web form which contains a GridView (containing Data) and a button
which will be used to send email.
At first include these namespaces to your code behind.
using System.Net.Mail;
using System.Text;
using System.IO;
protected void ibMail_Click(object sender, ImageClickEventArgs
e)
{
string to = "anukana@symtechindia.net";
string From = "mafire5@gmail.com";
string subject = "Balance
Detail";
string Body = "Dear
sir ,<br> Plz Check d Attachment <br><br>";
Body
+= GridViewToHtml(gvPArtyStatement); //Elaborate this
function detail later
Body
+= "<br><br>Regards,<br>Anukana";
bool send = send_mail(to, From, subject, Body);//Elaborate this function detail later
if (send == true)
{
string CloseWindow = "alert('Mail
Sent Successfully!');";
ClientScript.RegisterStartupScript(this.GetType(),
"CloseWindow", CloseWindow, true);
}
else
{
string CloseWindow = "alert('Problem
in Sending mail...try later!');";
ClientScript.RegisterStartupScript(this.GetType(),
"CloseWindow", CloseWindow, true);
}
}
send_mail() Definition :
public bool send_mail(string to, string
from, string subject, string
body)
{
MailMessage msg = new
MailMessage(from, to);
msg.Subject = subject;
AlternateView
view;
SmtpClient client;
StringBuilder msgText = new
StringBuilder();
msgText.Append("
<html><body><br></body></html>
<br><br><br> "
+ body);
view = AlternateView.CreateAlternateViewFromString(msgText.ToString(),
null, "text/html");
msg.AlternateViews.Add(view);
client = new SmtpClient();
client.Host = "smtp.gmail.com";
client.Port = 587;
client.Credentials = new System.Net.NetworkCredential("jhalpharegi@gmail.com",
"Your_password");
client.EnableSsl = true; //Gmail works on Server Secured Layer
client.Send(msg);
bool k = true;
return k;
}
private string GridViewToHtml(GridView gv)
{
StringBuilder sb = new
StringBuilder();
StringWriter sw = new
StringWriter(sb);
HtmlTextWriter hw = new
HtmlTextWriter(sw);
gv.RenderControl(hw);
return sb.ToString();
}
public override void VerifyRenderingInServerForm(Control control)
{
//Confirms that an HtmlForm control is rendered for the
specified ASP.NET server control at run time.
}