ABCpdf Attaches Doc to Email

I used ABDpdf to render the pdf and feed it to the browser, but I'm wondering if I can attach the resulting pdf to an email. Has anyone ever done this?

I hope there is a way that doesn't require me to save the pdf file in the temp directory, then attach the file and then delete it.

+2


source to share


2 answers


Meklarian is right, but one little thing to point out is that after you've saved the pdf to your stream, you'll want to reset your stream's position back to 0. Otherwise, the attachment that is sent will be all foo-denied ...

(It took me about two hours to figure this out. Hope to help someone else save some time.)



    //Create the pdf doc
    Doc theDoc = new Doc();
    theDoc.FontSize = 12;
    theDoc.AddText("Hello, World!");

    //Save it to the Stream
    Stream pdf = new MemoryStream();
    theDoc.Save(pdf);
    theDoc.Clear();

    //Important to reset back to the begining of the stream!!!
    pdf.Position = 0; 

    //Send the message
    MailMessage msg = new MailMessage();
    msg.To.Add("you@you.com");
    msg.From = new MailAddress("me@me.com");
    msg.Subject = "Hello";
    msg.Body = "World";
    msg.Attachments.Add(new Attachment(pdf, "MyPDF.pdf", "application/pdf"));
    SmtpClient smtp = new SmtpClient("smtp.yourserver.com");
    smtp.Send(msg);

      

+9


source


According to the documentation on the ABCpdf PDF support site, there is an overload of the Doc () object that supports persisting to a stream. With this function you can save the results of the generated PDF without explicitly writing to disk using the MemoryStream class.

ABCpdf PDF Component for .NET: Doc.Save ()
MemoryStream (System.IO) @MSDN

When creating a MemoryStream, you can stream the stream to any email provider that supports creating attachments from the stream. MailMessage in System.Net.Mail supports this.



MailMessage Class (System.Net.Mail) @MSDN MailMessage.Attachments
@MSDN Property
Attachments Class @MSDN
Attachments @MSDN Constructor

Finally, if you haven't used the MailMessage class before, use the SmtpClient class to send your message along the way.

SmtpClient Class (System.Net.Mail)

+3


source







All Articles