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

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

Introduction

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

Prototype

@Deprecated
public void setTLS(final boolean withTLS) 

Source Link

Document

Set or disable the STARTTLS encryption.

Usage

From source file:de.maklerpoint.office.Schnittstellen.Email.SimpleEmailSender.java

/**
 * /*from w  ww  . j  a  v a2 s.  c o  m*/
 * @param adress
 * @param Subject
 * @param body
 * @throws EmailException 
 */

public static void sendSimpleEMail(String adress, String Subject, String body) throws EmailException {
    Email email = new SimpleEmail();
    email.setHostName(Config.get("mailHost", ""));
    email.setSmtpPort(Config.getConfigInt("emailPort", 25));

    email.setTLS(Config.getConfigBoolean("mailTLS", false));
    email.setSSL(Config.getConfigBoolean("mailSSL", false));

    //email.setSslSmtpPort(Config.getConfigInt("emailPort", 25));
    email.setAuthenticator(
            new DefaultAuthenticator(Config.get("mailUsername", ""), Config.get("mailPassword", "")));

    email.setFrom(Config.get("mailSendermail", ""), Config.get("mailSender", ""));

    email.setSubject(Subject);
    email.setMsg(body);
    email.addTo(adress);

    email.send();
}

From source file:com.aprodher.actions.util.EmailUtils.java

public static Email conectaEmail() throws EmailException {
    Email email = new SimpleEmail();
    email.setHostName(HOSTNAME);/*from   w  ww . ja  v  a 2  s .c om*/
    email.setSmtpPort(587);
    email.setAuthenticator(new DefaultAuthenticator(USERNAME, PASSWORD));
    email.setSSL(true);
    email.setTLS(true);
    email.setFrom(EMAILORIGEM);
    return email;
}

From source file:com.basetechnology.s0.agentserver.mail.AgentMail.java

public int sendMessage(User user, String toEmail, String toName, String subject, String message,
        String messageTrailer1, String messageTrailer2) throws AgentServerException {
    try {/*from  w ww .  ja v  a  2  s . c o m*/
        // Wait until we have mail access
        agentServer.mailAccessManager.wait(user, toEmail);

        int messageId = ++nextMessageId;
        log.info("Sending mail on behalf of user " + user.id + " with message Id " + messageId + " To: '<"
                + toName + ">" + toEmail + "' Subject: '" + subject + "'");

        Email email = new SimpleEmail();
        email.setDebug(debug);
        email.setHostName(mailServerHostName);
        email.setSmtpPort(mailServerPort);
        email.setAuthenticator(new DefaultAuthenticator(mailServerUserName, mailServerUserPassword));
        email.setTLS(true);
        email.setFrom(mailServerFromEmail, mailServerFromName);
        // TODO: Reconsider whether we want to always mess with subject line
        email.setSubject(subject + " (#" + messageId + ")");
        email.setMsg(message + messageTrailer1 + (messageTrailer2 != null ? messageId + messageTrailer2 : ""));
        email.addTo(toEmail, toName);
        email.send();
        log.info("Message sent");

        // Return the message Id
        return messageId;
    } catch (EmailException e) {
        e.printStackTrace();
        throw new AgentServerException("EmailException sending email - " + e.getMessage());
    }
}

From source file:com.mirth.connect.connectors.smtp.SmtpSenderService.java

@Override
public Object invoke(String channelId, String method, Object object, String sessionId) throws Exception {
    if (method.equals("sendTestEmail")) {
        SmtpDispatcherProperties props = (SmtpDispatcherProperties) object;

        String host = replacer.replaceValues(props.getSmtpHost(), channelId);
        String portString = replacer.replaceValues(props.getSmtpPort(), channelId);

        int port = -1;
        try {/*from   w  ww. j  av  a  2  s  . c  o m*/
            port = Integer.parseInt(portString);
        } catch (NumberFormatException e) {
            return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE,
                    "Invalid port: \"" + portString + "\"");
        }

        String secure = props.getEncryption();

        boolean authentication = props.isAuthentication();

        String username = replacer.replaceValues(props.getUsername(), channelId);
        String password = replacer.replaceValues(props.getPassword(), channelId);
        String to = replacer.replaceValues(props.getTo(), channelId);
        String from = replacer.replaceValues(props.getFrom(), channelId);

        Email email = new SimpleEmail();
        email.setDebug(true);
        email.setHostName(host);
        email.setSmtpPort(port);

        if ("SSL".equalsIgnoreCase(secure)) {
            email.setSSL(true);
        } else if ("TLS".equalsIgnoreCase(secure)) {
            email.setTLS(true);
        }

        if (authentication) {
            email.setAuthentication(username, password);
        }

        email.setSubject("Mirth Connect Test Email");

        try {
            for (String toAddress : StringUtils.split(to, ",")) {
                email.addTo(toAddress);
            }

            email.setFrom(from);
            email.setMsg(
                    "Receipt of this email confirms that mail originating from this Mirth Connect Server is capable of reaching its intended destination.\n\nSMTP Configuration:\n- Host: "
                            + host + "\n- Port: " + port);

            email.send();
            return new ConnectionTestResponse(ConnectionTestResponse.Type.SUCCESS,
                    "Sucessfully sent test email to: " + to);
        } catch (EmailException e) {
            return new ConnectionTestResponse(ConnectionTestResponse.Type.FAILURE, e.getMessage());
        }
    }

    return null;
}

From source file:com.mirth.connect.server.util.SMTPConnection.java

public void send(String toList, String ccList, String from, String subject, String body) throws EmailException {
    Email email = new SimpleEmail();
    email.setHostName(host);/*from   www  .  ja v  a 2s .c  om*/
    email.setSmtpPort(Integer.parseInt(port));
    email.setSocketConnectionTimeout(socketTimeout);
    email.setDebug(true);

    if (useAuthentication) {
        email.setAuthentication(username, password);
    }

    if (StringUtils.equalsIgnoreCase(secure, "TLS")) {
        email.setTLS(true);
    } else if (StringUtils.equalsIgnoreCase(secure, "SSL")) {
        email.setSSL(true);
    }

    for (String to : StringUtils.split(toList, ",")) {
        email.addTo(to);
    }

    if (StringUtils.isNotEmpty(ccList)) {
        for (String cc : StringUtils.split(ccList, ",")) {
            email.addCc(cc);
        }
    }

    email.setFrom(from);
    email.setSubject(subject);
    email.setMsg(body);
    email.send();
}

From source file:de.eod.jliki.users.jsfbeans.UserRegisterBean.java

/**
 * Adds a new user to the jLiki database.<br/>
 *//*from   w  ww. j a v  a 2s. co  m*/
public final void addNewUser() {
    final User newUser = new User(this.username, this.password, this.email, this.firstname, this.lastname);
    final String userHash = UserDBHelper.addUserToDB(newUser);

    if (userHash == null) {
        Messages.addFacesMessage(null, FacesMessage.SEVERITY_ERROR, "message.user.register.failed",
                this.username);
        return;
    }

    UserRegisterBean.LOGGER.debug("Adding user: " + newUser.toString());

    final FacesContext fc = FacesContext.getCurrentInstance();
    final HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
    final ResourceBundle mails = ResourceBundle.getBundle("de.eod.jliki.EMailMessages",
            fc.getViewRoot().getLocale());
    final String activateEMailTemplate = mails.getString("user.registration.email");
    final StringBuffer url = request.getRequestURL();
    final String serverUrl = url.substring(0, url.lastIndexOf("/"));

    UserRegisterBean.LOGGER.debug("Generated key for user: \"" + userHash + "\"");

    final String emsLink = serverUrl + "/activate.xhtml?user=" + newUser.getName() + "&key=" + userHash;
    final String emsLikiName = ConfigManager.getInstance().getConfig().getPageConfig().getPageName();
    final String emsEMailText = MessageFormat.format(activateEMailTemplate, emsLikiName, this.firstname,
            this.lastname, this.username, emsLink);

    final String emsHost = ConfigManager.getInstance().getConfig().getEmailConfig().getHostname();
    final int emsPort = ConfigManager.getInstance().getConfig().getEmailConfig().getPort();
    final String emsUser = ConfigManager.getInstance().getConfig().getEmailConfig().getUsername();
    final String emsPass = ConfigManager.getInstance().getConfig().getEmailConfig().getPassword();
    final boolean emsTSL = ConfigManager.getInstance().getConfig().getEmailConfig().isUseTLS();
    final String emsSender = ConfigManager.getInstance().getConfig().getEmailConfig().getSenderAddress();

    final Email activateEmail = new SimpleEmail();
    activateEmail.setHostName(emsHost);
    activateEmail.setSmtpPort(emsPort);
    activateEmail.setAuthentication(emsUser, emsPass);
    activateEmail.setTLS(emsTSL);
    try {
        activateEmail.setFrom(emsSender);
        activateEmail.setSubject("Activate jLiki Account");
        activateEmail.setMsg(emsEMailText);
        activateEmail.addTo(this.email);
        activateEmail.send();
    } catch (final EmailException e) {
        UserRegisterBean.LOGGER.error("Sending activation eMail failed!", e);
        return;
    }

    this.username = "";
    this.password = "";
    this.confirm = "";
    this.email = "";
    this.firstname = "";
    this.lastname = "";
    this.captcha = "";
    this.termsOfUse = false;
    this.success = true;

    Messages.addFacesMessage(null, FacesMessage.SEVERITY_INFO, "message.user.registered", this.username);
}

From source file:com.pkrete.locationservice.endpoint.mailer.impl.EmailServiceImpl.java

/**
 * Sends the given EmailMessage to the recipients defined in the message.
 * Returns true if and only if the message was successfully sent to all the
 * recipients; otherwise false.//from w  w  w .j a v  a 2s.com
 *
 * @param message email message to be sent
 * @return true if and only if the message was successfully sent to all the
 * recipients; otherwise false
 */
@Override
public boolean send(EmailMessage message) {
    if (message == null) {
        logger.warn("Message cannot be null.");
        return false;
    }
    logger.debug("Create new \"{}\" email message.", message.getType().toString());

    if (message.getRecipients().isEmpty()) {
        logger.info("No recipients defined. Nothing to do -> exit.");
        return false;
    }
    Email email = new SimpleEmail();
    email.setHostName(PropertiesUtil.getProperty("mail.host"));
    email.setSmtpPort(this.converterService.strToInt(PropertiesUtil.getProperty("mail.port")));
    email.setAuthenticator(new DefaultAuthenticator(PropertiesUtil.getProperty("mail.user"),
            PropertiesUtil.getProperty("mail.password")));
    email.setTLS(true);
    try {
        // Set from address
        email.setFrom(message.getFrom());
        // Set subject
        email.setSubject(message.getSubject());

        // Build message body
        StringBuilder body = new StringBuilder();
        if (!message.getHeader().isEmpty()) {
            body.append(message.getHeader()).append("\n\n");
        }
        if (!message.getMessage().isEmpty()) {
            body.append(message.getMessage()).append("\n\n");
        }
        if (!message.getFooter().isEmpty()) {
            body.append(message.getFooter()).append("\n\n");
        }
        if (!message.getSignature().isEmpty()) {
            body.append(message.getSignature()).append("\n\n");
        }

        // Set message contents
        email.setMsg(body.toString());

        // Add message receivers
        for (String recipient : message.getRecipients()) {
            logger.info("Add recipient \"{}\".", recipient);
            email.addTo(recipient);
        }

        // Send message
        email.send();

        logger.info("Email was succesfully sent to {} recipients.", message.getRecipients().size());
    } catch (Exception e) {
        logger.error("Failed to send \"{}\" email message.", message.getType().toString());
        logger.error(e.getMessage());
        return false;
    }
    return true;
}

From source file:com.waveerp.sendMail.java

public void sendMsg(String strSource, String strSourceDesc, String strSubject, String strMsg,
        String strDestination, String strDestDesc) throws Exception {

    // Call the registry management system                    
    registrySystem rs = new registrySystem();
    // Call the encryption management system
    desEncryption de = new desEncryption();
    de.Encrypter("", "");

    String strHost = rs.readRegistry("NA", "NA", "NA", "EMAILHOST");
    String strPort = rs.readRegistry("NA", "NA", "NA", "EMAILPORT");
    String strUser = rs.readRegistry("NA", "NA", "NA", "EMAILUSER");
    String strPass = rs.readRegistry("NA", "NA", "NA", "EMAILPASSWORD");

    log(DEBUG, strHost + "|" + strPort + "|" + strUser + "|" + strPass);

    //Decrypt the encrypted password.
    strPass = de.decrypt(strPass);// w  w  w  .  j  ava  2  s.  co m

    Email email = new SimpleEmail();
    email.setHostName(strHost);
    email.setSmtpPort(Integer.parseInt(strPort));
    email.setAuthenticator(new DefaultAuthenticator(strUser, strPass));
    email.setTLS(false);
    email.setFrom(strSource, strSourceDesc);
    email.setSubject(strSubject);
    email.setMsg(strMsg);

    email.addTo(strDestination, strDestDesc);
    email.send();

}

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

/**
 * ?   ?? ?? ./*from   w  w w. jav  a 2  s  .  co  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;
}

From source file:com.pkrete.locationservice.admin.mailer.impl.BasicEmailService.java

/**
 * Send an email to the given user when the user is created or the password
 * is modified.//from w w  w  .j  av a2s.c  o m
 *
 * @param user the receiver of the email
 */
@Override
public void send(UserFull user) {
    logger.info("Create new email message.");
    Email email = new SimpleEmail();
    email.setHostName(PropertiesUtil.getProperty("mail.host"));
    email.setSmtpPort(this.converterService.strToInt(PropertiesUtil.getProperty("mail.port")));
    email.setAuthenticator(new DefaultAuthenticator(PropertiesUtil.getProperty("mail.user"),
            PropertiesUtil.getProperty("mail.password")));
    email.setTLS(true);
    try {
        // Init variables
        String header = null;
        String msg = null;

        // Set from address
        email.setFrom(this.messageSource.getMessage("mail.from", null, null));

        // Set message arguments
        Object[] args = new Object[] { user.getUsername(), user.getPasswordUi() };

        // Set variables values
        if (user.getUpdated() == null) {
            // This is a new user
            if (logger.isDebugEnabled()) {
                logger.debug("The message is for a new user.");
            }
            // Set subject
            email.setSubject(this.messageSource.getMessage("mail.title.add", null, null));
            // Set message header
            header = this.messageSource.getMessage("mail.header.add", null, null);
            // Set message content
            msg = this.messageSource.getMessage("mail.message.add", args, null);
        } else {
            // This is an existing user
            logger.debug("The message is for an existing user.");
            // Set subject
            email.setSubject(this.messageSource.getMessage("mail.title.edit", null, null));
            // Get message header
            header = this.messageSource.getMessage("mail.header.edit", null, null);
            // Get message content
            msg = this.messageSource.getMessage("mail.message.edit", args, null);
        }

        // Get note
        String note = this.messageSource.getMessage("mail.note", null, null);
        // Get footer
        String footer = this.messageSource.getMessage("mail.footer", null, null);
        // Get signature
        String signature = this.messageSource.getMessage("mail.signature", null, null);

        // Build message body
        StringBuilder result = new StringBuilder();
        if (!header.isEmpty()) {
            result.append(header).append("\n\n");
        }
        if (!msg.isEmpty()) {
            result.append(msg).append("\n\n");
        }
        if (!note.isEmpty()) {
            result.append(note).append("\n\n");
        }
        if (!footer.isEmpty()) {
            result.append(footer).append("\n\n");
        }
        if (!signature.isEmpty()) {
            result.append(signature).append("\n\n");
        }
        // Set message contents
        email.setMsg(result.toString());
        // Set message receiver
        email.addTo(user.getEmail());
        // Send message
        email.send();
        logger.info("Email was sent to \"{}\".", user.getEmail());
    } catch (Exception e) {
        logger.error("Failed to send email to \"{}\".", user.getEmail());
        logger.error(e.getMessage(), e);
    }
}