0

I'm working with a C# class for sending emails that expects a List of HttpPostedFiles as one of its method's parameters (for file attachment to an email). I have a file (on the file system) that I want to attach to an email in my program and I'm using this class to do it. My problem (I think) is that in the below code:

var constructorInfo = typeof(HttpPostedFile).GetConstructors(BindingFlags.NonPublic | BindingFlags.Instance)[0];    
HttpPostedFile postedFile = (HttpPostedFile)constructorInfo.Invoke(new object[] { pathString + "Policy_Assoc_home_computer_V8.pdf", "application/pdf", httpStream });

httpStream is supposed to be of type HttpInputStream. I don't know how to instantiate such a thing. I've tried the following:

string pathString = Server.MapPath("~/Policy/");
                FileStream stream = new FileStream(pathString + "Policy_Assoc_home_computer_V8.pdf", FileMode.Open);
                Assembly systemWebAssembly = typeof(HttpPostedFile).Assembly;
                Type typeHttpRawUploadedContent = systemWebAssembly.GetType("System.Web.HttpRawUploadedContent");
                Type typeHttpInputStream = systemWebAssembly.GetType("System.Web.HttpInputStream");
                Type[] uploadedParams = { typeof(int), typeof(int) };
                Type[] streamParams = { typeHttpRawUploadedContent, typeof(int), typeof(int) };
                object uploadedContent = typeHttpRawUploadedContent
                    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, uploadedParams, null)
                    .Invoke(new object[] { stream.Length, stream.Length });
                object httpStream = (Stream)typeHttpInputStream
                    .GetConstructor(BindingFlags.NonPublic | BindingFlags.Instance, null, streamParams, null)
                    .Invoke(new object[] { uploadedContent, 0, stream.Length });

but it throws an error on the line that instantiates uploadedContent:

object of type System.Int64 cannot be converted to System.Int32

How do I do this?

Melanie
  • 3,021
  • 6
  • 38
  • 56
  • Why are you using reflection? – Heretic Monkey Sep 06 '19 at 20:55
  • If I shouldn't, how else can I do this? I'm not familiar with the HttpPostedFile class, and all the examples I've seen use Reflection. – Melanie Sep 06 '19 at 20:56
  • 1
    If you have to use reflection you're probably doing something wrong. Also I don't get what this has to do with sending an attachment with an email. There are plenty of easy ways to do this. The error is just due to a missing cast from long to int, but you should probably just find a better way to do whatever it is you actually want to do. See SmtpClient or MailKit. – George Helyar Sep 06 '19 at 21:04
  • Okay, I see what the problem is. The real question is, why are you trying to create an instance of `HttpPostedFile`? It's an internal type used by ASP.NET to represent a file that's been posted to a website. Sounds like an [XY Problem](http://xyproblem.info) to me. I would use a different overload or class as I think the one you're using is not the correct one... – Heretic Monkey Sep 06 '19 at 21:05
  • 1
    I'm using it because the class I'm using for sending the email (I didn't write this class) expects a List of HttpPostedFiles as the attachment to the email (if there are attachments), and I want to attach a file to the email. – Melanie Sep 06 '19 at 21:31

0 Answers0