namespace Swan
{
using System;
using System.IO;
using System.Net.Mail;
using System.Reflection;
///
/// Extension methods.
///
public static class SmtpExtensions
{
private static readonly BindingFlags PrivateInstanceFlags = BindingFlags.Instance | BindingFlags.NonPublic;
///
/// The raw contents of this MailMessage as a MemoryStream.
///
/// The caller.
/// A MemoryStream with the raw contents of this MailMessage.
public static MemoryStream ToMimeMessage(this MailMessage @this)
{
if (@this == null)
throw new ArgumentNullException(nameof(@this));
var result = new MemoryStream();
var mailWriter = MimeMessageConstants.MailWriterConstructor.Invoke(new object[] { result });
MimeMessageConstants.SendMethod.Invoke(
@this,
PrivateInstanceFlags,
null,
MimeMessageConstants.IsRunningInDotNetFourPointFive ? new[] { mailWriter, true, true } : new[] { mailWriter, true },
null);
result = new MemoryStream(result.ToArray());
MimeMessageConstants.CloseMethod.Invoke(
mailWriter,
PrivateInstanceFlags,
null,
Array.Empty