2015-12-07 22 views
-1

我正在嘗試發送帶有參數和附件的HTML電子郵件。 我現在所擁有的是這樣的代碼:用html外部模板用java發送html郵件?

<%@include file="/libs/fd/af/components/guidesglobal.jsp" %> 
<%@page import="com.day.cq.wcm.foundation.forms.FormsHelper, 
       org.apache.sling.api.resource.ResourceUtil, 
       org.apache.sling.api.resource.ValueMap, 
       org.apache.sling.api.request.RequestParameter, 
       com.day.cq.mailer.MessageGatewayService, 
       com.day.cq.mailer.MessageGateway, 
       org.apache.commons.mail.Email, 
       org.apache.fulcrum.template.TemplateHtmlEmail, 
       org.apache.commons.mail.*" %> 
<%@taglib prefix="sling" uri="http://sling.apache.org/taglibs/sling/1.0" %> 
<%@taglib prefix="cq" uri="http://www.day.com/taglibs/cq/1.0" 
%> 
<cq:defineObjects/> 
<sling:defineObjects/> 
<% 
    String storeContent = "/libs/fd/af/components/guidesubmittype/store"; 
    FormsHelper.runAction(storeContent, "post", resource, slingRequest, slingResponse); 
    ValueMap props = ResourceUtil.getValueMap(resource); 
    HtmlEmail email = new HtmlEmail(); 
    String[] mailTo = props.get("mailto", new String[0]); 
    email.setFrom((String)props.get("from")); 
     for (String toAddr : mailTo) { 
      email.addTo(toAddr); 
     } 

    String htmlEmailTemplate = props.get("templatePath"); 

    //========Email Attachments=============== 
    for (Map.Entry<String, RequestParameter[]> param : slingRequest.getRequestParameterMap().entrySet()) { 
     RequestParameter rpm = param.getValue()[0]; 
     if(!rpm.isFormField()) { 
      EmailAttachment attachment = new EmailAttachment(); 
      attachment.setPath(rpm.getFileName()); 
      attachment.setDisposition(EmailAttachment.ATTACHMENT); 
      attachment.setDescription("Any Description"); 
      attachment.setName("Any name you can set"); 
      email.embed(new ByteArrayDataSource(rpm.get(), rpm.getContentType()), rpm.getFileName()); 
     } 
    } 
    //========Email Attachment END=========== 

    String emailTextToSend = "<p>Company Name: " + slingRequest.getParameter("company-name") + "</p>"; 
    emailTextToSend += "<p>Message: " + slingRequest.getParameter("address") + "</p>"; 
    email.setHtmlMsg(emailTextToSend); 
    email.setSubject((String)props.get("subject")); 
    MessageGatewayService messageGatewayService = sling.getService(MessageGatewayService.class); 
    MessageGateway messageGateway = messageGatewayService.getGateway(HtmlEmail.class); 
    messageGateway.send(email); 
%> 

有了這個代碼,我可以發送電子郵件,但我想修改代碼以使用路徑到HTML模板文件(路徑是變量htmlEmailTemplate

這是我的第一個問題,如何更改代碼 我的第二個問題是,如果在模板中我能有這樣的事情:

<span>${company-name}</span> 

哪裏company-name是該領域的一個s,我想在模板上使用。 這可能嗎?

+0

檢查模板引擎,如速度 – Jan

+0

我的意圖是不使用模板引擎,只是使用HTML電子郵件 –

回答

1

看看在com.day.cq.commons.mail.MailTemplate API 如果你的模板是在JCR庫,你可以用類似實例吧:

String template = values.get(TEMPLATE_PROPERTY, String.class); 
Resource templateRsrc = request.getResourceResolver().getResource(template); 
final MailTemplate mailTemplate = MailTemplate.create(templateRsrc.getPath(), 
      templateRsrc.getResourceResolver().adaptTo(Session.class)); 
final HtmlEmail email = mailTemplate.getEmail(StrLookup.mapLookup(properties), HtmlEmail.class); 

,其屬性是根本Key的HashMap:模板自身屬性的值。

由於MailTemplate會返回一個HtmlEmail對象,所以您仍然可以設置您在自己的代碼中設置的所有設置。

-1

以下鏈接可以幫助:

Link to send mail with attachment

+0

這是不夠的,以提供一個鏈接,這不是如何SO作品。請詳細說明,你不是谷歌:) – revelt

+0

如果你點擊了鏈接,那是我寫的博客,它有詳細信息。 我也提到了類似的回答http://stackoverflow.com/questions/33438632/attach-file-from-my-local-machine-to-send-mail-in-cq-aem/33808853#33808853 因此爲此,我更願意僅提及鏈接 – Manisha