0

I am trying to send an email with an attachment that will be uploaded by the user and then sent to an admin email.

I have got this configured correctly on IE 11 but however with Chrome/Firefox there is limitations with the FilePath it provides. As IE 11 provides the full file path it allows my function to work.

Is there a possible way round this for Chrome/Firefox.

Mail Message code:

protected void Submit_Click(object sender, EventArgs e)
    {

        using (MailMessage message = new MailMessage())
        {
                if (Attachment1.HasFile == false)
                {
                    message.From = new MailAddress(Environment.UserName + "@domain");
                    message.To.Add(new MailAddress("MyEmail"));
                    message.IsBodyHtml = true;
                    message.Subject = "New Request from self service portal: " + Summary.Text.ToString();
                    message.Body = "Customer Name:</br>Customer Username:" + Environment.UserName + "</br>" + DetailedSummary.Text.ToString();
                    SmtpClient client = new SmtpClient();
                    client.Host = "IP ADDRESS";
                    client.Send(message);
                } else {
                    message.From = new MailAddress(Environment.UserName + "@domain");
                    message.To.Add(new MailAddress("myemail"));
                    string file = Attachment1.PostedFile.FileName;
                    message.Attachments.Add(new Attachment(file));
                    message.IsBodyHtml = true;
                    message.Subject = "New Request from self service portal: " + Summary.Text.ToString();
                    message.Body = "Customer Name:</br>Customer Username:" + Environment.UserName + "</br>" + DetailedSummary.Text.ToString();
                    SmtpClient client = new SmtpClient();
                    client.Host = "IP ADDRESS";
                    client.Send(message);
            }

        }
    }

This is where the user will specify which file will be uploaded and will not be a static file being uploaded every time. Meaning, i will need the filepath from the FileUpload.

Gary Henshall
  • 72
  • 2
  • 11
  • 1
    Possible duplicate of: http://stackoverflow.com/questions/5336239/how-to-attach-a-file-from-memorystream-to-a-mailmessage-in-c-sharp – ernest Nov 11 '15 at 15:22
  • Try `System.Net.Mail.Attachment attach = new System.Net.Mail.Attachment(Attachment1.PostedFile.InputStream, "filename");` then `message.Attachments.Add(` – Artur Udod Nov 11 '15 at 15:26

1 Answers1

5

HttpPostedFile.FileName "Gets the fully qualified name of the file on the client."

During development on your machine (and using a browser that actually sends the full path, which proper browsers don't) that may work, but as soon as you deploy it on a server it'll break.

The easiest way would be to use the new Attachment(Attachment1.PostedFile.InputStream, "attachmentname") constructor to directly stream the uploaded file into your attachment without having to temporarily save it yourself.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • Thanks for the explanation. I am looking into the input stream now. I will accept as soon as i have worked it out! Cheers again. – Gary Henshall Nov 11 '15 at 15:49