0

Excuse my ignorant but I have never had to set up a mail server in Java before and I just managed to send a simple plaintext email. I'm using Apache James as my custom local server. A third party email server is not an option.

I have searched around on Google and on Stack Overflow for any tutorials that would show me how you can attach an image or a pdf to an a James email and have found nothing.

I am currently using Java with Spring boot and I can successfully upload multiple files and store them locally.. here is my code:

   @RequestMapping(value="/uploadMultiples", method=RequestMethod.POST)
   public @ResponseBody String uploadMultiples(MultipartHttpServletRequest request){
       logger.info("POST /uploadMultiples");
       Map<String, MultipartFile> fileMap = request.getFileMap();
       for(String fileName:request.getFileMap().keySet()) {
           MultipartFile file = request.getFile(fileName);
           if (!file.isEmpty()) {
               try {
                   logger.info("file name:" + file.getName());
                   logger.info("original file name " + file.getOriginalFilename());
                   byte[] bytes = file.getBytes();
                   BufferedOutputStream stream =
                        new BufferedOutputStream(new FileOutputStream(new File(file.getOriginalFilename())));
                   stream.write(bytes);
                   stream.close();
               } catch (Exception e) {
                   return "You failed to upload " + file.getOriginalFilename() + " => " + e.getMessage();
               }
           } else {
               return "You failed to upload " + file.getOriginalFilename() + " because the file was empty.";
           }
       }return "success";

   }

If anyone knows how I can just attach these files to an email and send it using Apache James that would be exactly what i'm looking for.

Snorlux
  • 53
  • 1
  • 5
  • I'm not clear on what you want to do. Do you want to upload the files to your server, then run a program on your server to send email using those files? Or do you want to run a program on the machine that contains the files before upload, and then attach the files to an email messages that you send to your James server? In any event, attaching a file is straightforward, see the [JavaMail FAQ](http://www.oracle.com/technetwork/java/javamail/faq/index.html#attach) and [sample programs](https://java.net/projects/javamail/pages/Home#Samples). – Bill Shannon Feb 04 '16 at 18:19

1 Answers1

0

The code you posted has absolutely nothing to do with the question title???. In order to create an email with an attachment it does not matter where the attachments originated from so your code is just unnecessary noise (please correct me if I'm wrong).

My guess is that you want to send an attachment using the java mail api's

lance-java
  • 25,497
  • 4
  • 59
  • 101
  • Well that's what I have, I can upload files and store them locally so I was wondering how I can change my code to attach those files I am uploading into an email.. – Snorlux Feb 04 '16 at 15:04
  • Being a developer, it should be obvious that the logic to send an email will be the same regardless of where the attachment originated. So including the code to upload a file is just unnecessary noise. Also your tags of `apache` and `spring-boot` are just more unnecessary noise. I'm surprised you didn't mention what colour t-shirt you are wearing :) – lance-java Feb 04 '16 at 16:59