Example usage for org.apache.commons.mail Email setContent

List of usage examples for org.apache.commons.mail Email setContent

Introduction

In this page you can find the example usage for org.apache.commons.mail Email setContent.

Prototype

public void setContent(final Object aObject, final String aContentType) 

Source Link

Document

Set the content and contentType.

Usage

From source file:com.cqecom.cms.components.eMailer.TrialOsubEmailController.java

public void doPost(SlingHttpServletRequest request, SlingHttpServletResponse response) {
    try {/*from w  ww .j av a  2s .  com*/
        String strLicenseName = "";
        String strLicensePassword = "";
        String strLanguageName = "";
        String strEndsAt = "";
        String strLanguageSlug = "";
        String strTrialUrl = "";
        String strOfferPromo = "";
        String strEmailTemplateUrl = "";
        String strFollowupEmailTemplateUrl = "";
        String strEmailSubject = "";
        String strEmailFrom = "";
        String followupEmail = "";
        String strFollowupEmailSubject = "";

        if (request != null) {
            strLicenseName = getParamValue(request, "license_name");
            strLicensePassword = getParamValue(request, "license_password");
            strLanguageName = getParamValue(request, "language_name");
            strEndsAt = getParamValue(request, "ends_at");
            strLanguageSlug = getParamValue(request, "language_slug");
            strTrialUrl = getParamValue(request, "trial_url");
            strOfferPromo = getParamValue(request, "offer_promo");
            strEmailTemplateUrl = getParamValue(request, "email_template_url");
            strFollowupEmailTemplateUrl = getParamValue(request, "followup_email_template_url");
            strEmailSubject = getParamValue(request, "email_subject");
            strFollowupEmailSubject = getParamValue(request, "followup_email_subject");
            strEmailFrom = getParamValue(request, "email_from");
            followupEmail = getParamValue(request, "followup_email");
        }

        if (followupEmail.equals("true")) {
            strEmailTemplateUrl = strFollowupEmailTemplateUrl;
            strEmailSubject = strFollowupEmailSubject;
        }

        session = repository.loginAdministrative(null);
        String pageHtml = session.getRootNode().getNode(strEmailTemplateUrl + "/jcr:content/content")
                .getProperty("text").getValue().getString();

        pageHtml = replaceToken(pageHtml, "license_name", strLicenseName);
        pageHtml = replaceToken(pageHtml, "license_password", strLicensePassword);
        pageHtml = replaceToken(pageHtml, "language_name", strLanguageName);
        pageHtml = replaceToken(pageHtml, "ends_at", strEndsAt);
        pageHtml = replaceToken(pageHtml, "language_slug", strLanguageSlug);
        pageHtml = replaceToken(pageHtml, "trial_url", strTrialUrl);
        pageHtml = replaceToken(pageHtml, "offer_promo", strOfferPromo);
        Email emailObj = new HtmlEmail();
        emailObj.setContent(pageHtml.toString(), "text/html");

        if (!strEmailFrom.equals(""))
            emailObj.setFrom(strEmailFrom, "CqEcom " + strLanguageName + " Trial");

        if (!strLicenseName.equals(""))
            emailObj.addTo(strLicenseName);

        if (!strEmailSubject.equals(""))
            emailObj.setSubject(strEmailSubject);
        ms.sendEmail(emailObj);
        logger.info("Mail sent to => " + strLicenseName);
    } catch (Exception ex) {
        logger.info(ex.getMessage());
    } finally {
        if (session != null)
            session.logout();
    }
}

From source file:de.hybris.basecommerce.SimpleSmtpServerUtilsTest.java

@Test
public void testSendSuccess() throws EmailException, AddressException {
    final String origMailPortNumber = Config.getParameter(Config.Params.MAIL_SMTP_PORT);
    final String origMailHost = Config.getParameter(Config.Params.MAIL_SMTP_SERVER);

    SimpleSmtpServer server = null;//from   www.  ja  v  a 2 s .  co  m
    try {
        server = SimpleSmtpServerUtils.startServer(TEST_START_PORT);
        Assert.assertFalse(server.isStopped());
        Assert.assertTrue(server.getPort() > 0);

        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, "localhost");
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, String.valueOf(server.getPort()));

        final Email email = MailUtils.getPreConfiguredEmail();

        email.setFrom("foo.bar@hybris.com");
        email.setTo(Arrays.asList(InternetAddress.parse("foo.bar@hybris.com")));
        email.setSubject("TEST TEST TEST");
        email.setContent("FOO", Email.TEXT_PLAIN);

        email.send();
    } finally {
        Config.setParameter(Config.Params.MAIL_SMTP_SERVER, origMailHost);
        Config.setParameter(Config.Params.MAIL_SMTP_PORT, origMailPortNumber);

        if (server != null) {
            server.stop();
        }
    }
}

From source file:egovframework.rte.tex.com.service.impl.EgovMailServiceImpl.java

/**
 * ?   ?? ?? .//from w  w  w.  j a  v a 2  s  . c o m
 * @param vo ?
 * @return ? 
 */
@Override
@SuppressWarnings("deprecation")
public boolean sendEmailTo(MemberVO vo) {
    boolean result = false;

    Email email = new SimpleEmail();

    email.setCharset("utf-8"); //  ?

    // setHostName?  ?
    // email.setHostName(mailInfoService.getString("hostName"));  // SpEL?  properties ? 
    email.setHostName(hostName); // SMTP 
    email.setSmtpPort(port);
    email.setAuthenticator(new DefaultAuthenticator(mailId, mailPass));
    email.setTLS(true);
    try {
        email.addTo(vo.getEmail(), vo.getId()); // ? 
    } catch (EmailException e) {
        e.printStackTrace();
    }
    try {
        email.setFrom(mailId, mailName); //  
    } catch (EmailException e) {
        e.printStackTrace();
    }
    email.setSubject(subject); // ? 
    email.setContent("ID: " + vo.getId() + "<br>" + "PASSWORD: " + vo.getPassword(),
            "text/plain; charset=utf-8");
    try {
        email.send();
        result = true;
    } catch (EmailException e) {
        e.printStackTrace();
    }
    return result;
}