Search in sources :

Example 11 with Authenticator

use of javax.mail.Authenticator in project suite by stupidsing.

the class SmtpSslGmail method send.

public void send(String to, String subject, String body) {
    Constants.bindSecrets("gmail .0 .1").map((username, enc) -> {
        String password = decode(System.getenv("USER").toCharArray(), enc);
        Properties props = new Properties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.port", "465");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", SSLSocketFactory.class.getName());
        Session session = Session.getDefaultInstance(props, new Authenticator() {

            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(username, password);
            }
        });
        try {
            String sender = username + "@gmail.com";
            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress(sender));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to != null ? to : sender));
            message.setSubject(subject);
            message.setText(body);
            Transport.send(message);
        } catch (MessagingException e) {
            Fail.t(e);
        }
        return true;
    });
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) Authenticator(javax.mail.Authenticator) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 12 with Authenticator

use of javax.mail.Authenticator in project apex-malhar by apache.

the class SmtpOutputOperator method reset.

private void reset() {
    if (!setupCalled) {
        return;
    }
    if (!StringUtils.isBlank(smtpPassword)) {
        properties.setProperty("mail.smtp.auth", "true");
        properties.setProperty("mail.smtp.starttls.enable", "true");
        if (useSsl) {
            properties.setProperty("mail.smtp.socketFactory.port", String.valueOf(smtpPort));
            properties.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
            properties.setProperty("mail.smtp.socketFactory.fallback", "false");
        }
        auth = new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(smtpUserName, smtpPassword);
            }
        };
    }
    properties.setProperty("mail.smtp.host", smtpHost);
    properties.setProperty("mail.smtp.port", String.valueOf(smtpPort));
    session = Session.getInstance(properties, auth);
    resetMessage();
}
Also used : Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication)

Example 13 with Authenticator

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

the class EmailService method lowerCase.

@POST
public String lowerCase(final String message) {
    try {
        // Create some properties and get the default Session
        final Properties props = new Properties();
        props.put("mail.smtp.host", "your.mailserver.host");
        props.put("mail.debug", "true");
        final Session session = Session.getInstance(props, new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication("MyUsername", "MyPassword");
            }
        });
        // Set this just to see some internal logging
        session.setDebug(true);
        // Create a message
        final MimeMessage msg = new MimeMessage(session);
        msg.setFrom(new InternetAddress("your@email.address"));
        final InternetAddress[] address = { new InternetAddress("user@provider.com") };
        msg.setRecipients(Message.RecipientType.TO, address);
        msg.setSubject("JavaMail API test");
        msg.setSentDate(new Date());
        msg.setText(message, "UTF-8");
        Transport.send(msg);
    } catch (final MessagingException e) {
        return "Failed to send message: " + e.getMessage();
    }
    return "Sent";
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MimeMessage(javax.mail.internet.MimeMessage) MessagingException(javax.mail.MessagingException) Properties(java.util.Properties) Authenticator(javax.mail.Authenticator) Date(java.util.Date) Session(javax.mail.Session) PasswordAuthentication(javax.mail.PasswordAuthentication) POST(javax.ws.rs.POST)

Example 14 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 15 with Authenticator

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

the class MailSessionFactory method create.

public Session create() {
    final String password = properties.getProperty("password");
    Authenticator auth = null;
    if (password != null) {
        final String protocol = properties.getProperty("mail.transport.protocol", "smtp");
        String user = properties.getProperty("mail." + protocol + ".user");
        if (user == null) {
            user = properties.getProperty("mail.user");
        }
        if (user != null) {
            final PasswordAuthentication pa = new PasswordAuthentication(user, password);
            auth = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return pa;
                }
            };
        }
    }
    if (useDefault) {
        if (auth != null) {
            return Session.getDefaultInstance(properties, auth);
        }
        return Session.getDefaultInstance(properties);
    }
    if (auth != null) {
        return Session.getInstance(properties, auth);
    }
    return Session.getInstance(properties);
}
Also used : Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication)

Aggregations

Authenticator (javax.mail.Authenticator)24 Properties (java.util.Properties)18 Session (javax.mail.Session)16 MessagingException (javax.mail.MessagingException)14 PasswordAuthentication (javax.mail.PasswordAuthentication)13 InternetAddress (javax.mail.internet.InternetAddress)13 MimeMessage (javax.mail.internet.MimeMessage)13 MimeMultipart (javax.mail.internet.MimeMultipart)8 IOException (java.io.IOException)7 Message (javax.mail.Message)7 MimeBodyPart (javax.mail.internet.MimeBodyPart)7 Date (java.util.Date)4 DataHandler (javax.activation.DataHandler)4 Multipart (javax.mail.Multipart)4 File (java.io.File)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 FileDataSource (javax.activation.FileDataSource)2 Folder (javax.mail.Folder)2 Store (javax.mail.Store)2 URLName (javax.mail.URLName)2