Attach file from my local machine to send mail in cq/AEM

女生的网名这么多〃 提交于 2020-01-11 10:25:41

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!