如何将ASP.NET表作为电子邮件发送而不是附件?

时间:2011-05-03 11:04:23

标签: c# asp.net email

我有一个通过ASP.NET表创建的报告,我想在邮件正文中发送包含此报告的电子邮件,而不是包含表格布局和内容的附件。如何在c#中完成?谢谢Dov

4 个答案:

答案 0 :(得分:2)

我在这个网站http://authors.aspalliance.com/stevesmith/articles/dotnetemailwebsite.asp的一篇文章中汇总了以下代码,我在这里找到了一个问题“用asp.net创建复杂的html电子邮件的最佳方法,怎么样?”,并且从这篇文章中如何在ASP.NET 2.0 http://www.dotnetcurry.com/ShowArticle.aspx?ID=92中打印。 我使用asp.net面板,这样我只能获取页面的一部分而不是整个页面,这样你就可以发送一个表,在我的情况下,无需发送激活发送电子邮件或任何其他部分的按钮我不想发送的页面。 注意:发送的控件的任何样式属性必须直接设置为控件而不是通过cssclass。 这是代码:

//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();

使用这段代码,我发送了一个生成并填充后台代码的asp.net表,它运行良好。

答案 1 :(得分:0)

您可以向asp.net页面发出http请求,然后您可以将响应嵌入到邮件正文中。 您应该将邮件正文格式设置为html。

答案 2 :(得分:0)

您可以使用System.Mail.Net

在.NET中发送HTML电子邮件

这样的东西
//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);

请注意,正文是HTML,因此您可以包含表格。虽然不同的电子邮件客户端支持不同数量的HTML,但要小心格式化,最好保持简单。

答案 3 :(得分:0)

尝试以下代码段

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 */
   }