开发者

How can I send an ASP.NET table as an Email message not as an attachment?

I have a report creat开发者_开发知识库ed thru an ASP.NET table and I want to send an email message containing this report in the body of the message not as an attachment including the table layout and content. How can this be done in c#? Thank you Dov


I put together the following code from an article at this site http://authors.aspalliance.com/stevesmith/articles/dotnetemailwebsite.asp which I found thru a question asked here "Best way to create complex html email message with asp.net, how?", and from this article How to Print in ASP.NET 2.0 http://www.dotnetcurry.com/ShowArticle.aspx?ID=92. I use an asp.net panel so that I can get only parts of the page and not the whole page so that you can send a table, in my case, without having to send the button which activates sending the email or any other part of the page which I don't want to send. NOTE: Any style properties for controls sent must be set directly to the controls and not thru cssclass. This is the code:

//Here I extract html of the control to be sent
StringWriter stringWrite = new StringWriter();
System.Web.UI.HtmlTextWriter htmlWrite = new System.Web.UI.HtmlTextWriter(stringWrite);
//pnlRpt is an asp.net panel containing the controls to be sent
pnlRpt.RenderControl(htmlWrite);
string htmlStr = stringWrite.ToString();

//Here I send the message whith the html of the table
MailMessage msg = new MailMessage();
msg.From = new MailAddress("EmailOfSender");
msg.To.Add("emailOfReceiver");
msg.Subject = "your subject";
msg.Body = htmlStr;
msg.IsBodyHtml = true;
SmtpClient smtp = new SmtpClient(mailServer);
smtp.Credentials = new System.Net.NetworkCredential(userName, usePass);
smtp.Send(msg);
msg.Dispose();

With this code I sent an asp.net table generated and populated in background code and it worked perfectly.


You can make http request to asp.net page then, you can embed the response in body of mail. You should set mail Body format as html.


You can send HTML email in .NET using System.Mail.Net

Something like

//create the mail message
MailMessage mail = new MailMessage();

//set the addresses
mail.From = new MailAddress("me@mycompany.com");
mail.To.Add("you@yourcompany.com");

//set the content
mail.Subject = "This is an email";
mail.Body = "this is a sample body with html in it. <b>This is bold</b> <font color=#336699>This is blue</font>";
mail.IsBodyHtml = true;

//send the message
SmtpClient smtp = new SmtpClient("address of email server possibly localhost");
smtp.Send(mail);

Notice the body is HTML so you can include a table. Be careful with formatting though different email clients support different amounts of HTML and it is best to keep things simple.


Try below code snippet

protected void SendEmail(object sender, EventArgs e)
   {
       try
       {
           using (StringWriter sw = new StringWriter())
           {
               using (HtmlTextWriter hw = new HtmlTextWriter(sw))
               {
                  // GridView5.DataBind();
                   GridView5.RenderControl(hw);//this is my gridview name
                   StringReader sr = new StringReader(sw.ToString());
                   MailMessage mm = new MailMessage("gowdhaman92@gmail.com",                    "gowdhaman92@gmail.com");//From and To Mail id's
                   mm.Subject = "Quotation from INDIAN Departmental store,Udumalpet";
                   mm.Body = "INDIAN Departmental store,Udumalpet:<hr />" + sw.ToString();
                   mm.IsBodyHtml = true;
                   SmtpClient smtp = new SmtpClient();
                   smtp.Host = "smtp.gmail.com";
                   smtp.EnableSsl = true;
                   System.Net.NetworkCredential NetworkCred = new System.Net.NetworkCredential();
                   NetworkCred.UserName = "gowdhaman92@gmail.com";
                   NetworkCred.Password = "perumalpudur";
                   smtp.UseDefaultCredentials = true;
                   smtp.Credentials = NetworkCred;
                   smtp.Port = 587;
                   smtp.Send(mm);
                   ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "Alert message", "alert('mail sent');", true);
               }
           }
       }
       catch (Exception)
       {
           ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Mail not sent!!!');", true);
       }
   }

       public override void VerifyRenderingInServerForm(Control control)
   {
       /* Verifies that the control is rendered */
   }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜