{"id":19134,"date":"2015-05-28T14:19:44","date_gmt":"2015-05-28T08:49:44","guid":{"rendered":"http:\/\/www.tothenew.com\/blog\/?p=19134"},"modified":"2016-12-19T14:54:55","modified_gmt":"2016-12-19T09:24:55","slug":"send-email-and-attach-filesimages-from-aem","status":"publish","type":"post","link":"https:\/\/www.tothenew.com\/blog\/send-email-and-attach-filesimages-from-aem\/","title":{"rendered":"Send email and attach files\/images from AEM"},"content":{"rendered":"<p class=\"western\">To Send email from AEM , follow the steps written below:<\/p>\n<p class=\"western\">Gmail SMTP Server could also be used to relay messages from your device or application. If you connect using SMTP, you can only send mail to Gmail or Google Apps users; if you connect using SSL\/TLS, you can send mail to anyone.<\/p>\n<p class=\"western\">If your device or application supports SSL &#8211; connect to smtp.gmail.com on port 465.<\/p>\n<p class=\"western\">To connect with SSL, you need to provide a Google username and password for authentication.<\/p>\n<p class=\"western\">You can connect it in this way:<\/p>\n<ul>\n<li class=\"western\"><b>CONFIGURE MAIL SERVER<\/b><\/li>\n<\/ul>\n<p class=\"western\">Go to the <b>felix configuration<\/b> by hitting the url \u2013 <span style=\"color: #000080;\"><span lang=\"zxx\"><span style=\"text-decoration: underline;\">http:\/\/localhost:4502\/system\/console\/configMgr<\/span><\/span><\/span> then open the <b>Day CQ Mail Service<\/b> and configure like this<\/p>\n<p class=\"western\"><a href=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/Screenshot-from-2015-04-14-111112.png\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone size-full wp-image-19135\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/Screenshot-from-2015-04-14-111112.png\" alt=\"Screenshot from 2015-04-14 11:11:12\" width=\"1166\" height=\"438\" \/><\/a><\/p>\n<ul>\n<li>\n<p class=\"western\"><b>ADD A TEMPLATE<\/b><\/p>\n<\/li>\n<\/ul>\n<p class=\"western\">A <b>template<\/b> is basically created in \/etc\/notification. The MailTemplate class provides email text templating functionality. Templates are nt:file nodes in the repository representing a text file (or html will also work, as in this case).<\/p>\n<p class=\"western\">The template will create a structure of how your email will look like. The ${subject} and ${message} act as the replacement variables.<\/p>\n<p class=\"western\">And then write the code as follows:<\/p>\n<p class=\"western\"><img decoding=\"async\" loading=\"lazy\" class=\"alignnone wp-image-19136 size-full\" src=\"\/blog\/wp-ttn-blog\/uploads\/2015\/04\/Screenshot-from-2015-04-14-120602.png\" alt=\"Screenshot from 2015-04-14 12:06:02\" width=\"941\" height=\"241\" \/><\/p>\n<ul>\n<li>\n<p class=\"western\"><span style=\"color: #000000;\"><b>CREATE A SERVLET<\/b><\/span><\/p>\n<\/li>\n<\/ul>\n<p class=\"western\">Create a servlet and write the following code snippet :<\/p>\n<p>[java]@Reference<br \/>\nMessageGatewayService messageGatewayService;<br \/>\n[\/java]<\/p>\n<p class=\"western\">This will get all the values with request as parameters in form of map.<\/p>\n<p>[java]@SuppressWarnings(&quot;unchecked&quot;)<br \/>\nparameterNames = request.getParameterNames();<br \/>\nparameters = new HashMap&lt;String, String&gt;();<br \/>\nwhile (parameterNames.hasMoreElements()) {<br \/>\nfinal String key = parameterNames.nextElement();<br \/>\nparameters.put(key, request.getParameter(key));<br \/>\n}<br \/>\n[\/java]<\/p>\n<p class=\"western\"><i>MailTemplate<\/i> will create a mail template<\/p>\n<p>[java]<br \/>\nResource templateRsrc = request.getResourceResolver().getResource(&quot;\/etc\/notification\/send-email.html&quot;);<br \/>\nString mailId = request.getParameter(&quot;mailId&quot;);<br \/>\n[\/java]<\/p>\n<ul>\n<li>\n<p class=\"western\"><b>ATTACH IMAGES<\/b><\/p>\n<\/li>\n<\/ul>\n<p class=\"western\">Email attachments can send any file attached with the mail<\/p>\n<p class=\"western\"><span style=\"font-family: FreeMono, serif;\">\u00a0<\/span><\/p>\n<p>[java]EmailAttachment attachment = new EmailAttachment();<br \/>\nattachment.setPath(&quot;&lt;image-Path&gt;&quot;);<br \/>\nattachment.setDisposition(EmailAttachment.ATTACHMENT);<br \/>\nattachment.setDescription(&quot;Any Description&quot;);<br \/>\nattachment.setName(&quot;Any name you can set&quot;);<br \/>\nfinal MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), templateRsrc.getResourceResolver().adaptTo(Session.class));<br \/>\nfinal HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(properties), HtmlEmail.class);<br \/>\nemail.addTo(mailId);<br \/>\n[\/java]<\/p>\n<p class=\"western\">The <i>getEmail<\/i> method returns the choosen (type argument) email implementation, as long as the type extends Email and has a publically accessible default constructor. Out of the box the three email implementations provided by the Apache Commons Email library can be used: SimpleEmail, HtmlEmail and MultiPartEmail.<\/p>\n<p>[java]<br \/>\nmessageGateway = messageGatewayService.getGateway(HtmlEmail.class);<br \/>\nmessageGateway.send(email);<br \/>\n[\/java]<\/p>\n<p class=\"western\"><i>MessageGateway<\/i><b> <\/b>is an Object capable of sending a message to a recipient. This is an ServiceProvider to be used as to register as an OSGI service. Gateways can be accessed via MessageGatewayService.<\/p>\n<ul>\n<li>\n<p class=\"western\"><b>ATTACH IMAGES FROM DAM<\/b><\/p>\n<\/li>\n<\/ul>\n<p class=\"western\">To <b>attach any image from dam <\/b>you can fetch the image in binary format and send it to desired recipient in this way,<\/p>\n<p>[java]<br \/>\nResource imageNodeRes = request.getResourceResolver().getResource(&quot;\/content\/dam\/indeximg\/anil-gupta-nav-img.jpg\/jcr:content\/renditions\/original&quot;);<br \/>\nNode imageNode=imageNodeRes.adaptTo(Node.class);<br \/>\nNode contentNode = imageNode.getNode(&quot;jcr:content&quot;);<br \/>\nBinary imageBinary = contentNode.getProperty(&quot;jcr:data&quot;).getBinary();<br \/>\nInputStream imageStream = imageBinary.getStream();<br \/>\nByteArrayDataSource imageDS = new ByteArrayDataSource(imageStream,&quot;image\/png&quot;);<br \/>\nemail.attach(imageDS,&quot;Some Image&quot;,&quot;Some Description&quot;);<br \/>\n[\/java]<\/p>\n<ul>\n<li>\n<p class=\"western\"><b>EXPLICITLY EMBEDD IMAGES<\/b><\/p>\n<\/li>\n<\/ul>\n<p class=\"western\"><b>Images can be<\/b> <b>Embbed explicitly<\/b> by this way but this will violate the mail Template and send mail in its own way.<\/p>\n<p>[java]<br \/>\nURL url = new URL(&quot;&lt;Any URL&gt;&quot;);<br \/>\nString cid = email.embed(url, &quot;&lt;Any Name&gt;&quot;);<br \/>\nemail.setHtmlMsg(&quot;&lt;html&gt;&lt;body&gt;&lt;img src=\\&quot;cid:&quot;+cid+&quot;\\&quot;&gt;&lt;\/body&gt;&lt;\/html&gt;&quot;);<br \/>\n[\/java]<\/p>\n<p class=\"western\">Thank you..<\/p>\n<p class=\"western\">Have nice time \ud83d\ude42<\/p>\n","protected":false},"excerpt":{"rendered":"<p>To Send email from AEM , follow the steps written below: Gmail SMTP Server could also be used to relay messages from your device or application. If you connect using SMTP, you can only send mail to Gmail or Google Apps users; if you connect using SSL\/TLS, you can send mail to anyone. If your [&hellip;]<\/p>\n","protected":false},"author":188,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"iawp_total_views":68},"categories":[1],"tags":[],"aioseo_notices":[],"_links":{"self":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19134"}],"collection":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/users\/188"}],"replies":[{"embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/comments?post=19134"}],"version-history":[{"count":0,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/posts\/19134\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/media?parent=19134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/categories?post=19134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.tothenew.com\/blog\/wp-json\/wp\/v2\/tags?post=19134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}