我有一个通过ASP.NET表创建的报告,我想在邮件的正文中发送包含此报告的电子邮件,而不是作为包括表格布局和内容的附件。这怎么可以在C#中完成?谢谢Dov如何将ASP.NET表格作为电子邮件发送而不是附件?
回答
我把以下代码放在这个网站的文章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表,并将其完美地工作。
然后你可以把http请求发送到asp.net页面,你可以在邮件正文中嵌入响应。 您应该将邮件正文格式设置为html。
您可以在.NET中使用System.Mail.Net
发送HTML电子邮件喜欢的东西
//create the mail message
MailMessage mail = new MailMessage();
//set the addresses
mail.From = new MailAddress("[email protected]");
mail.To.Add("[email protected]");
//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,但请注意格式,最好让事情简单些。
是不是一个常规的HTML表格被认为保持简单的事情?谢谢 – 2011-05-04 12:32:03
试试下面的代码片段
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("[email protected]", "[email protected]");//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 = "[email protected]";
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 */
}
请解释你的代码 – muzaffar 2015-09-04 10:10:00
- 1. git发送电子邮件:发送带有文件的电子邮件作为附件而不是补丁
- 2. 在asp.net发送电子邮件发送密码而不是电子邮件
- 3. ASP.NET MVC发送电子邮件附件
- 4. 将上传的文件作为电子邮件附件发送
- 5. 制作电子邮件表格发送电子邮件
- 6. 如何发送JsonNode作为电子邮件附件
- 7. PHP的电子邮件表格不发送电子邮件
- 8. 附件电子邮件表格!?如何创建一个文件上传表格,用电子邮件发送
- 9. PHP电子邮件表格发送数组而不是变量
- 10. 发送HTML电子邮件 - 如何将.html文件附加到电子邮件?
- 11. 电子邮件表格不发送
- 12. PHP发送电子邮件显示内容不作为附件
- 13. 如何将电子邮件的格式作为短信发送
- 14. 谷歌电子表格作为pdf电子邮件附件
- 15. 如何将保存的文本文件作为电子邮件附件发送
- 16. TESTNG:如何将电子邮件报告作为电子邮件发送
- 17. 保存文件并作为电子邮件附件发送
- 18. 用附件发送电子邮件到多封电子邮件
- 19. SendGrid电子邮件API,发送电子邮件附件
- 20. 如何从ASP.Net发送电子邮件
- 21. 如何将JSP输出的全部内容作为HTML电子邮件附件通过电子邮件发送
- 22. ASP通过附件发送电子邮件表格
- 23. 使用PEAR发送附件的电子邮件表格
- 24. 如何拍照并将其作为电子邮件附件发送到iOS中
- 25. php发送电子邮件附件
- 26. 发送带附件的电子邮件
- 27. 发送带附件的电子邮件
- 28. WP7.1发送电子邮件附件
- 29. 通过电子邮件发送部分页面作为文件附件Asp.Net
- 30. Java邮件API - 发送附件为com.sun.mail.util.BASE64DecoderStream的电子邮件
你能给HTTP请求和嵌入在邮件正文中的反应的例子吗?谢谢。 – 2011-09-13 08:45:44