Я подумал, что это может быть полезно для кого-то, кто будет искать ответ на тот же вопрос. Мне удалось преодолеть проблему, написав отражение System.Net.Mail.MailMessage.Send () следующим образом.
public static string SaveToTemp(this MailMessage Message)
{
SmtpClient smtp = new SmtpClient();
string fileName = Guid.NewGuid().ToString() + ".eml";
string fileNameWithPath = Path.Combine(smtp.PickupDirectoryLocation, fileName);
Assembly assembly = typeof(SmtpClient).Assembly;
Type _mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
using (FileStream _fileStream = new FileStream(fileNameWithPath, FileMode.Create))
{
// Get reflection info for MailWriter contructor
ConstructorInfo _mailWriterContructor =
_mailWriterType.GetConstructor(
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new Type[] { typeof(Stream) },
null);
// Construct MailWriter object with our FileStream
object _mailWriter = _mailWriterContructor.Invoke(new object[] { _fileStream });
// Get reflection info for Send() method on MailMessage
MethodInfo _sendMethod =
typeof(MailMessage).GetMethod(
"Send",
BindingFlags.Instance | BindingFlags.NonPublic);
// Call method passing in MailWriter
_sendMethod.Invoke(
Message,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { _mailWriter,true, true },
null);
// Finally get reflection info for Close() method on our MailWriter
MethodInfo _closeMethod =
_mailWriter.GetType().GetMethod(
"Close",
BindingFlags.Instance | BindingFlags.NonPublic);
// Call close method
_closeMethod.Invoke(
_mailWriter,
BindingFlags.Instance | BindingFlags.NonPublic,
null,
new object[] { },
null);
}
return fileNameWithPath;
}
Звонящий:
var mail = Mailer.Example_Mail()
mail.To.Add("some@somedomain.com");
var fileName = mail.SaveToTemp(); // Instead of mail.Send();