Using MemoryStream For Attachment

Skip Navigation LinksHome  /  Support  /  Forums  /  FireMail for .NET (v1)  /  Re: Using MemoryStream For Attachment

FireMail for .NET (v1) Forum

 Dec 13 2007 1:51 PM
I see you have a coding example for a System.IO.FileStream attachment, but I can't seem to find one for a System.IO.MemoryStream attachment. I don't want to have the attachment saved on my user's pc. I want to take the .pdf they create dynamically and attach it via MemoryStream. Thanks for your help!!
Posted by a ceTe Software moderator
Hello,

The Attachment object contructor can take either a byte array, a string (filepath) or it can take a System.IO.Stream.  The Stream object does not need to be a FileStream but rather could be any type derived form the System.IO.Stream class (i.e MemoryStream).

One additional thing to keep in mind here is that you need to make sure that you seek back to the beginning of the Stream before passing it into the Attachment constructor.

Thanks,
ceTe Software Support Team
Do you have a coding example for MemoryStream objects as attachments?

Thanks!
Nevermind -- I got it to work! Thanks for your help...
Posted by a ceTe Software moderator
Hello,

Following is the sample C# code that shows generated PDF document directly using the MemoryStream:

ceTe.DynamicPDF.Document document = new ceTe.DynamicPDF.Document();
ceTe.DynamicPDF.Page page = new ceTe.DynamicPDF.Page( PageSize.Letter, PageOrientation.Portrait, 54.0f );
                        
string text = "Hello ASPX C# World...\nFrom DynamicPDF™ Generator for .NET\nDynamicPDF.com";
Label label = new Label( text, 0, 0, 504, 100, Font.Helvetica, 18, TextAlign.Center );
                        
page.Elements.Add( label );
document.Pages.Add( page );
                        
System.IO.MemoryStream stream = new System.IO.MemoryStream();
PlainTextMessage ptMessage = new PlainTextMessage("xyz@xyz.com", "xyz@xyz.com", "Mail with Stream Attachment", " Files Attached");
ptMessage.Attachments.Add(document.Draw( stream ), "HelloWorld.pdf");
ptMessage.Send(smtpServer);

Thanks,
ceTe Software Support Team

All times are US Eastern Standard time. The time now is 12:30 AM.