Link to home
Start Free TrialLog in
Avatar of shelbyinfotech
shelbyinfotech

asked on

Adding an attachment to email vb.net from upload httppostedfile.

I am currently taking the uploaded file and saving it to disk.  Then, I attach it to an email.  This works however, sometimes, (inconsistent) it claims the file is in use by another process and won't save it to disk.  I had the bright idea that I could attach the filestream directly to  the mailmessage.attachment.add. I have not been able to get this to work.  I actually succeeded but the file that was attached had nothing in it.  It had the right name though. The command I tried was:

mailmessage.attachments.add(new attachment(uploadname.postedfile.inputstream, uploadname.postedfile.filename))

Any ideas? I've found lots of stuff about how to save the file to disk and then attach that but I'm trying to get away from that.
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

found this code online:
using (var stream = new MemoryStream())
using (var writer = new StreamWriter(stream))   //convert memory stream to Writer
using (var mailClient = new SmtpClient("localhost", 25)) // setup SMTP client
using (var message = new MailMessage("from@example.com", "to@example.com", "Subject", "Body")) // create mail message
{
    writer.WriteLine("1,2,3");
    writer.Flush();
    stream.Position = 0;     // read from the start of what was written
    message.Attachments.Add(new Attachment(stream, "filename.csv", "text/csv"));  // stream, the filename, mime type.
    mailClient.Send(message);
}

Open in new window

Avatar of shelbyinfotech
shelbyinfotech

ASKER

Here you know you are writing a csv and are supplying the mime type. I don't know this.  Can be a pdf, a word doc, anything the user uploads. Is not the postedfile.inputstream already a memory stream? vb.net would also be helpful.
SOLUTION
Avatar of Kyle Abrahams, PMP
Kyle Abrahams, PMP
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
the code I provided in https://www.experts-exchange.com/questions/29008033/Adding-an-attachment-to-email-vb-net-from-upload-httppostedfile.html?notificationFollowed=185654205&anchorAnswerId=42043308#a42043308 shows similar code to the proposed solution.

Specifically taking a memory stream and creating an attachment from it.  How the information got to the memory stream is the only difference.
Well, the piece that was missing was the part I was asking about.  I too had found the post about the memory stream. I don't care.  If it's that big of a deal to you, you can claim the solution.  I needed to do that other part and I did figure that out.  If you want the points for googling it and copying and pasting something that referred to what I was talking about, I'm not going to stand in your way.  Let the moderators sort it out.