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

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

Introduction

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

Prototype

public void setHostName(final String aHostName) 

Source Link

Document

Set the hostname of the outgoing mail server.

Usage

From source file:FacultyAdvisement.ImagineBean.java

public String submitRequest() {

    String emailCourses = "";

    for (Course c : currentCourses) {
        emailCourses += "<li>" + c.getSubject() + " " + c.getNumber() + "</li>";
    }/*from  ww w  .  j  a va2  s .c om*/

    try {
        Email email = new HtmlEmail();
        email.setHostName("smtp.googlemail.com");
        email.setSmtpPort(465);
        email.setAuthenticator(new DefaultAuthenticator("uco.faculty.advisement", "!@#$1234"));
        email.setSSLOnConnect(true);
        email.setFrom("uco.faculty.advisement@gmail.com");
        email.setSubject("Microsoft Imagine Account");
        email.setMsg("<font size=\"3\" style=\"font-family:verdana\"> \n" + "<ul><li>Student Name: "
                + student.getFirstName() + " " + student.getLastName() + "</li><li>Student Major: "
                + student.getMajorCode() + "<li>Current Courses: <ol>" + emailCourses + "</ol></li></ul> "
                + "Student Email if needed for response: " + student.getUsername()
                + "\n<p align=\"center\">UCO Faculty Advisement</p></font>");
        email.addTo("uco.faculty.advisement@gmail.com");
        email.send();
    } catch (EmailException ex) {
        Logger.getLogger(VerificationBean.class.getName()).log(Level.SEVERE, null, ex);
    }

    return "/customerFolder/imagineConfirm";
}

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 .  j  av a2 s . com
        // 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:ch.fihlon.moodini.business.token.control.TokenService.java

@SneakyThrows
private void sendChallenge(@NotNull final String email, @NotNull final Challenge challenge) {
    final SmtpConfiguration smtp = configuration.getSmtp();
    final Email mail = new SimpleEmail();
    mail.setHostName(smtp.getHostname());
    mail.setSmtpPort(smtp.getPort());/*from  www .  j  a v  a  2  s. c om*/
    mail.setAuthenticator(new DefaultAuthenticator(smtp.getUser(), smtp.getPassword()));
    mail.setSSLOnConnect(smtp.getSsl());
    mail.setFrom(smtp.getFrom());
    mail.setSubject("Your challenge to login to Moodini");
    mail.setMsg(String.format("Your one time challenge, valid for 10 minutes: %s", challenge.getChallenge()));
    mail.addTo(email);
    mail.send();
}

From source file:com.pax.pay.trans.receipt.paperless.AReceiptEmail.java

private int setBaseInfo(EmailInfo emailInfo, Email email) {
    try {/*from   w ww. ja v  a  2s  . c om*/
        //email.setDebug(true);
        email.setHostName(emailInfo.getHostName());
        email.setSmtpPort(emailInfo.getPort());
        email.setAuthentication(emailInfo.getUserName(), emailInfo.getPassword());
        email.setCharset("UTF-8");
        email.setSSLOnConnect(emailInfo.isSsl());
        if (emailInfo.isSsl())
            email.setSslSmtpPort(String.valueOf(emailInfo.getSslPort()));
        email.setFrom(emailInfo.getFrom());
    } catch (EmailException e) {
        e.printStackTrace();
        return -1;
    }
    return 0;
}

From source file:ch.sdi.core.impl.mail.MailSenderDefault.java

/**
 * @see ch.sdi.core.intf.MailSender#sendMail(java.lang.Object)
 *///from  www.  j  a  v  a2s . c om
@Override
public void sendMail(Email aMail) throws SdiException {
    aMail.setHostName(myHost);
    if (mySslOnConnect) {
        aMail.setSslSmtpPort("" + myPort);
    } else {
        aMail.setSmtpPort(myPort);
    } // if..else mySslOnConnect

    aMail.setAuthenticator(myAuthenticator);
    aMail.setSSLOnConnect(mySslOnConnect);
    aMail.setStartTLSRequired(myStartTlsRequired);

    try {
        aMail.setFrom(mySenderAddress);
    } catch (EmailException t) {
        throw new SdiException("Problems setting the sender address to mail: " + mySenderAddress, t,
                SdiException.EXIT_CODE_MAIL_ERROR);
    }

    if (myDryRun) {
        myLog.debug("DryRun is set. Not sending the mail");
        // TODO: save locally in output dir
    } else {
        try {
            aMail.send();
            myLog.debug("mail successfully sent");
        } catch (Throwable t) {
            throw new SdiException("Problems sending a mail", t, SdiException.EXIT_CODE_MAIL_ERROR);
        }
    } // if..else myDryRun

}

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);
    email.setSmtpPort(Integer.parseInt(port));
    email.setSocketConnectionTimeout(socketTimeout);
    email.setDebug(true);/*from w  w  w . j a  v a2  s .c o  m*/

    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:at.treedb.util.Mail.java

/**
 * /*from   w  w w .  j  av  a2  s .c o m*/
 * @param sendTo
 * @param subject
 * @param message
 * @throws EmailException
 */
public void sendMail(String mailTo, String mailFrom, String subject, String message) throws Exception {
    Objects.requireNonNull(mailTo, "Mail.sendMail(): mailTo can not be null!");
    Objects.requireNonNull(mailFrom, "Mail.sendMail(): mailFrom can not be null!");
    Objects.requireNonNull(subject, "Mail.sendMail(): subject can not be null!");
    Objects.requireNonNull(message, "Mail.sendMail(): message can not be null!");
    Email email = new SimpleEmail();
    email.setHostName(smtpHost);

    email.setAuthenticator(new DefaultAuthenticator(smtpUser, smtpPassword));
    if (transportSecurity == TransportSecurity.SSL) {
        email.setSSLOnConnect(true);
        email.setSSLCheckServerIdentity(false);
    } else if (transportSecurity == TransportSecurity.STARTTLS) {
        email.setStartTLSRequired(true);
    }
    email.setSmtpPort(smtpPort);
    email.setFrom(mailFrom);
    email.setSubject(subject);
    email.setMsg(message);
    email.addTo(mailTo);
    email.send();
}

From source file:de.cosmocode.palava.services.mail.VelocityMailService.java

@Override
public MimeMessage sendMessage(String templateName, String lang, Map<String, ?> params, String... to)
        throws Exception {
    if (templateName == null)
        throw new IllegalArgumentException("Template name is null");

    final VelocityContext ctx = new VelocityContext(params);

    final String prefix = StringUtils.isBlank(lang) ? "" : lang + "/";
    final Template template = engine.getTemplate(prefix + templateName, CHARSET);

    final Embedder embed = new Embedder(engine);
    ctx.put("embed", embed);

    ctx.put("entity", EntityEncoder.getInstance());

    final StringWriter writer = new StringWriter();
    template.merge(ctx, writer);/*  w w w  .  java  2  s  . c o  m*/

    final EmailFactory factory = EmailFactory.getInstance();
    final SAXBuilder builder = new SAXBuilder();
    final Document document = builder.build(new StringReader(writer.toString()));

    final Email email = factory.build(document, embed);
    email.setHostName(hostname);

    for (String recipient : to) {
        email.addTo(recipient);
    }

    email.send();

    return email.getMimeMessage();
}

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  w w  .jav 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.moss.error_reporting.server.Notifier.java

private void prepEmail(Email email, ReportId id) throws EmailException {
    email.setHostName(smtpServer);
    for (String recipient : emailRecipients) {
        email.addTo(recipient);/*from   ww w  . j  a  v  a2 s. co  m*/
    }

    //      email.addTo(emailRecipients.get(0));
    email.setFrom(emailFromAddress, "Error Report Server");
    email.setSubject("Error Report Received: " + id.getUuid());
}