tags:

views:

49

answers:

4

Hi all,

I'm using the code below in a ASP.NET page to send a file via email from our users home computer to a mailbox that is used for receiving work that needs photocopying. The code below works fine when sending a file within our network but fails when our users are at home and connected via our SSL VPN, there appears to be a bug in our VPN where it doesn't allow the file to be temporarily saved on the webserver before being sent via email. Can anyone offer any other suggestions on how to attach a file to a ASP.NET page and have the file sent via email without storing it on the web server? Many thanks Jane.

MailMessage mail = new MailMessage();
        mail.From = txtFrom.Text;
        mail.To = txtTo.Text;
        mail.Cc = txtFrom.Text;
        mail.Subject = txtSubject.Text;
        mail.Body = "test"
        mail.BodyFormat = MailFormat.Html;

        string strdir = "E:\\TEMPforReprographics\\"; //<-------PROBLEM AREA
        string strfilename = Path.GetFileName(txtFile.PostedFile.FileName);

        try
        {
            txtFile.PostedFile.SaveAs(strdir + strfilename);
            string strAttachment = strdir + strfilename;

            mail.Attachments.Add(new MailAttachment(strdir + strfilename));
            SmtpMail.SmtpServer = "172.16.0.88";
            SmtpMail.Send(mail);
            Response.Redirect("Thanks.aspx", true);
        }
        catch
        {
           Response.Write("An error has occured sending the email or uplocading the file.");
        }
        finally
        {

        }
+2  A: 

If you use the classes in the System.Net.Mail namespace, the Attachment class in there supports streams, so assuming you can read it in to memory as a stream first you can then add that to the attachment, that way you never have to store any files.

More information (and a sample) here:

http://msdn.microsoft.com/en-us/library/6sdktyws.aspx

ho1
A: 

Use string strdir = Path.GetTempPath();?

Naeem Sarfraz
A: 

Can anyone offer any other suggestions on how to attach a file to a ASP.NET page and have the file sent via email without storing it on the web server? Many thanks Jane.

That's impossible. A web server hosting an ASPX page has to receive the file from the client before processing it any further.

SiN
A: 

Hi, Off the top of my head, create the attachment like:

txtFile.PostedFile.InputStream.Position = 0  
mail.Attachments.Add(new MailAttachment(txtFile.PostedFile.InputStream, strfilename  ));

That should allow you to create the attachment, without saving it to disk.

Cheers!
Dave

dave wanta