In response to my article on Custom Binary Serialization in .NET, a visitor asked me for a specific example of a custom serialization class of .NET’s MailMessage class.
As I mentioned in the article, I struggled with the MailMessage and MailAddress class because they cannot be serialized unless you use custom serialization (which can be a big pain because it takes quite a bit of time to create a custom class). I spent days trying to serialize the MailMessage class using conventional methods, only to find that there is no quick workaround for it other than custom serialization.
Download My Custom SerialMailMessage Class
If you’re interested in using my class, you can download the SerialMailMessage code here.
Within the zipped file, you will find the following classes:
- SerialMailMessageExample - I have included a simple example that utilizes the SerialMailMessage class.
- SerialMailMessage - A custom serializable class that uses both SerialMailAddress and SerialMailAttachment to create a serializable form of .NET’s MailMessage class.
- Serialization - A simple class to perform serialization and deserialization of serializable objects
- SerialMailAddress - A custom serializable class that replaces .NET’s MailAddress class (which is not serializable).
- SerialMailAttachment - A custom serializable class that replaces .NET’s MailAttachment class (which is not serializable).
Please keep in mind that my SerialMailMessage code was intended for a web service, so there were certain limitations built into the design of the classes. If you aren’t using these classes with a web service, you’ll have quite a bit more flexibility than I did. I highly encourage you to modify and improve on my code — there is a shameful lack of useful custom serializable classes available online, hence the reason for me having to write my own.
If you have any questions, please post them as comments so that everyone can benefit from a discussion.