在.net core

时间:2017-08-04 04:12:34

标签: amazon-ses mailmessage asp.net-core-1.1

我正在使用带有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类的引用。以下是常规的.net框架:

Assembly assembly = typeof(SmtpClient).Assembly;

在.Net Core中,由于SmtpClient在MailKit中可用,我们引用它并给出以下错误:

'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?

2 个答案:

答案 0 :(得分:0)

**试试这个**

 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;
    }

答案 1 :(得分:0)

看起来在 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 });