Response as mail attachment
- by el ninho
using (MemoryStream stream = new MemoryStream())
{
compositeLink.PrintingSystem.ExportToPdf(stream);
Response.Clear();
Response.Buffer = false;
Response.AppendHeader("Content-Type", "application/pdf");
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.AppendHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.BinaryWrite(stream.GetBuffer());
Response.End();
}
I got this working fine. Next step is to send this pdf file to mail, as attachment
using (MemoryStream stream = new MemoryStream())
{
compositeLink.PrintingSystem.ExportToPdf(stream);
Response.Clear();
Response.Buffer = false;
Response.AppendHeader("Content-Type", "application/pdf");
Response.AppendHeader("Content-Transfer-Encoding", "binary");
Response.AppendHeader("Content-Disposition", "attachment; filename=test.pdf");
Response.BinaryWrite(stream.GetBuffer());
System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();
message.To.Add("[email protected]");
message.Subject = "Subject";
message.From = new System.Net.Mail.MailAddress("[email protected]");
message.Body = "Body";
message.Attachments.Add(Response.BinaryWrite(stream.GetBuffer()));
System.Net.Mail.SmtpClient smtp = new System.Net.Mail.SmtpClient("192.168.100.100");
smtp.Send(message);
Response.End();
}
I have problem with this line:
message.Attachments.Add(Response.BinaryWrite(stream.GetBuffer()));
Any help how to get this to work? Thanks