public static byte[] ConvertMailMessageToMemoryStream(MailMessage message) { Assembly assembly = typeof(SmtpClient).Assembly; Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter"); //var outStream = new ClosableMemoryStream(); using (var internalStream = new ClosableMemoryStream()) { ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor( BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { typeof(Stream) }, null); object mailWriter = mailWriterContructor.Invoke(new object[] { internalStream }); MethodInfo sendMethod = typeof(MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic); sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] { mailWriter, true }, null); //internalStream.Write(new byte[] { 13, 10 }, 0, 2); //internalStream.Position = 0; //string data = null; //using (var reader = new StreamReader(internalStream)) //{ // data = reader.ReadToEnd(); // //internalStream.Close(); //} MethodInfo closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic); closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] {}, null); //CRLF = new byte[] { 13, 10 }; //internalStream.Write(new byte[] { 13, 10 }, 0, 2); internalStream.Position = 0; var buffer = new byte[internalStream.Length]; var i = internalStream.Read(buffer, 0, (int)internalStream.Length); return(buffer); //return Convert.ToBase64String(buffer, 0, i); //string data = null; //using (var reader = new StreamReader(internalStream)) //{ // //data = reader.ReadToEnd(); // //internalStream.Close(); // return reader.re //} //return data; } //return data; }
public static byte[] ConvertMailMessageToMemoryStream(MailMessage message) { Assembly assembly = typeof(SmtpClient).Assembly; Type mailWriterType = assembly.GetType("System.Net.Mail.MailWriter"); //var outStream = new ClosableMemoryStream(); using (var internalStream = new ClosableMemoryStream()) { ConstructorInfo mailWriterContructor = mailWriterType.GetConstructor( BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {typeof (Stream)}, null); object mailWriter = mailWriterContructor.Invoke(new object[] { internalStream }); MethodInfo sendMethod = typeof (MailMessage).GetMethod("Send", BindingFlags.Instance | BindingFlags.NonPublic); sendMethod.Invoke(message, BindingFlags.Instance | BindingFlags.NonPublic, null, new[] {mailWriter, true}, null); //internalStream.Write(new byte[] { 13, 10 }, 0, 2); //internalStream.Position = 0; //string data = null; //using (var reader = new StreamReader(internalStream)) //{ // data = reader.ReadToEnd(); // //internalStream.Close(); //} MethodInfo closeMethod = mailWriter.GetType().GetMethod("Close", BindingFlags.Instance | BindingFlags.NonPublic); closeMethod.Invoke(mailWriter, BindingFlags.Instance | BindingFlags.NonPublic, null, new object[] {}, null); //CRLF = new byte[] { 13, 10 }; //internalStream.Write(new byte[] { 13, 10 }, 0, 2); internalStream.Position = 0; var buffer = new byte[internalStream.Length]; var i = internalStream.Read(buffer, 0, (int)internalStream.Length); return buffer; //return Convert.ToBase64String(buffer, 0, i); //string data = null; //using (var reader = new StreamReader(internalStream)) //{ // //data = reader.ReadToEnd(); // //internalStream.Close(); // return reader.re //} //return data; } //return data; }