【发布时间】:2015-09-04 18:40:49
【问题描述】:
我有一个通过 ASP.NET 表创建的报告,我想在邮件正文中发送一封包含此报告的电子邮件,而不是作为包含表格布局和内容的附件。如何在 c# 中做到这一点?谢谢多夫
【问题讨论】:
我有一个通过 ASP.NET 表创建的报告,我想在邮件正文中发送一封包含此报告的电子邮件,而不是作为包含表格布局和内容的附件。如何在 c# 中做到这一点?谢谢多夫
【问题讨论】:
我将以下代码放在此站点 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 表,它运行良好。
【讨论】:
然后您可以向asp.net 页面发出http 请求,您可以将响应嵌入到邮件正文中。 您应该将邮件正文格式设置为 html。
【讨论】:
您可以使用 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,但请注意格式化,最好保持简单。
【讨论】:
试试下面的代码sn-p
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 */
}
【讨论】: