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.