Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save robinvanderknaap/3811cd71e3a21a5a8a829db04c70035b to your computer and use it in GitHub Desktop.
Save robinvanderknaap/3811cd71e3a21a5a8a829db04c70035b to your computer and use it in GitHub Desktop.
using System.IO;
using System.Net.Mail;
using System.Reflection;
using System.Text;
namespace Infrastructure.MailMessageExtensions
{
public static class MailMessageExtensions
{
public static string ToEml(this MailMessage message)
{
var assembly = typeof(SmtpClient).Assembly;
var mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
using (var memoryStream = new MemoryStream())
{
// Get reflection info for MailWriter contructor
var mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null);
// Construct MailWriter object with our FileStream
var mailWriter = mailWriterContructor.Invoke(new object[] { memoryStream });
// Get reflection info for Send() method on MailMessage
var sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
// Call method passing in MailWriter
sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true, true }, null);
// Finally get reflection info for Close() method on our MailWriter
var closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
// Call close method
closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] { }, null);
return Encoding.ASCII.GetString(memoryStream.ToArray());
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment