1 Replies - 1209 Views - Last Post: 05 June 2014 - 12:46 PM Rate Topic: -----

#1 Emwat Oon  Icon User is offline

  • D.I.C Head

Reputation: 1
  • View blog
  • Posts: 54
  • Joined: 07-January 12

multiple email attachments w/ 2.0

Posted 05 June 2014 - 12:34 PM

I'm trying to accomplish multiple email attachments, but most of the ways I found involve javascript, AJAX, and ASP.net 4.5.

I've set up an asp wizard and the last button of the wizard should send the email with attachments. It does send the email, but there isn't any attachments.




protected void Wizard1_FinishButtonclick(object sender, WizardNavigationEventArgs e)
    {
--snip--
        MailMessage mail = new MailMessage(--snip--);
        mail.IsBodyHtml = false;

        HttpFileCollection uploadedFiles = Request.Files;
        for (int i = 0; i < uploadedFiles.Count; i++)
            {    
            HttpPostedFile userPostedFile = uploadedFiles[i];    
            try
                {    
                if (userPostedFile.ContentLength > 0 )
                    {
                    Attachment attachFile = new Attachment(userPostedFile.InputStream.ToString());
                    mail.Attachments.Add(attachFile);
                    }    
                } 
            catch (Exception Ex)
            {    
            Label1.Text += "Error: <br>" + Ex.Message;    
            }    
        }
        smtpClient.Send(mail);
        label2.Text = "Cheerio!!";
        Wizard1.Visible = false;
    }



I'm guessing I'm having trouble specifically with the code 'Attachment attachFile = new Attachment(userPostedFile.InputStream.ToString());

I'm gathering the files by
<asp:FileUpload ID="FileUpload1" runat="server" />
but I'm guessing that's really one file at the moment until I make more FileUploads.

Is This A Good Question/Topic? 0
  • +

Replies To: multiple email attachments w/ 2.0

#2 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 4332
  • View blog
  • Posts: 12,127
  • Joined: 18-April 07

Re: multiple email attachments w/ 2.0

Posted 05 June 2014 - 12:46 PM

When you pass a string to the Attachment contructor, it thinks you are using that as the filename of the file itself. I think you want to do something along these lines (this is untested)...

// Get the name of the file and append it to a path (so you get a path directly to the file uploaded)
string filename = Server.MapPath("~/") + Path.GetFileName(FileUpload1.FileName);

// Then attach via this name...
Attachment attachFile = new Attachment(filename);



From what I can tell there you are trying to get a string representation of the stream and using that as a filename which the constructor expects.

You could also use one of the other constructor overloads which takes a stream directly, but for those you also need additional parameters specifying content type or filename and type. Be sure to look over the other constructor overloads because there are ones for customizing the name etc.

Hope this gets you moving in the right direction. :)

This post has been edited by Martyr2: 05 June 2014 - 12:47 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1