// Create data table
public void Tempdt()
{
DataTable
dt = new DataTable();
dt.Columns.AddRange(new DataColumn[3]
{
new
DataColumn("OrderId"),
new
DataColumn("Product"),
new
DataColumn("Quanity")});
dt.Rows.Add(101, "Name1", 5);
dt.Rows.Add(102, "Name2", 2);
dt.Rows.Add(103, "Name3", 12);
dt.Rows.Add(104, "Name4", 9);
dt.Rows.Add(104, "Name5", 9);
Save_send_Email(dt);
}
// save and send pdf
private void Save_send_Email(DataTable
dt)
{
using (StringWriter sw = new
StringWriter())
{
using
(HtmlTextWriter hw = new HtmlTextWriter(sw))
{
string
companyName = "ASPSnippets";
int
orderNo = 2303;
StringBuilder
sb = new StringBuilder();
sb.Append("<table width='100%' cellspacing='0'
cellpadding='2'>");
sb.Append("<tr><td align='center'
style='background-color: #18B5F0' colspan = '2'><b>Order
Sheet</b></td></tr>");
sb.Append("<tr><td colspan =
'2'></td></tr>");
sb.Append("<tr><td><b>Order
No:</b>");
sb.Append(orderNo);
sb.Append("</td><td><b>Date:
</b>");
sb.Append(DateTime.Now);
sb.Append(" </td></tr>");
sb.Append("<tr><td colspan = '2'><b>Company
Name :</b> ");
sb.Append(companyName);
sb.Append("</td></tr>");
sb.Append("</table>");
sb.Append("<br />");
sb.Append("<table border = '1'>");
sb.Append("<tr>");
foreach
(DataColumn column in
dt.Columns)
{
sb.Append("<th style = 'background-color:
#D20B0C;color:#ffffff'>");
sb.Append(column.ColumnName);
sb.Append("</th>");
}
sb.Append("</tr>");
foreach
(DataRow row in
dt.Rows)
{
sb.Append("<tr>");
foreach
(DataColumn column in
dt.Columns)
{
sb.Append("<td>");
sb.Append(row[column]);
sb.Append("</td>");
}
sb.Append("</tr>");
}
sb.Append("</table>");
StringReader
sr = new StringReader(sb.ToString());
Document
pdfDoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
HTMLWorker
htmlparser = new HTMLWorker(pdfDoc);
using
(MemoryStream memoryStream = new MemoryStream())
{
PdfWriter
writer = PdfWriter.GetInstance(pdfDoc,
memoryStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
byte[]
bytes = memoryStream.ToArray();
memoryStream.Close();
MailMessage
mm = new MailMessage("from@gmail.com", "to@gmail.com");
mm.Subject = "iTextSharp PDF";
mm.Body = "iTextSharp PDF Attachment";
mm.Attachments.Add(new Attachment(new MemoryStream(bytes),
"iTextSharpPDF.pdf"));
mm.IsBodyHtml = true;
SmtpClient
smtp = new SmtpClient();
smtp.Host = "smtp.gmail.com";
smtp.EnableSsl = true;
NetworkCredential
NetworkCred = new NetworkCredential();
NetworkCred.UserName = "from@gmail.com";
NetworkCred.Password = "passowrd";
smtp.UseDefaultCredentials
= true;
smtp.Credentials =
NetworkCred;
smtp.Port = 587;
smtp.Send(mm);
File.WriteAllBytes(Server.MapPath("~/Files/ddd1.pdf"),bytes);
}
}
}
}