Response as mail attachment

Posted by el ninho on Stack Overflow See other posts from Stack Overflow or by el ninho
Published on 2012-07-03T15:01:47Z Indexed on 2012/07/03 15:15 UTC
Read the original article Hit count: 184

Filed under:
|
|
|
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

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET