Click here to Skip to main content
15,912,756 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hai all,

How to send a mail with an attachemnt in asp.net c#..the attachmnet is a pdf and its created at the time of sening mail.pls help me..

Thanks n Advnce
Aatif Ali-Bangalore
Posted

 
Share this answer
 
Comments
Aatif Ali from Bangalore 13-Jun-13 1:01am    
hai frnd,i want to send pdf as attachment,this pdf is created just before sending the mail using the following code..

string src = string.Empty;
string AbsolutePath = string.Empty;
_Filter.Clear();
_Filter.Add("Id",hdnCertificateId.Value);
_CertificatesEntityCollection = CertificatesManager.SelectFiltered(_Filter);

ScriptManager.RegisterStartupScript(this, typeof(string), "script123", "AsPdf()", true);
Response.Clear();
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
//Response.AddHeader("content-disposition", "attachment;filename=" + _CertificatesEntityCollection.First().Name + ".pdf");
Response.AddHeader("Content-Disposition", "attachment; filename=\"" + _CertificatesEntityCollection.First().Name + "\"");



Response.Cache.SetCacheability(HttpCacheability.NoCache);
StringWriter sw = new StringWriter();
HtmlTextWriter hw = new HtmlTextWriter(sw);

pnlCertificate.RenderControl(hw);
pnlCertificate.Style.Add("border", "2px solid black");
pnlCertificate.Style.Add("margin-top", "60px");
pnlCertificate.Style.Add("text-align", "justify");

src = sw.ToString();
AbsolutePath = HttpContext.Current.Request.Url.Scheme + "://" + HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.ApplicationPath;
src = src.Replace("img src=\"/", string.Format("img src=\"{0}", AbsolutePath));
StringReader sr = new StringReader(src);
//Document pdfDoc = new Document(PageSize.A4_LANDSCAPE, 20f, 10f, 50f, 0f);
Document pdfDoc = new Document();
pdfDoc.SetPageSize(iTextSharp.text.PageSize.A4.Rotate());

HTMLWorker htmlparser = new HTMLWorker(pdfDoc);
PdfWriter.GetInstance(pdfDoc, Response.OutputStream);
pdfDoc.Open();
htmlparser.Parse(sr);
pdfDoc.Close();
Response.Write(pdfDoc);
Response.End();

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900