在 .net 核心中将 MailMessage 转换为 MemoryStream
Posted
技术标签:
【中文标题】在 .net 核心中将 MailMessage 转换为 MemoryStream【英文标题】:Convert MailMessage to MemoryStream in .net core 【发布时间】:2018-01-11 21:10:29 【问题描述】:我正在使用带有 Amazon SES(SimpleEmail)的 .Net Core 1.2 来发送电子邮件(原始电子邮件)。
下面是我们在.net framework 4.5中使用的工作代码版本:
public MemoryStream ConvertMailMessageToMemoryStream(MailMessage message)
Assembly assembly = typeof(SmtpClient).Assembly;
Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
MemoryStream fileStream = new MemoryStream();
ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] typeof(Stream) , null);
object mailWriter = mailWriterContructor.Invoke(new object[] fileStream );
MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic);
sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] mailWriter, true, true , null);
MethodInfo closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic);
closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] , null);
return fileStream;
但在 .Net Core 1.2 中,我们无法获取 SmtpClient 和 System.Net.Mail.MailWriter 类 Assembly 的参考。以下是在常规 .net 框架中工作:
Assembly assembly = typeof(SmtpClient).Assembly;
在.Net Core中,由于MailKit中提供了SmtpClient,我们引用了它并给出如下错误:
'Type' does not contain a definition for 'Assembly' and no extension method 'Assembly' accepting a first argument of type 'Type' could be found (are you missing a using directive or an assembly reference?)
.Net Core 中有什么方法可以将 MailMessage 转换为 MemoryStream 吗?
【问题讨论】:
请提供错误的详细描述,否则很难在这里为您提供帮助。 Matt,请查看更新后的问题和详细要求 【参考方案1】:在 dotnet core 5.0 中似乎没有构造函数只接受一个参数,试试这个。
ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] typeof(Stream), typeof(bool) , null);
object mailWriter = mailWriterContructor.Invoke(new object[] fileStream,true );
【讨论】:
这段代码在 .NET 5 中为我工作,构造函数只是添加了一个额外的布尔参数【参考方案2】:**试试这个**
private static string ConvertMailMessageToMemoryStream(MailMessage message)
BindingFlags flags = BindingFlags.Instance | BindingFlags.NonPublic;
Assembly assembly = typeof(SmtpClient).Assembly;
Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter");
MemoryStream stream = new MemoryStream();
ConstructorInfo mailWriterConstructor = mailWriterType.GetConstructor(flags, null, new[] typeof(Stream) , null);
object mailWriter = mailWriterConstructor.Invoke(new object[] stream );
MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", flags);
sendMethod.Invoke(message, flags, null, new[] mailWriter, true, true , null);
var sr = new StreamReader(stream);
var str = sr.ReadToEnd();
MethodInfo closeMethod = mailWriter.GetType().GetMethod("Close", flags);
closeMethod.Invoke(mailWriter, flags, null, new object[] , null);
return str;
【讨论】:
类似的东西一直在为我工作,但不再在 NET5.0 中 这就是问题中提供的代码。以上是关于在 .net 核心中将 MailMessage 转换为 MemoryStream的主要内容,如果未能解决你的问题,请参考以下文章
将 System.Net.mail.MailMessage 保存为 .msg 文件
可以在 C# 中的委托上传递 System.Net.Mail.MailMessage 对象吗?
为啥我需要 Dispose 一个 System.Net.Mail.MailMessage 实例?