How to attach XDocument to MailMessage in C #?

I have a C # form, the input of which I am going to turn into an XML document for attaching to an email sent to myself. So far, I have an XDocument, which I believe is complete, and I also figured out how to attach .txt to MailMessage with:

MailMessage.Attachments.Add(new Attachment("[...]\test.txt")); 

I understand that I could use

 XDocument.Save("[...]\formData.xml"); 

to save the file and then

 MailMessage.Attachments.Add(new Attachment("[...]\formData.xml")); 

load and attach it, but it seems wasteful.

Does anyone have a better way around this? I suppose there should be a way to attach an XDocument without going into a file system like this ...

+4
source share
1 answer

Check out the Attachment class; you can add attachments in several other ways than just from a file, for example, from a stream or the contents of a string:

http://msdn.microsoft.com/en-us/library/system.net.mail.attachment.aspx

+3
source

All Articles