简体   繁体   中英

Attach file to email without creating the file

I have a webpage that generates an XmlDocument, and I have to send it by email.

For now, I save the XMLDocument to the disc using the Save method, then add it to my mailMessage attachments. Therefore, I'll have to delete those files if I don't want them to start accumulating.

What I would like, is a way to add the XML file as an attachment without having to actually save it on my disk.

Is that possible and, if it is, how can I do that ?

You can use the Attachment constructor that takes a Stream . Save the XmlDocument to a MemoryStream , and pass that stream to the attachment.

(assuming you use the built-in SmtpClient/MailMessage API)

Yes, you can create your xml instance using XmlWriter to a MemoryStream instance and append it to your email using the overload constructor of Attachment class.

http://msdn.microsoft.com/en-us/library/ms162617(v=vs.110).aspx

http://msdn.microsoft.com/en-us/library/ab7hb4y5(v=vs.110).aspx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM