问题描述
我正在学习AEM,我正在处理一个要求,我可以发送电子邮件,但是我无法添加从我的机器浏览的附件。
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.
要求 -
HTML中收集了一个表单,从中收集信息,并且有一个可以上传文件的浏览按钮。
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.
另外同时,通过POST请求,表单内容和附件应发送到JSON
Also at the same time, through a POST request the form content and the attachement should be sent to a JSON
通过电子邮件将内容发送到收件人和POST方法是工作正常
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 ?
谢谢!
推荐答案
在HTML中,您可以获取文件从输入框中输入fileas ::
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>
在java中,您可以将这个文件作为::
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);
你也可以参考这个
这篇关于从本地机器附加文件,发送邮件到cq / AEM的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!