Expand my Community achievements bar.

Enhance your AEM Assets & Boost Your Development: [AEM Gems | June 19, 2024] Improving the Developer Experience with New APIs and Events
SOLVED

Email service send mail with file attachments

Avatar

Level 2

Hi All,

 

could you help me to find  "how to attach file without upload file in DAM and direct attach to email and send ".

1 Accepted Solution

Avatar

Correct answer by
Community Advisor

Hi @ritesh01 

 

  • Use <input> element with type as file to upload a file and pass it on to a servlet.
    <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>

    dev_aem_0-1647955939159.png
  • Write a sling servlet that receives the file as an inputstream. You can attach this to org.apache.commons.mail.HtmlEmail
    Below is working piece of servlet code.
    try {
    RequestParameter attach = request.getRequestParameter("file");
    InputStream ip = attach.getInputStream();
    HtmlEmail email = new HtmlEmail();
    //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.");
    email.addTo("receiver@example.com");
    email.setSubject("test email subject");
    email.setMsg("text email body");
    email.setHtmlMsg("<html><head></head><body><p>html email body</p></body></html>");
    email.setFrom("sender@example.com","TestUser");
    // Declare a MessageGateway service
    MessageGateway<Email> messageGateway;
    messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
    messageGateway.send(email);
    } catch (EmailException e) {
    e.printStackTrace();
    }
    tempsnip.png
    Note: SMTP has to be configured in order for aem to send emails. This is an OOTB configuration.

Thanks

View solution in original post

4 Replies

Avatar

Correct answer by
Community Advisor

Hi @ritesh01 

 

  • Use <input> element with type as file to upload a file and pass it on to a servlet.
    <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>

    dev_aem_0-1647955939159.png
  • Write a sling servlet that receives the file as an inputstream. You can attach this to org.apache.commons.mail.HtmlEmail
    Below is working piece of servlet code.
    try {
    RequestParameter attach = request.getRequestParameter("file");
    InputStream ip = attach.getInputStream();
    HtmlEmail email = new HtmlEmail();
    //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.");
    email.addTo("receiver@example.com");
    email.setSubject("test email subject");
    email.setMsg("text email body");
    email.setHtmlMsg("<html><head></head><body><p>html email body</p></body></html>");
    email.setFrom("sender@example.com","TestUser");
    // Declare a MessageGateway service
    MessageGateway<Email> messageGateway;
    messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
    messageGateway.send(email);
    } catch (EmailException e) {
    e.printStackTrace();
    }
    tempsnip.png
    Note: SMTP has to be configured in order for aem to send emails. This is an OOTB configuration.

Thanks

Avatar

Level 2

Single file working fine but I have added multiple files and added for loop it moves infinity loop can you help me 

Avatar

Community Advisor

I have not tried multiple files. I'll give it a try and let you know how it goes.

Avatar

Level 2

multifile upload working only file name issue like that I have upload 4 file and received in the mail 9 files (request Parameter + upload files) and every file name same  

ritesh01_1-1648096150317.png