I'm currently looking at serializing a MailMessage object in C #, and although there are several example variations on the network, they are serialized in binary format, which skips the IMO point.
My approach is that I would like to serialize MailMessage to the RFC2822 eml line, and the code below is what I came up with.
public string SerializeEmail(MailMessageArgs e) { string rfc822eml = ""; Guid g = Guid.NewGuid(); lock (g.ToString()) { System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(@"C:\tmpspool"); di.CreateSubdirectory(g.ToString()); string spoolDir = @"C:\tmpspool\" + g.ToString(); SmtpClient Client = new SmtpClient("localhost"); Client.DeliveryMethod = SmtpDeliveryMethod.SpecifiedPickupDirectory; Client.PickupDirectoryLocation = spoolDir; Client.Send(e.mailObj); var files = from file in Directory.EnumerateFiles(spoolDir) select file; string serializedEml = files.First(); rfc822eml = File.ReadAllText(serializedEml); File.Delete(serializedEml); Directory.Delete(spoolDir); } return rfc822eml; }
This is pretty unpleasant, but it really works. Ideally, I would create a new SMTPClient and add a Serialize function that would automatically return the rfc822 line without pressing the file system.
It seems to me that I canβt track the SMTPClient.Send function in Visual Studio, this βidealβ way to make things a little more complicated.
I already figured out how to deserialize the RFC message by adding some minor changes to the Peter Huber POP3MimeClient and POP3MailClient classes so that I can deserialize the file on my hard drive or the string back to MailMessage.
What is imposing on me is that I really should be able to serialize MailMessaage using a stream and write the stream to a line-or file of my choice, instead of going around the house to create GUID-based folders, as the code above does and reads the contents of the file back to the line ...
Any pointers on how I could make this more elegant would be greatly appreciated.
Thanks.
c # serialization rfc822 mailmessage rfc2822
PaulV
source share