1 Reply Latest reply: Jul 31, 2013 2:21 PM by Bill Shannon-Oracle RSS

    attaching a local file to an email


      I have been asked to develop the means to attach a local file as an attachment to an emails created and sent via Javamail.  This is a new task for me and I have managed so far to get everything working right except the part where I can upload and attach a local file to an email message.  I have a form page containing this field:   <TR> <TD><FONT FACE="verdana,arial" SIZE="-2"><B>Attach file</B></FONT></TD> </TD><TD><INPUT NAME="attachment1" id="attachment1"  type="file"></TD> </TR>.  I click "Browse" and the input field is filled with a string of text i.e. "c:\temp\readme.txt" stating the location and name of the local file to attach.  I click Submit and this code runs in my script that pertains to uploading and attaching a file:

      BodyPart messageBodyPart = new MimeBodyPart();
      Multipart multipart = new MimeMultipart();
      messageBodyPart = new MimeBodyPart();
      DataSource source = new FileDataSource(Attachment1);
      messageBodyPart.setDataHandler(new DataHandler(source));

      try {
      } catch(Exception e) {


      This works but only works on attaching a file that is stored on the server and not the one of my local drive.  If I wanted to attach c:\abc.txt that sits on my local drive to an email I can go through the motions of doing that but it does not work.  So I created another c:\abc.txt file but placed this one on the server where Javamail resides.  I clicked Browse, navigated to c:\abc.txt on my local drive, clicked Submit and it worked great.  But -  attached to the resulting email was abc.txt that sits on the server and not the one on my local drive.  What I now need is code that will actually upload a file from my local drive and attach it to my outgoing email.  Am rather new to Javamail so I do not know the details of how to do this particiulat task (uploading and attaching a local file to Javamail).  Am using Java 1.7, Javamail 1.4.5 and Tomcat 6.0.  Form is in JSP.   Other than my needing to attach a local file to an email Javamail is working great for me.  I hope I have stated this clearly; any help would be appreciated.  Thank you.