Example usage for com.liferay.portal.kernel.util InfrastructureUtil getMailSession

List of usage examples for com.liferay.portal.kernel.util InfrastructureUtil getMailSession

Introduction

In this page you can find the example usage for com.liferay.portal.kernel.util InfrastructureUtil getMailSession.

Prototype

public static Session getMailSession() 

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   www . ja  v a2  s .  c  o  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;
}

From source file:com.liferay.petra.mail.MailEngine.java

License:Open Source License

public static Session getSession(boolean cache) {
    Session session = null;/*w w  w  .j ava2  s.c om*/

    try {
        session = MailServiceUtil.getSession();
    } catch (SystemException se) {
        if (_log.isWarnEnabled()) {
            _log.warn(se, se);
        }

        session = InfrastructureUtil.getMailSession();
    }

    if (_log.isDebugEnabled()) {
        session.setDebug(true);

        Properties properties = session.getProperties();

        properties.list(System.out);
    }

    return session;
}

From source file:com.liferay.util.mail.MailEngine.java

License:Open Source License

public static Session getSession(boolean cache) {
    Session session = null;//from   w  w w  .ja va 2s . c  o  m

    try {
        session = MailServiceUtil.getSession();
    } catch (SystemException se) {
        if (_log.isWarnEnabled()) {
            _log.warn(se, se);
        }

        session = InfrastructureUtil.getMailSession();
    }

    if (_log.isDebugEnabled()) {
        session.setDebug(true);

        session.getProperties().list(System.out);
    }

    return session;
}