Is there a way to serialize an .Net MailMessage object

I am trying to write a proc that will take a MailMessage object as a parameter and split it into parts for storing the object, body, addresses, addresses and attachments (hard part) in the database, so the letter may be sent at some point in the future.

My first take on this was to rip out the parts I needed and store them in a database, and this works great, except for the attachments. I can’t figure out how to get through the collection, and then actually do something with them.

Is there an easy way to serialize a MailMessage object that will actually use the contents of the attachments with it?

Am I doing all this wrong? Has anyone done this before?

+4
source share
2 answers

This is actually not a good way. So, I continued my initial method of looping through the MailMessage object and getting all the information I cared about. For the attachments, which were the hardest part, each application has a ContentStream, and I just read that stream and wrote it to disk, saved the file name, and then I can recreate it when I want to send it.

I have not fully tested this method, so I do not recommend it to anyone else, but it seems to be the best solution in our particular case.

+1
source

If I had to guess which strategy I would use, it would be for each attachment to turn it into an array of bytes, and then put these byte arrays and message details into an XML document, and then pass this XML document to the database as parameter.

0
source

Source: https://habr.com/ru/post/1307564/


All Articles