Lưu trữ

Archive for Tháng Mười Một, 2011

Mail sending with asp.net,c#,webconfig.

Tháng Mười Một 29, 2011 Bình luận về bài viết này
ASP.NET Code Snippet (Toggle Plain Text)
  1. SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings[“SMTP”], 25);
  2. //SmtpClient smtp = new SmtpClient(“localhost”);
  3. MailMessage message = new MailMessage();
  4. message.Subject = “Thank you for booking.”;
  5. //message.To.Add(d.email);
  6. message.To.Add(new MailAddress(“xxx@yyyy.com”));
  7. message.Body = “Body : Start Date & Time: ” + objBook.starttime + “<br> Customer Name: ” + name + “</br><br> CustomerPhone:” + phone + “</br><br> CustomerAddress:” + address + “</br><br> Source:” + source + “</br><br> Destination:” + dest;
  8. message.From = new MailAddress(“xxx@yyyy.com”);
  9. message.IsBodyHtml = true;
  10. //smtp.Host = “localhost”;
  11. try
  12. {
  13. smtp.Send(message);
  14. }
  15. catch (System.Exception e)
  16. {
  17. //TODO show message that driver email not valid or server problem
  18. MessageBox.Show(e.Message);
  19. }
  20. SmtpClient messagecust = new SmtpClient();
  21. MailMessage msgcust = new MailMessage();
  22. msgcust.Subject = “Thank you for booking.”;
  23. msgcust.To.Add(objBook.objCust.email);
  24. msgcust.Body = “Start Date & Time: ” + objBook.starttime + “<br> DriverName:'” + d.name + “</br><br> Driver PhoneNumber: ” + d.phone + “</br><br> Driver Email-id: ” + d.email;
  25. msgcust.From = new MailAddress(“xxx@yyyy.com”);
  26. msgcust.IsBodyHtml = true;
  27. messagecust.Host = ConfigurationManager.AppSettings[“SMTP”];
  28. //messagecust.Host = “localhost”;
  29. try
  30. {
  31. smtp.Send(msgcust);
  32. }
  33. catch (System.Exception e)
  34. {
  35. //TODO show message that customer email not valid or server problem
  36. MessageBox.Show(e.Message);
  37. }
  38. ////// web config code:
  39. <system.net>
  40. <mailSettings>
  41. <smtp>
  42. <!–<network host=”localhost”/>–>
  43. <network host=”mail.YYYYY.com” port=”25″/>
  44. </smtp>
  45. </mailSettings>
  46. </system.net>
Chuyên mục:ASP WEBFORM

Exporting WebPage to PDF…

Tháng Mười Một 29, 2011 Bình luận về bài viết này
ASP.NET Code Snippet (Toggle Plain Text)
  1. <%@ Page Language=”C#” AutoEventWireup=”true”
  2. CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
  3. <!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
  4. <head runat=”server”>
  5. <title>Untitled Page</title>
  6. </head>
  7. <body>
  8. <form id=”form1″ runat=”server”>
  9. <div>
  10. <asp:GridView ID=”GridView1″ runat=”server”
  11. AutoGenerateColumns=”False”
  12. DataSourceID=”SqlDataSource1″ Width=”257px”>
  13. <Columns>
  14. <asp:BoundField DataField=”Name”
  15. HeaderText=”Name”
  16. SortExpression=”Name” />
  17. <asp:BoundField DataField=”Location”
  18. HeaderText=”Location”
  19. SortExpression=”Location” />
  20. </Columns>
  21. </asp:GridView>
  22. <asp:SqlDataSource ID=”SqlDataSource1″
  23. runat=”server”
  24. ConnectionString=”<%$ ConnectionStrings:ConnectionString %>”
  25. SelectCommand=”SELECT [Name], [Location] FROM [Test]”>
  26. </asp:SqlDataSource>
  27. </div>
  28. <br />
  29. <asp:Button ID=”btnExport” runat=”server”
  30. OnClick=”btnExport_Click”
  31. Text=”Export to PDF” />
  32. </form>
  33. </body>
  34. </html>
  35. default.aspx.cs code:
  36. using System;
  37. using System.Data;
  38. using System.Configuration;
  39. using System.Web;
  40. using System.Web.Security;
  41. using System.Web.UI;
  42. using System.Web.UI.WebControls;
  43. using System.Web.UI.WebControls.WebParts;
  44. using System.Web.UI.HtmlControls;
  45. using iTextSharp.text;
  46. using iTextSharp.text.pdf;
  47. using iTextSharp.text.html;
  48. using System.IO;
  49. using System.Collections;
  50. using System.Net;
  51. public partial class _Default : System.Web.UI.Page
  52. {
  53. protected void Page_Load(object sender, EventArgs e)
  54. {
  55. }
  56. protected void btnExport_Click(object sender, EventArgs e)
  57. {
  58. HtmlForm form = new HtmlForm();
  59. form.Controls.Add(GridView1);
  60. StringWriter sw = new StringWriter();
  61. HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
  62. form.Controls[0].RenderControl(hTextWriter);
  63. string html = sw.ToString();
  64. Document Doc = new Document();
  65. //PdfWriter.GetInstance
  66. //(Doc, new FileStream(Request.PhysicalApplicationPath
  67. //+ “\\AmitJain.pdf”, FileMode.Create));
  68. PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath
  69. (Environment.SpecialFolder.Desktop)+ “\\VishalRane.pdf”, FileMode.Create));
  70. Doc.Open();
  71. Chunk c = new Chunk(“Export GridView to PDF Using iTextSharp \n”,FontFactory.GetFont(“Verdana”, 15));
  72. Paragraph p = new Paragraph();
  73. p.Alignment = Element.ALIGN_CENTER;
  74. p.Add(c);
  75. Chunk chunk1 = new Chunk(“By Vishal RAne, vishalrane50@gmail.com \n”,FontFactory.GetFont(“Verdana”, 8));
  76. Paragraph p1 = new Paragraph();
  77. p1.Alignment = Element.ALIGN_RIGHT;
  78. p1.Add(chunk1);
  79. Doc.Add(p);
  80. Doc.Add(p1);
  81. System.Xml.XmlTextReader xmlReader =
  82. new System.Xml.XmlTextReader(new StringReader(html));
  83. HtmlParser.Parse(Doc, xmlReader);
  84. Doc.Close();
  85. string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ “\\AmitJain.pdf”;
  86. ShowPdf(Path);
  87. }
  88. private void ShowPdf(string strS)
  89. {
  90. Response.ClearContent();
  91. Response.ClearHeaders();
  92. Response.ContentType = “application/pdf”;
  93. Response.AddHeader(“Content-Disposition”,”attachment; filename=” + strS);
  94. Response.TransmitFile(strS);
  95. Response.End();
  96. //Response.WriteFile(strS);
  97. Response.Flush();
  98. Response.Clear();
  99. }
  100. }
Chuyên mục:ASP WEBFORM