Search in sources :

Example 31 with Session

use of javax.mail.Session in project zm-mailbox by Zimbra.

the class DataSourceManager method test.

/*
     * Tests connecting to a data source.  Do not actually create the
     * data source.
     */
public static void test(DataSource ds) throws ServiceException {
    ZimbraLog.datasource.info("Testing: %s", ds);
    try {
        DataImport di = getInstance().getDataImport(ds, true);
        di.test();
        if (ds.isSmtpEnabled()) {
            Session session = JMSession.getSession(ds);
            Transport smtp = session.getTransport();
            String emailAddress = ds.getEmailAddress();
            if (smtp instanceof SmtpTransport) {
                test((SmtpTransport) smtp, emailAddress);
            } else {
                test((SMTPTransport) smtp, emailAddress);
            }
        }
        ZimbraLog.datasource.info("Test succeeded: %s", ds);
    } catch (ServiceException x) {
        ZimbraLog.datasource.info("Test failed: %s", ds, x);
        throw x;
    } catch (Exception e) {
        ZimbraLog.datasource.info("Test failed: %s", ds, e);
        throw ServiceException.INVALID_REQUEST("Datasource test failed", e);
    }
}
Also used : DataImport(com.zimbra.cs.account.DataSource.DataImport) ServiceException(com.zimbra.common.service.ServiceException) SmtpTransport(com.zimbra.cs.mailclient.smtp.SmtpTransport) Transport(javax.mail.Transport) SmtpTransport(com.zimbra.cs.mailclient.smtp.SmtpTransport) SMTPTransport(com.sun.mail.smtp.SMTPTransport) MessagingException(javax.mail.MessagingException) ServiceException(com.zimbra.common.service.ServiceException) JMSession(com.zimbra.cs.util.JMSession) Session(javax.mail.Session)

Example 32 with Session

use of javax.mail.Session in project Activiti by Activiti.

the class JndiEmailTest method setUp.

@BeforeClass
public void setUp() {
    Properties props = new Properties();
    props.put("mail.transport.protocol", "smtp");
    props.put("mail.smtp.provider.class", MockEmailTransport.class.getName());
    props.put("mail.smtp.class", MockEmailTransport.class.getName());
    props.put("mail.smtp.provider.vendor", "test");
    props.put("mail.smtp.provider.version", "0.0.0");
    Provider provider = new Provider(Type.TRANSPORT, "smtp", MockEmailTransport.class.getName(), "test", "1.0");
    Session mailSession = Session.getDefaultInstance(props);
    SimpleNamingContextBuilder builder = null;
    try {
        mailSession.setProvider(provider);
        builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
        builder.bind("java:comp/env/Session", mailSession);
    } catch (NamingException e) {
        logger.error("Naming error in email setup", e);
    } catch (NoSuchProviderException e) {
        logger.error("provider error in email setup", e);
    }
}
Also used : SimpleNamingContextBuilder(org.springframework.mock.jndi.SimpleNamingContextBuilder) NamingException(javax.naming.NamingException) Properties(java.util.Properties) NoSuchProviderException(javax.mail.NoSuchProviderException) Provider(javax.mail.Provider) Session(javax.mail.Session) BeforeClass(org.junit.BeforeClass)

Example 33 with Session

use of javax.mail.Session in project zm-mailbox by Zimbra.

the class JMSession method getSmtpSession.

/**
     * Returns the JavaMail SMTP {@link Session} with settings from the given
     * account and its domain.
     */
public static Session getSmtpSession(Account account) throws MessagingException {
    Domain domain = null;
    if (account != null) {
        try {
            domain = Provisioning.getInstance().getDomain(account);
        } catch (ServiceException e) {
            ZimbraLog.smtp.warn("Unable to look up domain for account %s.", account.getName(), e);
        }
    }
    Session session = getSmtpSession(domain);
    if (account != null && account.isSmtpEnableTrace()) {
        session.setDebug(true);
    }
    return session;
}
Also used : ServiceException(com.zimbra.common.service.ServiceException) Domain(com.zimbra.cs.account.Domain) Session(javax.mail.Session)

Example 34 with Session

use of javax.mail.Session in project zm-mailbox by Zimbra.

the class JMSession method getSession.

public static Session getSession(DataSource ds) throws ServiceException {
    String smtpHost = ds.getSmtpHost();
    int smtpPort = ds.getSmtpPort();
    boolean isAuthRequired = ds.isSmtpAuthRequired();
    String smtpUser = ds.getSmtpUsername();
    String smtpPass = ds.getDecryptedSmtpPassword();
    if (DataSourceAuthMechanism.XOAUTH2.name().equalsIgnoreCase(ds.getAuthMechanism())) {
        smtpPass = ds.getDecryptedOAuthToken();
    }
    boolean useSSL = ds.isSmtpConnectionSecure();
    if (smtpHost == null || smtpHost.length() == 0) {
        throw ServiceException.FAILURE("null smtp host", null);
    }
    if (smtpPort <= 0) {
        throw ServiceException.FAILURE("invalid smtp port", null);
    }
    if (isAuthRequired && (smtpUser == null || smtpUser.length() == 0 || smtpPass == null || smtpPass.length() == 0)) {
        throw ServiceException.FAILURE("missing smtp username or password", null);
    }
    long timeout = LC.javamail_smtp_timeout.longValue() * Constants.MILLIS_PER_SECOND;
    String localhost = LC.zimbra_server_hostname.value();
    Properties props = new Properties();
    Session session;
    props.put("mail.smtp.socketFactory", SocketFactories.defaultSocketFactory());
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.put("mail.smtp.ssl.socketFactory", SocketFactories.defaultSSLSocketFactory());
    props.setProperty("mail.smtp.ssl.socketFactory.fallback", "false");
    props.put("mail.smtps.ssl.socketFactory", SocketFactories.defaultSSLSocketFactory());
    props.setProperty("mail.smtps.ssl.socketFactory.fallback", "false");
    if (useSSL) {
        props.setProperty("mail.transport.protocol", "smtps");
        props.setProperty("mail.smtps.connectiontimeout", Long.toString(timeout));
        props.setProperty("mail.smtps.timeout", Long.toString(timeout));
        props.setProperty("mail.smtps.localhost", localhost);
        props.setProperty("mail.smtps.sendpartial", "true");
        props.setProperty("mail.smtps.host", smtpHost);
        props.setProperty("mail.smtps.port", smtpPort + "");
        if (isAuthRequired) {
            props.setProperty("mail.smtps.auth", "true");
            props.setProperty("mail.smtps.user", smtpUser);
            props.setProperty("mail.smtps.password", smtpPass);
            if (DataSourceAuthMechanism.XOAUTH2.name().equalsIgnoreCase(ds.getAuthMechanism())) {
                addOAuth2Properties(smtpPass, props, "smtps");
            }
            session = Session.getInstance(props, new SmtpAuthenticator(smtpUser, smtpPass));
        } else {
            session = Session.getInstance(props);
        }
        session.setProtocolForAddress("rfc822", "smtps");
    } else {
        props.setProperty("mail.transport.protocol", "smtp");
        props.setProperty("mail.smtp.connectiontimeout", Long.toString(timeout));
        props.setProperty("mail.smtp.timeout", Long.toString(timeout));
        props.setProperty("mail.smtp.localhost", localhost);
        props.setProperty("mail.smtp.sendpartial", "true");
        props.setProperty("mail.smtp.host", smtpHost);
        props.setProperty("mail.smtp.port", smtpPort + "");
        if (LC.javamail_smtp_enable_starttls.booleanValue()) {
            props.setProperty("mail.smtp.starttls.enable", "true");
        // props.put("mail.smtp.socketFactory.class", TlsSocketFactory.getInstance());
        }
        if (isAuthRequired) {
            props.setProperty("mail.smtp.auth", "true");
            props.setProperty("mail.smtp.user", smtpUser);
            props.setProperty("mail.smtp.password", smtpPass);
            if (DataSourceAuthMechanism.XOAUTH2.name().equalsIgnoreCase(ds.getAuthMechanism())) {
                addOAuth2Properties(smtpPass, props, "smtp");
            }
            session = Session.getInstance(props, new SmtpAuthenticator(smtpUser, smtpPass));
        } else {
            session = Session.getInstance(props);
        }
        session.setProtocolForAddress("rfc822", "smtp");
    }
    if (LC.javamail_smtp_debug.booleanValue()) {
        session.setDebug(true);
    }
    JMSession.setProviders(session);
    return session;
}
Also used : Properties(java.util.Properties) Session(javax.mail.Session)

Example 35 with Session

use of javax.mail.Session in project zm-mailbox by Zimbra.

the class JMSession method getSmtpSession.

/**
     * Returns a new JavaMail {@link Session} that has the latest SMTP settings
     * from LDAP. Settings are retrieved from the local server and overridden by
     * the domain.
     *
     * @param domain the domain, or {@code null} to use server settings
     */
private static Session getSmtpSession(Domain domain) throws MessagingException {
    Server server;
    try {
        server = Provisioning.getInstance().getLocalServer();
    } catch (ServiceException e) {
        throw new MessagingException("Unable to initialize JavaMail session", e);
    }
    Properties props = getJavaMailSessionProperties(server, domain);
    Session session = Session.getInstance(props);
    setProviders(session);
    if (LC.javamail_smtp_debug.booleanValue()) {
        session.setDebug(true);
    }
    return session;
}
Also used : Server(com.zimbra.cs.account.Server) ServiceException(com.zimbra.common.service.ServiceException) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Session(javax.mail.Session)

Aggregations

Session (javax.mail.Session)110 MimeMessage (javax.mail.internet.MimeMessage)72 Properties (java.util.Properties)66 InternetAddress (javax.mail.internet.InternetAddress)49 MessagingException (javax.mail.MessagingException)47 Message (javax.mail.Message)31 Test (org.junit.Test)30 Transport (javax.mail.Transport)28 JMSession (com.zimbra.cs.util.JMSession)20 PasswordAuthentication (javax.mail.PasswordAuthentication)19 Date (java.util.Date)17 MimeBodyPart (javax.mail.internet.MimeBodyPart)16 MimeMultipart (javax.mail.internet.MimeMultipart)16 ZMimeMessage (com.zimbra.common.zmime.ZMimeMessage)15 IOException (java.io.IOException)15 SharedByteArrayInputStream (javax.mail.util.SharedByteArrayInputStream)15 Authenticator (javax.mail.Authenticator)12 Multipart (javax.mail.Multipart)11 NoSuchProviderException (javax.mail.NoSuchProviderException)10 BodyPart (javax.mail.BodyPart)9