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.
using System.Net.Mail;
using System.Text;
using System.IO;
Now in Button Click event write this code :
send_mail() Definition :
GridViewToHtml() definition :
Now browse and send mail and output will be like this one -:
Sometime one can encountered by this error -:
RegisterForEventValidation can only be called during Render();
This means that either you have forgot to override VerifyRenderingInServerForm in code behind or EventValidation is true.
So the solution is set EventValidation to false and must override VerifyRenderingInServerForm method.
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;
Now in Button Click event write this code :
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);
}
}
{
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;
}
{
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;
}
GridViewToHtml() definition :
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.
}
{
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.
}
Now browse and send mail and output will be like this one -:
Sometime one can encountered by this error -:
RegisterForEventValidation can only be called during Render();
This means that either you have forgot to override VerifyRenderingInServerForm in code behind or EventValidation is true.
So the solution is set EventValidation to false and must override VerifyRenderingInServerForm method.
Hi,
ReplyDeleteExcellent!! work
helped me a lot
thank you
vr
Hi,
ReplyDeletecan i know what is gvPArtyStatement in this code is it a predefined method or function? what exactly this do?
and this code is for send mail to gmail account i want to know how to send the mail to official id like internal id(abcd@hp.com).
Here gvPArtyStatement is id of Gridview.And to send mail from internal id apply these changes to code -:
Deleteclient.Host = "74.5.21.6";//Relevent ip address
client.EnableSsl = false;
client.Credentials = new System.Net.NetworkCredential("abcd@hp.com", "pwd");
You are AWESOME!
ReplyDeleteThank a Lot Buddy, Its Working...
ReplyDeletehow to send data repeater contents to mail
ReplyDeleteThanks a lot for this code it works great !!!
ReplyDeletecan we use this code in user control(ascx)..I Had Tried...But Showing Error "'userControl:VerifyRenderingInServerForm(System.Web.UI.Control)':no suitable method found to override"..How Can I Rectify it In User Conrol..Pls Help Me.......
ReplyDeleteUse this method to override
Deletepublic override void VerifyRenderingInServerForm(Control control)
{
return;
/* Confirms that an HtmlForm control is rendered for the specified ASP.NET server control at run time. */
}
I have requirement of sending the grid data with meeting request.
ReplyDeleteSo, I have created meeting request with ContentType as Calender and mail body with ContentType as HTML. But in the mail Not able to see the gridlines to the grid data. How I can resolve his?
I use the same code but while debugging its work however after deployment it is not working
ReplyDeleteWOW it works no need to Add anything, just change the gridID, thanks hey.
ReplyDeleteThanks..!! it is working fyn..!!
ReplyDeleteHi can u tell me how to send tabs in working formt in mails. i have 2 tabs in html page and that i want to send in mail but its not woorking.
ReplyDeleteHi, i've tried sending a gridview with imagefield tag, i can send email but images r not visible in it. If you can help me in this ?
ReplyDeleteDear , i have one column of Grid is as a image , send i send mail, it suceed but now images shown in gmail Account..pls share code for image sending , thanks
ReplyDeletevery useful
ReplyDeletethank you
hey can i send image containing gridview in mail.
ReplyDelete