Skip to content

Latest commit

 

History

History
76 lines (53 loc) · 2 KB

Readme.md

File metadata and controls

76 lines (53 loc) · 2 KB

Introduction

This repository contains a .NET assembly which adds a couple of extension methods to the MailMessage class of the System.Net.Mail namespace. It also contains a serializable replica of the MailMessage class.

Usage & Examples

To use the library add the S22.Mail.dll assembly to your project references in Visual Studio. The SerializableMailMessage class implements conversion operators to allow for implicit conversion between SerializableMailMessage and MailMessage objects.

using System;
using System.IO;
using System.Net.Mail;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using S22.Mail;

namespace Test {
	class Program {
		static void Main() {
			MailMessage msg = MyMailMessage();

			IFormatter formatter = new BinaryFormatter();
			using(MemoryStream s = new MemoryStream()) {
				// Serialize MailMessage to memory stream
				formatter.Serialize(s, (SerializableMailMessage)message);

				// Rewind stream and deserialize MailMessage
				s.Seek(0, SeekOrigin.Begin);
				MailMessage Tmp = (SerializableMailMessage)formatter.Deserialize(s)

				Console.WriteLine(Tmp.Subject);
				Console.WriteLine(Tmp.Body);
			}
		}

		static MailMessage MyMailMessage() {
			MailMessage m = new MailMessage("[email protected]", "[email protected]");

			m.Subject = "Hello World";
			m.Body = "This is just a test";
			m.Attachments.Add(new Attachment("Test.cs""));

			return m;
		}
	}
}

Extension Methods

System.Net.Mail.MailMessage

  • Load(Stream stream)
  • Load(string name)
  • Save(Stream stream)
  • Save(string name)

System.Net.Mail.Attachment

  • SaveAs(string name)

Credits

This library is copyright © 2012 Torben Könke.

License

This library is released under the MIT license.

Bug reports

Please send your bug reports and questions to [email protected] or create a new issue on the GitHub project homepage.