I am able to create an excel attachment by dumping it into a memorystream and attach it to an email.
However, when I try it as a PDF it doesn't work and throws an error that it's not a supported file type or has been damaged (for example it was sent as an email attachment and wasn't correctly decoded).
I am certain that I did not decode it correctly. Here is what I have, the export works great, the email attachment does not.
How can I get the PDF into a memory stream to add it as an attachment to an email?
However, when I try it as a PDF it doesn't work and throws an error that it's not a supported file type or has been damaged (for example it was sent as an email attachment and wasn't correctly decoded).
I am certain that I did not decode it correctly. Here is what I have, the export works great, the email attachment does not.
How can I get the PDF into a memory stream to add it as an attachment to an email?
protected void RadGrid1_PdfExporting(object sender, GridPdfExportingArgs e) { StringBuilder customHTML = new StringBuilder(); // customHTML.Append("<p style='color: red; font-weight: bold; font-size: 11pt;'>Offer Summary</p>"); customHTML.Append("<table style='font-size: 10px; font-family:Arial Unicode MS;width:500px;'>"); customHTML.Append("<colgroup><col style='width: 200px; white-space:nowrap;' /><col style='text-align:left;' /></colgroup>"); // ColGroup = Number of <td> in a row is needed for Telerik export to PDF customHTML.Append("<tr><td>Customer: " + LabelCustomerName.Text + "</td><td>Agent: " + LabelAgentName.Text + " </td></tr>"); customHTML.Append("<tr><td>Customer ID: " + LabelCustomerId.Text + "</td><td>Group ID: " + LabelGroupId.Text + " </td></tr>"); customHTML.Append("<tr><td>Account Executive: " + LabelAccountExecutive.Text + "</td><td>Credit Status: " + LabelCreditStatus.Text + " </td></tr>"); customHTML.Append("<tr><td>Account Manager: " + LabelAccountManager.Text + "</td><td>Review Date: " + LabelReviewDate.Text + " </td></tr>"); customHTML.Append("</table>"); customHTML.Append("<p></p>"); e.RawHTML = customHTML + e.RawHTML; MemoryStream attachmentMemoryStream = new MemoryStream(new ASCIIEncoding().GetBytes(e.RawHTML)); // Test Email SmtpClient smtpClient = new SmtpClient(Convert.ToString(ConfigurationManager.AppSettings["SERVER_EMAIL"])); string authType = "Basic"; MailMessage email = new MailMessage(); StringBuilder body = new StringBuilder(); string emaURL = Convert.ToString(ConfigurationManager.AppSettings["BASE_URL"]); email.From = new MailAddress("someemail@gmail.com"); email.CC.Add("myemail@gmail.com"); email.Subject = "File_" + LabelCustomerName.Text + " - " + Convert.ToString(ConfigurationManager.AppSettings["SERVER_NAME"]) + " - Group " + LabelGroupId.Text; email.Attachments.Add(new Attachment(attachmentMemoryStream, "File_" + LabelCustomerName.Text + "_" + DateTime.Today.ToShortDateString() + ".pdf")); email.Body = body.ToString(); email.IsBodyHtml = true; NetworkCredential nc = new NetworkCredential(RijndaelAlgorithm.Decrypt(Convert.ToString(ConfigurationManager.AppSettings["CredentialU"])), RijndaelAlgorithm.Decrypt(Convert.ToString(ConfigurationManager.AppSettings["CredentialP"]))); smtpClient.Credentials = nc.GetCredential(Convert.ToString(smtpClient), 25, authType); smtpClient.Send(email); }