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:
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.
April 23rd, 2008 at 9:49 am
Do you have any sample code for the web service you created that is using the serialized mailmessage class?
April 23rd, 2008 at 4:36 pm
The only code that I can really release is the code that I have included in this post. The ‘SerialMailMessageExample’ should be sufficient to explain the usage of the classes.
Do you have any specific questions that I could help you with instead?
April 24th, 2008 at 6:31 pm
What did you do ensure security in your web service?
Preventing spam and so on? Using https?
April 24th, 2008 at 9:31 pm
Currently, I employ an IP white list. I have a running list of IP addresses that are allowed to send e-mail (via checking Me.Context.Request.UserHostAddress in the asmx.vb file). If a connected client isn’t on the white list, then the web service throws an exception and won’t let them send. A white list can be done manually (as I’ve described above), or you can actually configure IIS to do the same thing.
A white list is only practical if you only intend to have your own web server send out e-mails. If you want your web service to be accessible to specific users on any computer/IP address, then you’d have to employ some other mechanism of security.
.NET web services allow for active directory user authentication, but you could also do something as simplistic as a username/password over HTTPS or require a client to use an application GUID that must match up with an existing GUID that you have on your side.
April 25th, 2008 at 1:46 pm
I got my web service to work with your Serializable MailMessage Class. Excellent code.
So the only concertn now is the security. I thought of using SSL, but must admit I’m not that experienced in this area. Is there an easy way to use HTTPS? without a certificate?
April 26th, 2008 at 11:28 pm
Glad to hear it! SSL is an excellent choice (if not the only choice). SSL is possible, but it requires a certificate. Certificates can be free if you self-sign them — I’ve written an article (http://dotnetyuppie.com/2008/04/26/enabling-ssl-https-for-iis-in-windows-xp/) describing the process.
If you’re in a live environment with clients using the web service, you’ll need to get it signed (and unfortunately pay money). To my knowledge, you can’t do SSL unless you have a certificate that is self-signed (and unverified) or signed by a third-party (and costs money).
Hope this helps,
DNY