com.szmslab.quickjavamail.utils.MailProperties.java Source code

Java tutorial

Introduction

Here is the source code for com.szmslab.quickjavamail.utils.MailProperties.java

Source

/*
 * Copyright (c) 2014 szmslab
 *
 * This software is released under the MIT License.
 * http://opensource.org/licenses/mit-license.php
 */
package com.szmslab.quickjavamail.utils;

import java.security.GeneralSecurityException;
import java.util.Iterator;
import java.util.Map;
import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.net.ssl.SSLSocketFactory;

import org.apache.commons.lang3.StringUtils;

import com.sun.mail.util.MailSSLSocketFactory;

/**
 * JavaMail?????
 *
 * @author szmslab
 */
abstract public class MailProperties {

    /**
     * JavaMail?
     */
    private Properties properties = new Properties();

    /**
     * 
     */
    private String protocol;

    /**
     * ?
     */
    private Authenticator authenticator;

    /**
     * ??
     */
    public MailProperties() {
        this.protocol = getDefaultProtocol();
        connectiontimeout("60000");
        timeout("60000");
    }

    /**
     * JavaMail?????
     *
     * @return JavaMail?
     */
    public Properties getProperties() {
        return properties;
    }

    /**
     * ????
     *
     * @return 
     */
    public String getProtocol() {
        return protocol;
    }

    /**
     * ?????
     *
     * @return ?
     */
    public Authenticator getAuthenticator() {
        return authenticator;
    }

    /**
     * ?????
     *
     * @param host
     *            ??
     * @return ?
     */
    public MailProperties host(String host) {
        setString("mail.host", host);
        setString(String.format("mail.%s.host", protocol), host);
        return this;
    }

    /**
     * ?????
     *
     * @param port
     *            ??
     * @return ?
     */
    public MailProperties port(String port) {
        // SMTP25?SMTP over SSL465?STARTTLS587
        // POP3(STARTTLS):110?POP3 over SSL:995?IMAP(STARTTLS):143?IMAP over SSL:993
        setString(String.format("mail.%s.port", protocol), port);
        return this;
    }

    /**
     * ???
     *
     * @param connectiontimeout
     *            
     * @return ?
     */
    public MailProperties connectiontimeout(String connectiontimeout) {
        setString(String.format("mail.%s.connectiontimeout", protocol), connectiontimeout);
        return this;
    }

    /**
     * ?????
     *
     * @param timeout
     *            ??
     * @return ?
     */
    public MailProperties timeout(String timeout) {
        setString(String.format("mail.%s.timeout", protocol), timeout);
        return this;
    }

    /**
     * ????
     *
     * @param userName
     *            ??
     * @param password
     *            
     * @return ?
     */
    public MailProperties authenticate(final String userName, final String password) {
        if (StringUtils.isNotBlank(userName) && StringUtils.isNotBlank(password)) {
            authenticator = new Authenticator() {
                @Override
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(userName, password);
                }
            };
            if (protocol.startsWith("smtp")) {
                setString(String.format("mail.%s.auth", protocol), Boolean.TRUE.toString());
            }
        }
        return this;
    }

    /**
     * STARTTLS???
     *
     * @return ?
     */
    public MailProperties starttls() {
        starttls(false);
        return this;
    }

    /**
     * STARTTLS???
     *
     * @param isTrustedHost
     *            ????????
     * @return ?
     */
    public MailProperties starttls(boolean isTrustedHost) {
        setString(String.format("mail.%s.starttls.enable", protocol), Boolean.TRUE.toString());
        if (isTrustedHost) {
            setString(String.format("mail.%s.ssl.trust", protocol),
                    getString(String.format("mail.%s.host", protocol)));
        }
        return this;
    }

    /**
     * SSL???
     *
     * @return ?
     * @throws GeneralSecurityException
     */
    public MailProperties ssl() throws GeneralSecurityException {
        ssl(false);
        return this;
    }

    /**
     * SSL???
     *
     * @param isTrustedHost
     *            ????????
     * @return ?
     * @throws GeneralSecurityException
     */
    public MailProperties ssl(boolean isTrustedHost) throws GeneralSecurityException {
        String beforeProtocol = protocol;
        protocol = getSslProtocol();
        Properties newProperties = new Properties();
        for (Iterator<Map.Entry<Object, Object>> itr = properties.entrySet().iterator(); itr.hasNext();) {
            Map.Entry<Object, Object> entry = itr.next();
            String key = (String) entry.getKey();
            key = key.replaceAll(beforeProtocol, protocol);
            newProperties.put(key, entry.getValue());
        }
        properties = newProperties;

        setString(String.format("mail.%s.ssl.enable", protocol), Boolean.TRUE.toString());
        if (isTrustedHost) {
            MailSSLSocketFactory factory = new MailSSLSocketFactory();
            factory.setTrustedHosts(new String[] { getString(String.format("mail.%s.host", protocol)) });
            setObject(String.format("mail.%s.socketFactory", protocol), factory);
        } else {
            setString(String.format("mail.%s.socketFactory.class", protocol), SSLSocketFactory.class.getName());
        }
        setString(String.format("mail.%s.socketFactory.fallback", protocol), Boolean.FALSE.toString());
        setString(String.format("mail.%s.socketFactory.port", protocol),
                getString(String.format("mail.%s.port", protocol)));
        return this;
    }

    /**
     * ?????
     *
     * @return ?
     */
    abstract protected String getDefaultProtocol();

    /**
     * SSL?????
     *
     * @return SSL?
     */
    abstract protected String getSslProtocol();

    /**
     * JavaMail????
     *
     * @param key
     *            JavaMail???
     * @param value
     *            ?
     */
    protected void setString(String key, String value) {
        if (StringUtils.isNotBlank(value)) {
            properties.setProperty(key, value);
        }
    }

    /**
     * JavaMail?????
     *
     * @param key
     *            JavaMail???
     * @return ?
     */
    protected String getString(String key) {
        return properties.getProperty(key);
    }

    /**
     * JavaMail????
     *
     * @param key
     *            JavaMail???
     * @param value
     *            ?
     */
    protected void setObject(String key, Object value) {
        if (value != null) {
            properties.put(key, value);
        }
    }

    /**
     * JavaMail?????
     *
     * @param key
     *            JavaMail???
     * @return ?
     */
    protected Object getObject(String key) {
        return properties.get(key);
    }

    /* (? Javadoc)
     * @see java.lang.Object#toString()
     */
    @Override
    public String toString() {
        return properties.toString();
    }

}