Convert MailMessage to MemoryStream in .net core
Asked Answered
S

2

7

I am using the .Net Core 1.2 with Amazon SES(SimpleEmail) to send the Emails(Raw Emails).

Below is the working code version we have used in .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;

    }

But in .Net Core 1.2 we are unable to get reference for class Assembly of SmtpClient and System.Net.Mail.MailWriter. Below is working in regular .net framework:

Assembly assembly = typeof(SmtpClient).Assembly;

In .Net Core, since SmtpClient is available in MailKit, we have referenced it and gives the following error:

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

Is there any way in .Net Core to convert the MailMessage to MemoryStream?

Superclass answered 4/8, 2017 at 4:12 Comment(2)
Please provided a detailed description of the error otherwise it's very hard to help you here.Gamesome
Matt, please check the updated question and detailed requirementSuperclass
P
2

Looks like there is no constructor accepting only one argument in dotnet core 5.0, try this.

ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor(BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream), typeof(bool) }, null);

    object mailWriter = mailWriterContructor.Invoke(new object[] { fileStream,true });
Parasitic answered 23/7, 2021 at 15:10 Comment(1)
This code worked for me in .NET 5, the constructor just added an additional boolean parameterAlbumose
D
-1

** try this**

 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;
    }
Dagda answered 28/3, 2019 at 12:38 Comment(2)
Something like this has been working for me but no longer in NET5.0Polanco
This is literally the code provided in the question.Wort

© 2022 - 2024 — McMap. All rights reserved.