Web design, SQL, and .NET for the young, up-and-coming developer Dot Net Yuppie

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:

  1. SerialMailMessageExample - I have included a simple example that utilizes the SerialMailMessage class.
  2. SerialMailMessage - A custom serializable class that uses both SerialMailAddress and SerialMailAttachment to create a serializable form of .NET’s MailMessage class.
  3. Serialization - A simple class to perform serialization and deserialization of serializable objects
  4. SerialMailAddress - A custom serializable class that replaces .NET’s MailAddress class (which is not serializable).
  5. 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.

6 Responses to “Serializable MailMessage Class”

  1. Lars Eggan Says:

    Do you have any sample code for the web service you created that is using the serialized mailmessage class?

    [Reply]

    DotNetYuppie reply on April 23, 2008:

    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?

    [Reply]

  2. Lars Eggan Says:

    What did you do ensure security in your web service?
    Preventing spam and so on? Using https?

    [Reply]

    DotNetYuppie reply on April 24, 2008:

    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.

    [Reply]

  3. Lars Eggan Says:

    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?

    [Reply]

    DotNetYuppie reply on April 26, 2008:

    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

    [Reply]

Leave a Reply