问题
I am learning AEM and I am working on a requirement where in I am able to send email message however I am unable to add attachments that are browsed from my machine.
Requirement -
There is a form made in HTML from where info is collected and there is a browse button from where a file can be uploaded.
As soon as the file is uploaded an email should be sent to an email address with form content and with the attachment.
Also at the same time, through a POST request the form content and the attachement should be sent to a JSON
Sending the content via email to the receipient and to the POST method is working fine.
Any suggestions on how can I get attachement working in this ?
Thanks!
回答1:
In HTML, You can fetch the file from input box with type "file" as ::
<form id="submitForm" action="/bin/servlets/submitForm" method="POST" novalidate="novalidate" enctype="multipart/form-data">
<label for="name">Name </label><input name="userName" type="text" class="fieldInner" id="name" required>
<input name="file" value="Choose File" type="file" class="chooseFileInner" required/>
<input type="submit" id="applied" value="Submit"/>
</form>
in java, you can fetch this file as ::
RequestParameter attach = request.getRequestParameter("file");
InputStream ip = attach.getInputStream();
MailTemplate mailTemplate = MailTemplate.create(templatePath, session);
HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(new HashMap<String, String>(parameters)), HtmlEmail.class);
ByteArrayDataSource fileDS = new ByteArrayDataSource(ip, "application/pdf");
email.attach(fileDS, "application/pdf", "This is your attached file.");
messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
messageGateway.send(email);
You can also refer this Link to send images in email in aem
来源:https://stackoverflow.com/questions/33438632/attach-file-from-my-local-machine-to-send-mail-in-cq-aem