code: private File extractFileAttachment(final Mailbox mb, final Part p) {
  
  		File file = null;
  
  		try {
  
  			final Class fileClass = p.getContentType().toLowerCase().startsWith("image/") ? Image.class : File.class;
  
  			final App app = StructrApp.getInstance();
  
  			try (final Tx tx = app.tx()) {
  
  				org.structr.web.entity.Folder fileFolder = FileHelper.createFolderPath(SecurityContext.getSuperUserInstance(), getStoragePath(mb.getUuid()));
  
  				try {
  
  					String fileName = decodeText(p.getFileName());
  
  					if (fileName == null) {
  						fileName = NodeServiceCommand.getNextUuid();
  					}
  
  					file = FileHelper.createFile(SecurityContext.getSuperUserInstance(), p.getInputStream(), p.getContentType(), fileClass, fileName, fileFolder);
  
  				} catch (FrameworkException ex) {
  
  					logger.warn("EMail in mailbox[" + mb.getUuid() + "] attachment has invalid name. Using random UUID as fallback.");
  					file = FileHelper.createFile(SecurityContext.getSuperUserInstance(), p.getInputStream(), p.getContentType(), fileClass, NodeServiceCommand.getNextUuid(), fileFolder);
  				}
  
  				tx.success();
  
  			} catch (IOException | FrameworkException ex) {
  
  				logger.error("Exception while extracting file attachment: ", ex);
  			}
  
  
  		} catch (MessagingException ex) {
  
  			logger.error("Exception while extracting file attachment: ", ex);
  		}
  
  		return file;
  	}
docs: Returns attachment UUID to append to the mail to be created