Example usage for org.apache.commons.mail SimpleEmail addHeader

List of usage examples for org.apache.commons.mail SimpleEmail addHeader

Introduction

In this page you can find the example usage for org.apache.commons.mail SimpleEmail addHeader.

Prototype

public void addHeader(final String name, final String value) 

Source Link

Document

Adds a header ( name, value ) to the headers Map.

Usage

From source file:org.sonar.plugins.emailnotifications.EmailNotificationChannel.java

private void send(EmailMessage emailMessage) throws EmailException {
    // Trick to correctly initilize javax.mail library
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    try {//www .  j  a va 2 s .  co  m
        LOG.debug("Sending email: {}", emailMessage);
        String host = null;
        try {
            host = new URL(configuration.getServerBaseURL()).getHost();
        } catch (MalformedURLException e) {
            // ignore
        }

        SimpleEmail email = new SimpleEmail();
        if (StringUtils.isNotBlank(host)) {
            /*
            * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and
            * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook
            */
            if (StringUtils.isNotEmpty(emailMessage.getMessageId())) {
                String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">";
                email.addHeader(IN_REPLY_TO_HEADER, messageId);
                email.addHeader(REFERENCES_HEADER, messageId);
            }
            // Set headers for proper filtering
            email.addHeader(LIST_ID_HEADER, "Sonar <sonar." + host + ">");
            email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL());
        }
        // Set general information
        email.setCharset("UTF-8");
        String from = StringUtils.isBlank(emailMessage.getFrom()) ? FROM_NAME_DEFAULT
                : emailMessage.getFrom() + " (Sonar)";
        email.setFrom(configuration.getFrom(), from);
        email.addTo(emailMessage.getTo(), " ");
        String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ",
                "") + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT);
        email.setSubject(subject);
        email.setMsg(emailMessage.getMessage());
        // Send
        email.setHostName(configuration.getSmtpHost());
        if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "SSL")) {
            email.setSSL(true);
            email.setSslSmtpPort(String.valueOf(configuration.getSmtpPort()));

            // this port is not used except in EmailException message, that's why it's set with the same value than SSL port.
            // It prevents from getting bad message.
            email.setSmtpPort(configuration.getSmtpPort());
        } else if (StringUtils.isBlank(configuration.getSecureConnection())) {
            email.setSmtpPort(configuration.getSmtpPort());
        } else {
            throw new SonarException(
                    "Unknown type of SMTP secure connection: " + configuration.getSecureConnection());
        }
        if (StringUtils.isNotBlank(configuration.getSmtpUsername())
                || StringUtils.isNotBlank(configuration.getSmtpPassword())) {
            email.setAuthentication(configuration.getSmtpUsername(), configuration.getSmtpPassword());
        }
        email.setSocketConnectionTimeout(SOCKET_TIMEOUT);
        email.setSocketTimeout(SOCKET_TIMEOUT);
        email.send();

    } finally {
        Thread.currentThread().setContextClassLoader(classloader);
    }
}

From source file:org.sonar.server.notification.email.EmailNotificationChannel.java

private void send(EmailMessage emailMessage) throws EmailException {
    // Trick to correctly initialize javax.mail library
    ClassLoader classloader = Thread.currentThread().getContextClassLoader();
    Thread.currentThread().setContextClassLoader(getClass().getClassLoader());

    try {//from   ww  w. ja v a2  s .com
        LOG.debug("Sending email: {}", emailMessage);
        String host = null;
        try {
            host = new URL(configuration.getServerBaseURL()).getHost();
        } catch (MalformedURLException e) {
            // ignore
        }

        SimpleEmail email = new SimpleEmail();
        if (StringUtils.isNotBlank(host)) {
            /*
             * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and
             * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook
             */
            if (StringUtils.isNotEmpty(emailMessage.getMessageId())) {
                String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">";
                email.addHeader(IN_REPLY_TO_HEADER, messageId);
                email.addHeader(REFERENCES_HEADER, messageId);
            }
            // Set headers for proper filtering
            email.addHeader(LIST_ID_HEADER, "SonarQube <sonar." + host + ">");
            email.addHeader(LIST_ARCHIVE_HEADER, configuration.getServerBaseURL());
        }
        // Set general information
        email.setCharset("UTF-8");
        String from = StringUtils.isBlank(emailMessage.getFrom()) ? FROM_NAME_DEFAULT
                : (emailMessage.getFrom() + " (SonarQube)");
        email.setFrom(configuration.getFrom(), from);
        email.addTo(emailMessage.getTo(), " ");
        String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ",
                "") + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT);
        email.setSubject(subject);
        email.setMsg(emailMessage.getMessage());
        // Send
        email.setHostName(configuration.getSmtpHost());
        configureSecureConnection(email);
        if (StringUtils.isNotBlank(configuration.getSmtpUsername())
                || StringUtils.isNotBlank(configuration.getSmtpPassword())) {
            email.setAuthentication(configuration.getSmtpUsername(), configuration.getSmtpPassword());
        }
        email.setSocketConnectionTimeout(SOCKET_TIMEOUT);
        email.setSocketTimeout(SOCKET_TIMEOUT);
        email.send();

    } finally {
        Thread.currentThread().setContextClassLoader(classloader);
    }
}