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

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

Introduction

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

Prototype

public InternetAddress getFromAddress() 

Source Link

Document

Gets the sender of the email.

Usage

From source file:com.qwazr.library.email.EmailConnectorTest.java

private void checkEmail(final Map<String, Object> params, final Email email) {
    Assert.assertNotNull(email);/*from   www . ja  v a  2  s .c  o  m*/
    Assert.assertEquals(params.get("subject"), email.getSubject());
    Assert.assertEquals(params.get("from_email"), email.getFromAddress().getAddress());
    Assert.assertEquals(params.get("from_name"), email.getFromAddress().getPersonal());
}

From source file:de.hybris.platform.test.MailUtilsTest.java

@Test
public void testPreConfiguredEmail() throws EmailException {
    //read out original values
    final String orgSMTPServer = Config.getString(Config.Params.MAIL_SMTP_SERVER, null);
    final String orgFrom = Config.getString(Config.Params.MAIL_FROM, null);
    final String orgUseTLS = Config.getString(Config.Params.MAIL_USE_TLS, Boolean.FALSE.toString());
    final String orgSMTPPort = Config.getString(Config.Params.MAIL_SMTP_PORT, "-1");

    //define test values
    String smtpPort = "42";
    String smtpServer = "smtp.hybris.de";
    String from = "from@hybris.de";
    String useTLS = Boolean.FALSE.toString();
    //test test values
    Config.setParameter(Config.Params.MAIL_SMTP_SERVER, smtpServer);
    Config.setParameter(Config.Params.MAIL_SMTP_PORT, smtpPort);
    Config.setParameter(Config.Params.MAIL_FROM, from);
    Config.setParameter(Config.Params.MAIL_USE_TLS, useTLS);
    //check test values
    Email email = MailUtils.getPreConfiguredEmail();
    assertEquals(email.getSmtpPort(), smtpPort);
    assertEquals(email.getHostName(), smtpServer);
    assertEquals(email.getFromAddress().getAddress(), from);
    assertEquals(Boolean.toString(email.isTLS()), useTLS);

    email = null;/*w  w  w.  j  av a 2  s  .c o  m*/

    //define new test values
    smtpPort = "43";
    smtpServer = "smtp2.hybris.de";
    from = "from2@hybris.de";
    useTLS = Boolean.TRUE.toString();
    //set new test values
    Config.setParameter(Config.Params.MAIL_SMTP_SERVER, smtpServer);
    Config.setParameter(Config.Params.MAIL_SMTP_PORT, smtpPort);
    Config.setParameter(Config.Params.MAIL_FROM, from);
    Config.setParameter(Config.Params.MAIL_USE_TLS, useTLS);
    //check changed test values
    email = MailUtils.getPreConfiguredEmail();
    assertEquals(email.getSmtpPort(), smtpPort);
    assertEquals(email.getHostName(), smtpServer);
    assertEquals(email.getFromAddress().getAddress(), from);
    assertEquals(Boolean.toString(email.isTLS()), useTLS);

    //restore old values
    Config.setParameter(Config.Params.MAIL_SMTP_SERVER, orgSMTPServer);
    Config.setParameter(Config.Params.MAIL_SMTP_PORT, orgSMTPPort);
    Config.setParameter(Config.Params.MAIL_FROM, orgFrom);
    Config.setParameter(Config.Params.MAIL_USE_TLS, orgUseTLS);
}

From source file:com.github.triceo.robozonky.notifications.email.AbstractEmailingListener.java

@Override
public void handle(final T event) {
    if (!this.shouldSendEmail(event)) {
        LOGGER.debug("Will not send e-mail.");
        return;//  w  w w.j ava 2  s  . c  o  m
    } else
        try {
            final Email email = AbstractEmailingListener.createNewEmail(properties);
            email.setSubject(this.getSubject(event));
            email.setMsg(TemplateProcessor.INSTANCE.process(this.getTemplateFileName(), this.getData(event)));
            LOGGER.debug("Will send '{}' from {} to {} through {}:{} as {}.", email.getSubject(),
                    email.getFromAddress(), email.getToAddresses(), email.getHostName(), email.getSmtpPort(),
                    properties.getSmtpUsername());
            email.send();
            emailsOfThisType.increase();
            this.properties.getGlobalCounter().increase();
        } catch (final Exception ex) {
            throw new RuntimeException("Failed processing event.", ex);
        }

}

From source file:io.mapzone.controller.email.EmailService.java

public void send(Email email) throws EmailException {
    String env = System.getProperty("io.mapzone.controller.SMTP");
    if (env == null) {
        throw new IllegalStateException(
                "Environment variable missing: io.mapzone.controller.SMTPP. Format: <host>|<login>|<passwd>|<from>");
    }/*from w w  w  .  ja v a  2s .  c  o  m*/
    String[] parts = StringUtils.split(env, "|");
    if (parts.length < 3 || parts.length > 4) {
        throw new IllegalStateException(
                "Environment variable wrong: io.mapzone.controller.SMTP. Format: <host>|<login>|<passwd>|<from> : "
                        + env);
    }

    email.setDebug(true);
    email.setHostName(parts[0]);
    //email.setSmtpPort( 465 );
    //email.setSSLOnConnect( true );
    email.setAuthenticator(new DefaultAuthenticator(parts[1], parts[2]));
    if (email.getFromAddress() == null && parts.length == 4) {
        email.setFrom(parts[3]);
    }
    if (email.getSubject() == null) {
        throw new EmailException("Missing subject.");
    }
    email.send();
}

From source file:org.apache.sling.commons.messaging.mail.internal.SimpleMailBuilderIT.java

@Test
public void testBuildWithDefaults() throws Exception {
    final MailBuilder mailBuilder = getService(MailBuilder.class);
    final Email email = mailBuilder.build("Stop your messing around, Better think of your future...",
            "rudy@ghosttown", Collections.emptyMap());
    email.buildMimeMessage();/*from   w ww.j  av a  2 s .  c o  m*/
    final byte[] bytes = MailUtil.toByteArray(email);
    final String mail = new String(bytes, StandardCharsets.UTF_8);
    logger.debug("mail: " + mail);
    assertEquals("rudy@ghosttown", email.getToAddresses().get(0).getAddress());
    assertEquals("Rudy, A Message to You", email.getSubject());
    assertEquals("dandy.livingstone@kingston.jamaica", email.getFromAddress().getAddress());
    assertEquals("localhost", email.getHostName());
    logger.debug(email.getMimeMessage().getContent().toString());
}

From source file:org.apache.sling.commons.messaging.mail.internal.SimpleMailBuilderIT.java

@Test
public void testBuildWithData() throws Exception {
    final MailBuilder mailBuilder = getService(MailBuilder.class);
    final Map<String, String> configuration = new HashMap<>();
    configuration.put("mail.subject", "Rudy, A Message to You");
    configuration.put("mail.from", "specials@thespecials.com");
    final Map data = Collections.singletonMap("mail", configuration);
    final Email email = mailBuilder.build("A Message to You, Rudy", "rudy@ghosttown", data);
    email.buildMimeMessage();// w ww  . ja va 2s  .c  o m
    final byte[] bytes = MailUtil.toByteArray(email);
    final String mail = new String(bytes, StandardCharsets.UTF_8);
    logger.debug("mail: " + mail);
    assertEquals("rudy@ghosttown", email.getToAddresses().get(0).getAddress());
    assertEquals("Rudy, A Message to You", email.getSubject());
    assertEquals("specials@thespecials.com", email.getFromAddress().getAddress());
    assertEquals("localhost", email.getHostName());
    logger.debug(email.getMimeMessage().getContent().toString());
}

From source file:org.openhab.binding.mail.internal.SMTPHandler.java

/**
 * use this server to send a mail//  ww  w .  ja va  2s  .  c  om
 *
 * @param mail the Email that needs to be sent
 * @return true if successful, false if failed
 */
public boolean sendMail(Email mail) {
    try {
        if (mail.getFromAddress() == null) {
            mail.setFrom(config.sender);
        }
        mail.setHostName(config.hostname);
        switch (config.security) {
        case SSL:
            mail.setSSLOnConnect(true);
            mail.setSslSmtpPort(config.port.toString());
            break;
        case TLS:
            mail.setStartTLSEnabled(true);
            mail.setStartTLSRequired(true);
            mail.setSmtpPort(config.port);
            break;
        case PLAIN:
            mail.setSmtpPort(config.port);
        }
        if (!config.username.isEmpty() && !config.password.isEmpty()) {
            mail.setAuthenticator(new DefaultAuthenticator(config.username, config.password));
        }
        mail.send();
    } catch (EmailException e) {
        logger.warn("Trying to send mail but exception occured: {} ", e.getMessage());
        return false;
    }
    return true;
}

From source file:org.polymap.rhei.um.email.EmailService.java

public void send(Email email) throws EmailException {
    String env = System.getProperty("org.polymap.rhei.um.SMTP");
    if (env == null) {
        throw new IllegalStateException(
                "Environment variable missing: org.polymap.rhei.um.SMTP. Format: <host>|<login>|<passwd>|<from>");
    }//  w  w  w  .ja va 2  s.  co  m
    String[] parts = StringUtils.split(env, "|");
    if (parts.length < 3 || parts.length > 4) {
        throw new IllegalStateException(
                "Environment variable wrong: org.polymap.rhei.um.SMTP. Format: <host>|<login>|<passwd>|<from> : "
                        + env);
    }

    email.setDebug(true);
    email.setHostName(parts[0]);
    //email.setSmtpPort( 465 );
    //email.setSSLOnConnect( true );
    email.setAuthenticator(new DefaultAuthenticator(parts[1], parts[2]));
    if (email.getFromAddress() == null && parts.length == 4) {
        email.setFrom(parts[3]);
    }
    if (email.getSubject() == null) {
        throw new EmailException("Missing subject.");
    }
    email.send();
}

From source file:org.sigmah.server.mail.DummyMailSender.java

@Override
public void send(Email email) throws EmailException {
    if (email == null || ArrayUtils.isEmpty(email.getToAddresses())) {
        // does nothing.
        throw new EmailException("Email object null or invalid.");
    }//  w  w  w  .  j ava 2 s.c o  m

    Assert.assertNotNull(email.getFromAddress());
    Assert.assertFalse(email.getFromAddress().trim().isEmpty());

    Assert.assertNotNull(email.getSubject());
    Assert.assertFalse(email.getSubject().isEmpty());

    Assert.assertNotNull(email.getContent());
    Assert.assertFalse(email.getContent().isEmpty());

    Assert.assertNotNull(email.getContentType());
    Assert.assertFalse(email.getContentType().isEmpty());

    Assert.assertNotNull(email.getEncoding());
    Assert.assertFalse(email.getEncoding().isEmpty());

    // Building the output
    System.out.println("From: \"" + email.getFromName() + "\" <" + email.getFromAddress() + '>');

    final StringBuilder toAddressesBuilder = new StringBuilder();
    for (final String address : email.getToAddresses()) {
        toAddressesBuilder.append(address).append(", ");
    }
    toAddressesBuilder.setLength(toAddressesBuilder.length() - 2);
    System.out.println("To: " + toAddressesBuilder);

    if (ArrayUtils.isNotEmpty(email.getCcAddresses())) {
        final StringBuilder ccAddressesBuilder = new StringBuilder();
        for (final String address : email.getCcAddresses()) {
            ccAddressesBuilder.append(address).append(", ");
        }
        ccAddressesBuilder.setLength(toAddressesBuilder.length() - 2);
        System.out.println("Cc: " + ccAddressesBuilder);
    }

    System.out.println("Subject: " + email.getSubject());
    final SimpleDateFormat dateFormat = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z", Locale.US);
    System.out.println("Date: " + dateFormat.format(new Date()));
    System.out.println("Content-Type: " + email.getContentType() + ";charset=" + email.getEncoding());

    System.out.println();
    System.out.println(email.getContent());
}

From source file:org.sigmah.server.mail.MailSenderImpl.java

/**
 * {@inheritDoc}//from   ww  w. j a v a  2s  .co  m
 */
@Override
public void send(final Email email) throws EmailException {

    if (email == null || ArrayUtils.isEmpty(email.getToAddresses())) {
        // does nothing.
        throw new EmailException("Email object null or invalid.");
    }

    // Simple email.
    final SimpleEmail simpleEmail = new SimpleEmail();

    // Mail content parameters.
    simpleEmail.setFrom(email.getFromAddress(), email.getFromName());
    for (final String address : email.getToAddresses()) {
        simpleEmail.addTo(address);
    }
    if (ArrayUtils.isNotEmpty(email.getCcAddresses())) {
        for (final String address : email.getCcAddresses()) {
            simpleEmail.addCc(address);
        }
    }
    simpleEmail.setSubject(email.getSubject());
    simpleEmail.setContent(email.getContent(), email.getContentType());

    // Mail sending parameters.
    simpleEmail.setCharset(email.getEncoding());
    simpleEmail.setHostName(email.getHostName());
    simpleEmail.setSmtpPort(email.getSmtpPort());

    // Authentication is needed.
    final String userName = email.getAuthenticationUserName();
    final String password = email.getAuthenticationPassword();
    if (userName != null && password != null) {
        simpleEmail.setAuthentication(userName, password);
    }

    // Sends the mail.
    simpleEmail.send();
}