Click here to Skip to main content
15,889,335 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
iTextSharp.text.html.simpleparser.StyleSheet styles = new    iTextSharp.text.html.simpleparser.StyleSheet();
         PdfPTable table = new PdfPTable(3);
         table.TotalWidth = 400f;
         //styles.LoadStyle("pdf", "border", "1");
         styles.LoadStyle("pdf", "size", "4");
         styles.LoadStyle("pdf", "font", "12px");
         string invoicename = lblcustomername.Text + " " + lbldispatchdate.Text;
          Response.ContentType = "application/pdf";
         Response.AddHeader("content-disposition", "attachment;filename=" + invoicename + ".pdf");
         Response.Cache.SetCacheability(HttpCacheability.NoCache);

         StringWriter stringWriter = new StringWriter();
         HtmlTextWriter htmlTextWriter = new HtmlTextWriter(stringWriter);

         divM.RenderControl(htmlTextWriter);
         StringReader sr = new StringReader(stringWriter.ToString());
         Document doc = new Document(PageSize.A2, 8f, 8f, 8f, 0f);

         HTMLWorker htmlparser = new HTMLWorker(doc);
         htmlparser.SetStyleSheet(styles);
         MemoryStream memoryStream = new MemoryStream();
         PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream);

         // PdfWriter.GetInstance(doc, Response.OutputStream);
         doc.Open();
         htmlparser.Parse(sr);
         Response.Write(doc);
         writer.CloseStream = false;
         doc.Close();
         memoryStream.Position = 0;

         var mail = new MailMessage();
         mail.From = new MailAddress("www@gmail.com");
         mail.Attachments.Add(new Attachment(memoryStream, invoicename + ".pdf"));
         DataTable dtmailid = new DataTable();
         objFeedRetPL.customername = lblcustomername.Text.ToString();
         dtmailid = objFeedRetBAL.Getdistributormailid(objFeedRetPL);
         if (dtmailid.Rows.Count > 0)
         {
             //   mail.To.Add(dtmailid.Rows[0]["mailid"].ToString());
         }

         mail.Subject = "Please Find Attached File";
         mail.To.Add("xxx@gmail.com");
         _smtp.Host = "mail.sreego.com";
         _smtp.Timeout = 40000;
         mail.Priority = MailPriority.High;
         _smtp.Send(mail);

         Response.End();

I have tried using this code.It is sending pdf file as attachement But ,showing save/open/cancel Dailog box. I don't need this dailog box here,I want to send only attchement and don't want to download or open this file after sending file as attchement.How can i prevent this dailog box ,Please any one help me.
Posted
Updated 9-Nov-14 19:43pm
v3
Comments
rmksiva 10-Nov-14 2:03am    
Hi,

What is the Purpose of " Response.Write(doc); and Response.End " code in this scenario ?Once Remove and Check !

1 solution

I wonder if its because of the way you add the attachment without specifying the content-type - I did a quick look but the docco is 'bleh'

you could try this (strung together, may need a bit of poking into shape) :-

System.Net.Mime.ContentType ct = new System.Net.Mime.ContentType(System.Net.Mime.MediaTypeNames.Application.Pdf);
System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(memoryStream, ct);
attach.ContentDisposition.FileName = invoicename + ".pdf";
mail.Attachments.Add(attach);


or maybe this

mail.Attachments.Add(new Attachment(memorystream, invoicename+".pdf", "application/pdf"));


(I'd check if "application/pdf" was legal before using it)
 
Share this answer
 
v2
Comments
sr@22 10-Nov-14 2:19am    
its not working.still showing save/open/cancel dailog box.
sr@22 10-Nov-14 2:20am    
Successfully sending pdf file as attachment but showing dailog box.I don't want this dailog box.
Garth J Lancaster 10-Nov-14 3:46am    
things to try :-

1) step through the code with the debugger, and show exactly what line is popping up the dialog

2) create a pdf or use an existing one, but not one created by your application, and use that instead as the attachment and see if the issue is still there
sr@22 10-Nov-14 4:07am    
Already did that.I am not getting which statement is showing popup exactly.
Garth J Lancaster 10-Nov-14 4:29am    
1) what is _smtp ? (ie, where is it defined and which implementation is it)

2) comment out all but these lines and try with a dummy file

var mail = new MailMessage();
mail.From = new MailAddress("www@gmail.com");
mail.Attachments.Add(new Attachment(memoryStream, invoicename + ".pdf"));
mail.Subject = "Please Find Attached File";
mail.To.Add("xxx@gmail.com");
_smtp.Host = "mail.sreego.com";
_smtp.Timeout = 40000;
mail.Priority = MailPriority.High;
_smtp.Send(mail);

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