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)
-
SmtpClient smtp = new SmtpClient(ConfigurationManager.AppSettings[“SMTP”], 25);
-
//SmtpClient smtp = new SmtpClient(“localhost”);
-
MailMessage message = new MailMessage();
-
message.Subject = “Thank you for booking.”;
-
//message.To.Add(d.email);
-
message.To.Add(new MailAddress(“xxx@yyyy.com”));
-
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;
-
message.From = new MailAddress(“xxx@yyyy.com”);
-
message.IsBodyHtml = true;
-
//smtp.Host = “localhost”;
-
try
-
{
-
smtp.Send(message);
-
}
-
catch (System.Exception e)
-
{
-
//TODO show message that driver email not valid or server problem
-
MessageBox.Show(e.Message);
-
}
-
SmtpClient messagecust = new SmtpClient();
-
MailMessage msgcust = new MailMessage();
-
msgcust.Subject = “Thank you for booking.”;
-
msgcust.To.Add(objBook.objCust.email);
-
msgcust.Body = “Start Date & Time: ” + objBook.starttime + “<br> DriverName:'” + d.name + “</br><br> Driver PhoneNumber: ” + d.phone + “</br><br> Driver Email-id: ” + d.email;
-
msgcust.From = new MailAddress(“xxx@yyyy.com”);
-
msgcust.IsBodyHtml = true;
-
messagecust.Host = ConfigurationManager.AppSettings[“SMTP”];
-
//messagecust.Host = “localhost”;
-
try
-
{
-
smtp.Send(msgcust);
-
}
-
catch (System.Exception e)
-
{
-
//TODO show message that customer email not valid or server problem
-
MessageBox.Show(e.Message);
-
}
-
////// web config code:
-
<system.net>
-
<mailSettings>
-
<smtp>
-
<!–<network host=”localhost”/>–>
-
<network host=”mail.YYYYY.com” port=”25″/>
-
</smtp>
-
</mailSettings>
-
</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)
-
<%@ Page Language=”C#” AutoEventWireup=”true”
-
CodeFile=”Default.aspx.cs” Inherits=”_Default” %>
-
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN”
-
<html xmlns=”http://www.w3.org/1999/xhtml” >
-
<head runat=”server”>
-
<title>Untitled Page</title>
-
</head>
-
<body>
-
<form id=”form1″ runat=”server”>
-
<div>
-
<asp:GridView ID=”GridView1″ runat=”server”
-
AutoGenerateColumns=”False”
-
DataSourceID=”SqlDataSource1″ Width=”257px”>
-
<Columns>
-
<asp:BoundField DataField=”Name”
-
HeaderText=”Name”
-
SortExpression=”Name” />
-
<asp:BoundField DataField=”Location”
-
HeaderText=”Location”
-
SortExpression=”Location” />
-
</Columns>
-
</asp:GridView>
-
<asp:SqlDataSource ID=”SqlDataSource1″
-
runat=”server”
-
ConnectionString=”<%$ ConnectionStrings:ConnectionString %>”
-
SelectCommand=”SELECT [Name], [Location] FROM [Test]”>
-
</asp:SqlDataSource>
-
</div>
-
<br />
-
<asp:Button ID=”btnExport” runat=”server”
-
OnClick=”btnExport_Click”
-
Text=”Export to PDF” />
-
</form>
-
</body>
-
</html>
-
default.aspx.cs code:
-
using System;
-
using System.Data;
-
using System.Configuration;
-
using System.Web;
-
using System.Web.Security;
-
using System.Web.UI;
-
using System.Web.UI.WebControls;
-
using System.Web.UI.WebControls.WebParts;
-
using System.Web.UI.HtmlControls;
-
using iTextSharp.text;
-
using iTextSharp.text.pdf;
-
using iTextSharp.text.html;
-
using System.IO;
-
using System.Collections;
-
using System.Net;
-
public partial class _Default : System.Web.UI.Page
-
{
-
protected void Page_Load(object sender, EventArgs e)
-
{
-
}
-
protected void btnExport_Click(object sender, EventArgs e)
-
{
-
HtmlForm form = new HtmlForm();
-
form.Controls.Add(GridView1);
-
StringWriter sw = new StringWriter();
-
HtmlTextWriter hTextWriter = new HtmlTextWriter(sw);
-
form.Controls[0].RenderControl(hTextWriter);
-
string html = sw.ToString();
-
Document Doc = new Document();
-
//PdfWriter.GetInstance
-
//(Doc, new FileStream(Request.PhysicalApplicationPath
-
//+ “\\AmitJain.pdf”, FileMode.Create));
-
PdfWriter.GetInstance(Doc, new FileStream(Environment.GetFolderPath
-
(Environment.SpecialFolder.Desktop)+ “\\VishalRane.pdf”, FileMode.Create));
-
Doc.Open();
-
Chunk c = new Chunk(“Export GridView to PDF Using iTextSharp \n”,FontFactory.GetFont(“Verdana”, 15));
-
Paragraph p = new Paragraph();
-
p.Alignment = Element.ALIGN_CENTER;
-
p.Add(c);
-
Chunk chunk1 = new Chunk(“By Vishal RAne, vishalrane50@gmail.com \n”,FontFactory.GetFont(“Verdana”, 8));
-
Paragraph p1 = new Paragraph();
-
p1.Alignment = Element.ALIGN_RIGHT;
-
p1.Add(chunk1);
-
Doc.Add(p);
-
Doc.Add(p1);
-
System.Xml.XmlTextReader xmlReader =
-
new System.Xml.XmlTextReader(new StringReader(html));
-
HtmlParser.Parse(Doc, xmlReader);
-
Doc.Close();
-
string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ “\\AmitJain.pdf”;
-
ShowPdf(Path);
-
}
-
private void ShowPdf(string strS)
-
{
-
Response.ClearContent();
-
Response.ClearHeaders();
-
Response.ContentType = “application/pdf”;
-
Response.AddHeader(“Content-Disposition”,”attachment; filename=” + strS);
-
Response.TransmitFile(strS);
-
Response.End();
-
//Response.WriteFile(strS);
-
Response.Flush();
-
Response.Clear();
-
}
-
}
Chuyên mục:ASP WEBFORM