-1

Hi I wrote the below Java code to email a file as attachement. It works fine if I provide a sample filepath. But, I want it to take the file path browsed by the user in the Front End

        //code to attach the file

        MimeBodyPart messageBodyPart = new MimeBodyPart();
        Multipart multipart = new MimeMultipart();
        messageBodyPart.setText(sb.toString());



        MimeBodyPart messageBodyPart2 = new MimeBodyPart();

        //String filename = filePath;
        String filename = "C:/Users/S.Mandava/Documents/ContactusAction.java";
        DataSource source = new FileDataSource(filename);
        messageBodyPart2.setDataHandler(new DataHandler(source));
        messageBodyPart2.setFileName(filename);
        multipart.addBodyPart(messageBodyPart);
        multipart.addBodyPart(messageBodyPart2);
        msg.setContent(multipart);
        System.out.println("Attaching the file");




        Transport.send(msg);

Form example, I want the system to take the file path uploaded in the below form when the user clicks on submit and the file should be sent as an attachment in an email

 <form >
    <input type ="file"/ id="multiple_files" name="multiple_files" multiple><br/>
      <button>Submit</button>
 </form>

How do I do it? I want to use JSP and JavaScript on the Front End and Java on the Back End. I am having difficulty in getting the file path.

  • 1
    google something related to fileupload in jsp and servlet, you will get your result. – Rishal Sep 16 '16 at 14:35
  • 1
    [http://www.codejava.net/java-ee/jsp/send-attachments-with-e-mail-using-jsp-servlet-and-javamail](http://www.codejava.net/java-ee/jsp/send-attachments-with-e-mail-using-jsp-servlet-and-javamail) check this link – Rishal Sep 16 '16 at 16:50
  • Rishal , I tried that example. When I hit on submit button, I get This site can’t be reached – krish mandava Sep 19 '16 at 16:34

1 Answers1

0

As @Rishal_dev_singh said, search for examples, Google is your friend... What you do is an InputStream, you don't define a path

KESO
  • 335
  • 2
  • 5
  • 17