Search in sources :

Example 1 with Authenticator

use of javax.mail.Authenticator in project japid42 by branaway.

the class Mail method getSession.

public static Session getSession() {
    if (session == null) {
        Properties props = new Properties();
        // Put a bogus value even if we are on dev mode, otherwise JavaMail
        // will complain
        props.put("mail.smtp.host", getConfigOr("mail.smtp.host", "localhost"));
        String channelEncryption;
        if (getConfigOr("mail.smtp.protocol", "smtp").equals("smtps")) {
            // Backward compatibility before stable5
            channelEncryption = "starttls";
        } else {
            channelEncryption = getConfigOr("mail.smtp.channel", "clear");
        }
        if (channelEncryption.equals("clear")) {
            props.put("mail.smtp.port", "25");
        } else if (channelEncryption.equals("ssl")) {
            // port 465 + setup yes ssl socket factory (won't verify that
            // the server certificate is signed with a root ca.)
            props.put("mail.smtp.port", "465");
            props.put("mail.smtp.socketFactory.port", "465");
            props.put("mail.smtp.socketFactory.class", "play.utils.YesSSLSocketFactory");
            props.put("mail.smtp.socketFactory.fallback", "false");
        } else if (channelEncryption.equals("starttls")) {
            // port 25 + enable starttls + ssl socket factory
            props.put("mail.smtp.port", "25");
            props.put("mail.smtp.starttls.enable", "true");
        // can't install our socket factory. will work only with server
        // that has a signed certificate
        // story to be continued in javamail 1.4.2 :
        // https://glassfish.dev.java.net/issues/show_bug.cgi?id=5189
        }
        if (getConfig("mail.smtp.localhost") != null) {
            props.put("mail.smtp.localhost", // override
            getConfig("mail.smtp.localhost"));
        // defaults
        }
        String sockfac = getConfig("mail.smtp.socketFactory.class");
        if (sockfac != null) {
            props.put("mail.smtp.socketFactory.class", sockfac);
        }
        String port = getConfig("mail.smtp.port");
        if (port != null) {
            props.put("mail.smtp.port", port);
        }
        String user = getConfigOr("mail.smtp.user", null);
        String password = getConfigOr("mail.smtp.pass", null);
        if (password == null) {
            // Fallback to old convention
            password = getConfigOr("mail.smtp.password", null);
        }
        String authenticator = getConfig("mail.smtp.authenticator");
        session = null;
        if (authenticator != null) {
            props.put("mail.smtp.auth", "true");
            try {
                session = Session.getInstance(props, (Authenticator) Play.application().classloader().loadClass(authenticator).newInstance());
            } catch (Exception e) {
                Logger.error("Cannot instanciate custom SMTP authenticator: " + authenticator, e);
            }
        }
        if (session == null) {
            if (user != null && password != null) {
                props.put("mail.smtp.auth", "true");
                session = Session.getInstance(props, new SMTPAuthenticator(user, password));
            } else {
                props.remove("mail.smtp.auth");
                session = Session.getInstance(props);
            }
        }
        if (Boolean.parseBoolean(getConfigOr("mail.debug", "false"))) {
            session.setDebug(true);
        }
    }
    return session;
}
Also used : Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) TimeoutException(java.util.concurrent.TimeoutException) MessagingException(javax.mail.MessagingException) MailException(cn.bran.play.exceptions.MailException) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) EmailException(org.apache.commons.mail.EmailException)

Example 2 with Authenticator

use of javax.mail.Authenticator in project gocd by gocd.

the class Pop3MailClient method getInboxFolder.

private Folder getInboxFolder() throws MessagingException {
    Properties pop3Props = new Properties();
    pop3Props.setProperty("mail.pop3.host", host);
    Authenticator auth = new PopupAuthenticator();
    Session session = Session.getInstance(pop3Props, auth);
    URLName url = new URLName("pop3", host, port, "", username, password);
    Store store = session.getStore(url);
    store.connect();
    Folder folder = store.getFolder("INBOX");
    folder.open(Folder.READ_WRITE);
    return folder;
}
Also used : URLName(javax.mail.URLName) Store(javax.mail.Store) Properties(java.util.Properties) Folder(javax.mail.Folder) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session)

Example 3 with Authenticator

use of javax.mail.Authenticator in project opennms by OpenNMS.

the class JavaMailer2 method createAuthenticator.

/**
     * Helper method to create an Authenticator based on Password Authentication
     *
     * @param user a {@link java.lang.String} object.
     * @param password a {@link java.lang.String} object.
     * @return a {@link javax.mail.Authenticator} object.
     */
public Authenticator createAuthenticator(final String user, final String password) {
    Authenticator auth;
    auth = new Authenticator() {

        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password);
        }
    };
    return auth;
}
Also used : Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 4 with Authenticator

use of javax.mail.Authenticator in project Xponents by OpenSextant.

the class MailClient method connect.

/**
     * Connect to the mail server and establish the user + folder session.
     * @throws MessagingException javamail error
     */
public void connect() throws MessagingException {
    logger.debug("host: {}, user: {}", config.getHost(), config.getUsername());
    logger.debug("setting session to debug: {}", config.isDebug());
    logger.debug("mailBoxFolder: {}", config.getMailFolder());
    // session = Session.getDefaultInstance(properties);
    Authenticator auth = config.getExchangeServerVersion() > 0 ? new NTLMAuth(config.getUsername(), config.getPassword()) : null;
    session = Session.getInstance(config, auth);
    session.setDebug(config.isDebug());
    store = session.getStore((config.isSSLEnabled() ? "imaps" : "imap"));
    store.connect(config.getHost(), config.getUsername(), config.getPassword());
    logger.debug("session connected?: " + store.isConnected());
    // Get folder
    folder = store.getFolder(config.getMailFolder());
    if (config.isReadOnly()) {
        folder.open(Folder.READ_ONLY);
        logger.debug("opened folder in READ_ONLY mode");
    } else {
        folder.open(Folder.READ_WRITE);
        logger.debug("opened folder in READ_WRITE mode");
    }
}
Also used : Authenticator(javax.mail.Authenticator)

Example 5 with Authenticator

use of javax.mail.Authenticator in project jmeter by apache.

the class MailerModel method sendMail.

/**
     * Sends a mail with the given parameters using SMTP.
     *
     * @param from
     *            the sender of the mail as shown in the mail-client.
     * @param vEmails
     *            all receivers of the mail. The receivers are seperated by
     *            commas.
     * @param subject
     *            the subject of the mail.
     * @param attText
     *            the message-body.
     * @param smtpHost
     *            the smtp-server used to send the mail.
     * @param smtpPort the smtp-server port used to send the mail.
     * @param user the login used to authenticate
     * @param password the password used to authenticate
     * @param mailAuthType {@link MailAuthType} Security policy
     * @param debug Flag whether debug messages for the mail session should be generated
     * @throws AddressException If mail address is wrong
     * @throws MessagingException If building MimeMessage fails
     */
public void sendMail(String from, List<String> vEmails, String subject, String attText, String smtpHost, String smtpPort, final String user, final String password, MailAuthType mailAuthType, boolean debug) throws MessagingException {
    InternetAddress[] address = new InternetAddress[vEmails.size()];
    for (int k = 0; k < vEmails.size(); k++) {
        address[k] = new InternetAddress(vEmails.get(k));
    }
    // create some properties and get the default Session
    Properties props = new Properties();
    props.put(MAIL_SMTP_HOST, smtpHost);
    // property values are strings
    props.put(MAIL_SMTP_PORT, smtpPort);
    Authenticator authenticator = null;
    if (mailAuthType != MailAuthType.NONE) {
        props.put(MAIL_SMTP_AUTH, "true");
        switch(mailAuthType) {
            case SSL:
                props.put(MAIL_SMTP_SOCKETFACTORY_CLASS, "javax.net.ssl.SSLSocketFactory");
                break;
            case TLS:
                props.put(MAIL_SMTP_STARTTLS, "true");
                break;
            default:
                break;
        }
    }
    if (!StringUtils.isEmpty(user)) {
        authenticator = new javax.mail.Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        };
    }
    Session session = Session.getInstance(props, authenticator);
    session.setDebug(debug);
    // create a message
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(from));
    msg.setRecipients(Message.RecipientType.TO, address);
    msg.setSubject(subject);
    msg.setText(attText);
    Transport.send(msg);
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Aggregations

Authenticator (javax.mail.Authenticator)17 Properties (java.util.Properties)12 Session (javax.mail.Session)12 MessagingException (javax.mail.MessagingException)10 PasswordAuthentication (javax.mail.PasswordAuthentication)9 InternetAddress (javax.mail.internet.InternetAddress)9 MimeMessage (javax.mail.internet.MimeMessage)9 IOException (java.io.IOException)7 Message (javax.mail.Message)5 MimeMultipart (javax.mail.internet.MimeMultipart)5 MimeBodyPart (javax.mail.internet.MimeBodyPart)4 Date (java.util.Date)3 DataHandler (javax.activation.DataHandler)3 Folder (javax.mail.Folder)2 Multipart (javax.mail.Multipart)2 Store (javax.mail.Store)2 URLName (javax.mail.URLName)2 MailException (cn.bran.play.exceptions.MailException)1 PropertiesUtil (com.myspringboot.utils.PropertiesUtil)1 IMAPFolder (com.sun.mail.imap.IMAPFolder)1