Answered by:
Implement Carbon Copy (CC) on Sending PDF Attachment Email

Question
-
User1228272764 posted
hi
I have developed a function that can handle report by render HTML to PDF with some library and directly sending its content as an attachment to the recipient. Here is My code :
//cek state aktivitas bp sampai mana var doc = new Document(); MemoryStream memoryStream = new MemoryStream(); PdfWriter writer = PdfWriter.GetInstance(doc, memoryStream); //added********** StringWriter sw = new StringWriter(); HtmlTextWriter w = new HtmlTextWriter(sw); print.RenderControl(w); string htmWrite = sw.GetStringBuilder().ToString(); htmWrite = Regex.Replace(htmWrite, "</?(a|A).*?>", ""); htmWrite = htmWrite.Replace("\r\n", ""); StringReader reader = new StringReader(htmWrite); HTMLWorker htmlparser = new HTMLWorker(doc); //added************ doc.Open(); var titleFont = FontFactory.GetFont("Arial", 18, Font.BOLD); var subTitleFont = FontFactory.GetFont("Arial", 14, Font.BOLD); //font BaseFont bfTimes = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1252, false); Font times = new Font(bfTimes, 10, Font.NORMAL); Font timesubhead = new Font(bfTimes, 12, Font.NORMAL); //****add img string imageURL = Server.MapPath(".") + "/honda.jpg"; iTextSharp.text.Image jpg = iTextSharp.text.Image.GetInstance(imageURL); //Resize image depend upon your need jpg.ScaleToFit(160f, 140f); jpg.SetAbsolutePosition(420, 690);//jpg.Alignment = 6; x/y doc.Add(jpg); if (cabang == "112") { doc.Add(new Paragraph(" ")); doc.Add(new Paragraph("Honda Mugen", timesubhead)); doc.Add(new Paragraph("PT. Mitra Usaha Gentaniaga", times)); doc.Add(new Paragraph("Jl. Raya Pasar Minggu No.10, Jakarta 12740 - Indonesia", times)); doc.Add(new Paragraph("Telp : (021) 797 3000 (Show Room), 797 2000 (Bengkel)", times)); doc.Add(new Paragraph("Fax : (021) 79738341", times)); doc.Add(new Paragraph("Web: www.hondamugen.co.id", times)); //add by img } else { doc.Add(new Paragraph(" ")); doc.Add(new Paragraph("Honda Mugen", timesubhead)); doc.Add(new Paragraph("PT. Mitra Usaha Gentaniaga", times)); doc.Add(new Paragraph("Jl. Lingkar Luar Barat, Puri Kembangan Jakarta Barat 11610 - Indonesia", times)); doc.Add(new Paragraph("Telp : (021) 5835 8000(Show Room), (021) 5835 9000 (Bengkel)", times)); doc.Add(new Paragraph("Fax : (021) 5835 7942", times)); doc.Add(new Paragraph("Web: www.hondamugen.co.id", times)); } htmlparser.Parse(reader); writer.CloseStream = false; doc.Close(); memoryStream.Position = 0; string penerima = TxtEmailReciever.Text; string penerima_2 = TxtEmailReciever.Text; //mailMessage.CC.Add(new MailAddress(cc)); //Adding CC email Id MailMessage mm = new MailMessage(penerima, penerima) { Subject = "Laporan Aktivitas BP - " + DateTime.Now.ToShortDateString() + " - " + wo + "", IsBodyHtml = true, Body = "Berikut Telampir Laporan Aktivitas Body Paint untuk No. WO = " + wo + "." }; mm.Attachments.Add(new Attachment(memoryStream, "ReportAktivitasBP_" + DateTime.Now.ToShortDateString() + "_" + wo + ".pdf")); SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, Credentials = new NetworkCredential("hmugen1991@gmail.com", "112m128p") }; smtp.Send(mm); ScriptManager.RegisterClientScriptBlock(this, this.GetType(), "alertMessage", "alert('Berhasil Mengirim Laporan BP ke Email!')", true);
my code working perfectly but how am I supposed to add CC on this function?
I tried this way but still not working:
mm.Attachments.Add(new Attachment(memoryStream, "ReportAktivitasBP_" + DateTime.Now.ToShortDateString() + "_" + wo + ".pdf")); SmtpClient smtp = new SmtpClient { Host = "smtp.gmail.com", Port = 587, EnableSsl = true, Credentials = new NetworkCredential("hmugen1991@gmail.com", "112m128p") }; mm.CC.Add(new MailAddress(penerima_2)); //Adding CC email Id smtp.Send(mm);
can you guys help me?
Friday, May 24, 2019 3:48 AM
Answers
-
User753101303 posted
Hi,
What if not using the same mail address for both To and CC ? My guess is that the mail server is checking for that (to avoid sending the same message to the same destination twice) and removes the duplicate address from CC.
Just try with another address than what you have in To and see what happens.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 24, 2019 11:19 AM
All replies
-
User475983607 posted
Pretty simple, just populate the CC address property.
https://docs.microsoft.com/en-us/dotnet/api/system.net.mail.mailmessage.cc?view=netframework-4.8
Friday, May 24, 2019 11:06 AM -
User753101303 posted
Hi,
What if not using the same mail address for both To and CC ? My guess is that the mail server is checking for that (to avoid sending the same message to the same destination twice) and removes the duplicate address from CC.
Just try with another address than what you have in To and see what happens.
- Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
Friday, May 24, 2019 11:19 AM -
User1228272764 posted
yes, I have tried, and well done it's working. just add additional email CC as second address with (,) between 2 address. thank you for the good suggestions!
Monday, May 27, 2019 8:06 AM