Click here to Skip to main content
15,881,600 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
C#
public void SendCCEmail(int result)
        {
  
Document document = new Document(PageSize.A4, 88f, 88f, 10f, 10f);
               
            Font NormalFont = FontFactory.GetFont("Arial", 12, Font.NORMAL, Color.BLACK);
            using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
            {
               
                PdfWriter writer = PdfWriter.GetInstance(document,  memoryStream);
                
                Phrase phrase = null;
                PdfPCell cell = null;
                PdfPTable table = null;
                Color color = null;

                document.Open();

                //Header Table
                table = new PdfPTable(2);
                table.TotalWidth = 500f;
                table.LockedWidth = true;
                table.SetWidths(new float[] { 0.3f, 0.7f });

                //Company Logo
                cell = ImageCell("~/images/Logo.JPG", 30f, PdfPCell.ALIGN_CENTER);
                table.AddCell(cell);

------
----
                document.Add(table);
                document.Close();
                byte[] bytes = memoryStream.ToArray();
                memoryStream.Close();


                MailMessage Msg = new MailMessage();
                MailAddress fromMail = new MailAddress("frommailid");
                // Sender e-mail address.
                Msg.From = fromMail;
                // Recipient e-mail address.
                Msg.To.Add(new MailAddress("tomailid"));
                Msg.CC.Add(new MailAddress("ccmailid"));
                // Subject of e-mail
                Msg.Subject = "subject";
                Msg.Body = "Msg Body";
            
                Msg.Attachments.Add(new Attachment(new MemoryStream(bytes), "CC" + result + ".pdf"));
                Msg.IsBodyHtml = true;

                SmtpClient smtps = new SmtpClient("smtp.mailid.com", 2525);
                smtps.UseDefaultCredentials = false;
                smtps.Credentials = new NetworkCredential("username", "password");

                smtps.EnableSsl = true;
                smtps.Send(Msg);
}
Posted
Updated 11-Nov-14 1:47am
v2
Comments
Afzaal Ahmad Zeeshan 11-Nov-14 7:48am    
Doesn't seem to get any answer without any clarification of where you're stuck.
Afzaal Ahmad Zeeshan 11-Nov-14 7:59am    
Since the only problem is with saving the PDF file. You should consider saving the File from the memory directly.
Member 10989312 11-Nov-14 10:39am    
var output = new FileStream(Path.Combine("c:\\myPDF\\", "CC" + result + ".pdf"), FileMode.Create);
var writer1 = PdfWriter.GetInstance(document, output);

Dear Afzaal Thanx for your reply. The pdf file is stored in C:\MyPdf folder but not able to open.

1 solution

Try specifying the content type of the attachment:
C#
Attachment pdfAttachment = new Attachment(new MemoryStream(bytes), "CC" + result + ".pdf");
pdfAttachment.ContentType.MediaType = System.Net.Mime.MediaTypeNames.Application.Pdf;
Msg.Attachments.Add(pdfAttachment);



EDIT:
Based on the discussion in the comments below, the problem was an un-closed FileStream in the mail sending code, resulting in a "file in use" error.

The solution was to wrap the FileStream in a using block so that it was always closed.
 
Share this answer
 
v2
Comments
Member 10989312 11-Nov-14 10:42am    
The email is sent successfully with pdf attachment file but i am not able to store it in my local system folder.
Richard Deeming 11-Nov-14 10:44am    
Is that after making the change I suggested?

What is the error message when you try to save the attachment?
Member 10989312 11-Nov-14 10:52am    
when i open the pdf file i am getting "Adobe Reader could not open 'CC21.PDF' because it is either not a supported file type of because the file has been damaged(for example, it was sent as an email attachment and was't correctly decoded).
Richard Deeming 11-Nov-14 10:57am    
Try calling writer.Flush() after document.Close(), but before reading the contents of the MemoryStream.
Member 10989312 11-Nov-14 11:04am    
Yap.. I tried, now i am getting There was an error opening this document. This file is already open or in use by another application

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