Example usage for com.liferay.portal.util PropsValues MAIL_SESSION_MAIL_SMTP_USER

List of usage examples for com.liferay.portal.util PropsValues MAIL_SESSION_MAIL_SMTP_USER

Introduction

In this page you can find the example usage for com.liferay.portal.util PropsValues MAIL_SESSION_MAIL_SMTP_USER.

Prototype

String MAIL_SESSION_MAIL_SMTP_USER

To view the source code for com.liferay.portal.util PropsValues MAIL_SESSION_MAIL_SMTP_USER.

Click Source Link

Usage

From source file:com.liferay.mail.service.impl.MailServiceImpl.java

License:Open Source License

public Session getSession() throws SystemException {
    if (_session != null) {
        return _session;
    }//from  w  w  w .  ja v  a 2s.  co  m

    Session session = InfrastructureUtil.getMailSession();

    if (!PrefsPropsUtil.getBoolean(PropsKeys.MAIL_SESSION_MAIL)) {
        _session = session;

        return _session;
    }

    String advancedPropertiesString = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES,
            PropsValues.MAIL_SESSION_MAIL_ADVANCED_PROPERTIES);
    String pop3Host = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_POP3_HOST,
            PropsValues.MAIL_SESSION_MAIL_POP3_HOST);
    String pop3Password = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_POP3_PASSWORD,
            PropsValues.MAIL_SESSION_MAIL_POP3_PASSWORD);
    int pop3Port = PrefsPropsUtil.getInteger(PropsKeys.MAIL_SESSION_MAIL_POP3_PORT,
            PropsValues.MAIL_SESSION_MAIL_POP3_PORT);
    String pop3User = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_POP3_USER,
            PropsValues.MAIL_SESSION_MAIL_POP3_USER);
    String smtpHost = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_SMTP_HOST,
            PropsValues.MAIL_SESSION_MAIL_SMTP_HOST);
    String smtpPassword = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_SMTP_PASSWORD,
            PropsValues.MAIL_SESSION_MAIL_SMTP_PASSWORD);
    int smtpPort = PrefsPropsUtil.getInteger(PropsKeys.MAIL_SESSION_MAIL_SMTP_PORT,
            PropsValues.MAIL_SESSION_MAIL_SMTP_PORT);
    String smtpUser = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_SMTP_USER,
            PropsValues.MAIL_SESSION_MAIL_SMTP_USER);
    String storeProtocol = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_STORE_PROTOCOL,
            PropsValues.MAIL_SESSION_MAIL_STORE_PROTOCOL);
    String transportProtocol = PrefsPropsUtil.getString(PropsKeys.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL,
            PropsValues.MAIL_SESSION_MAIL_TRANSPORT_PROTOCOL);

    Properties properties = session.getProperties();

    // Incoming

    if (!storeProtocol.equals(Account.PROTOCOL_POPS)) {
        storeProtocol = Account.PROTOCOL_POP;
    }

    properties.setProperty("mail.store.protocol", storeProtocol);

    String storePrefix = "mail." + storeProtocol + ".";

    properties.setProperty(storePrefix + "host", pop3Host);
    properties.setProperty(storePrefix + "password", pop3Password);
    properties.setProperty(storePrefix + "port", String.valueOf(pop3Port));
    properties.setProperty(storePrefix + "user", pop3User);

    // Outgoing

    if (!transportProtocol.equals(Account.PROTOCOL_SMTPS)) {
        transportProtocol = Account.PROTOCOL_SMTP;
    }

    properties.setProperty("mail.transport.protocol", transportProtocol);

    String transportPrefix = "mail." + transportProtocol + ".";

    boolean smtpAuth = false;

    if (Validator.isNotNull(smtpPassword) || Validator.isNotNull(smtpUser)) {

        smtpAuth = true;
    }

    properties.setProperty(transportPrefix + "auth", String.valueOf(smtpAuth));
    properties.setProperty(transportPrefix + "host", smtpHost);
    properties.setProperty(transportPrefix + "password", smtpPassword);
    properties.setProperty(transportPrefix + "port", String.valueOf(smtpPort));
    properties.setProperty(transportPrefix + "user", smtpUser);

    // Advanced

    try {
        if (Validator.isNotNull(advancedPropertiesString)) {
            Properties advancedProperties = PropertiesUtil.load(advancedPropertiesString);

            Iterator<Map.Entry<Object, Object>> itr = advancedProperties.entrySet().iterator();

            while (itr.hasNext()) {
                Map.Entry<Object, Object> entry = itr.next();

                String key = (String) entry.getKey();
                String value = (String) entry.getValue();

                properties.setProperty(key, value);
            }
        }
    } catch (IOException ioe) {
        if (_log.isWarnEnabled()) {
            _log.warn(ioe, ioe);
        }
    }

    _session = Session.getInstance(properties);

    return _session;
}