Search in sources :

Example 16 with Authenticator

use of javax.mail.Authenticator in project maven-plugins by apache.

the class ProjectJavamailMailSender method send.

// ----------------------------------------------------------------------
// MailSender Implementation
// ----------------------------------------------------------------------
public void send(MailMessage mail) throws MailSenderException {
    verify(mail);
    try {
        Authenticator auth = null;
        if (getUsername() != null) {
            auth = new Authenticator() {

                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(getUsername(), getPassword());
                }
            };
        }
        Session session = Session.getDefaultInstance(props, auth);
        session.setDebug(getLogger().isDebugEnabled());
        Message msg = new MimeMessage(session);
        InternetAddress addressFrom = new InternetAddress(mail.getFrom().getRfc2822Address());
        msg.setFrom(addressFrom);
        if (mail.getToAddresses().size() > 0) {
            InternetAddress[] addressTo = new InternetAddress[mail.getToAddresses().size()];
            int count = 0;
            for (Object o : mail.getToAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressTo[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.TO, addressTo);
        }
        if (mail.getCcAddresses().size() > 0) {
            InternetAddress[] addressCc = new InternetAddress[mail.getCcAddresses().size()];
            int count = 0;
            for (Object o : mail.getCcAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressCc[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.CC, addressCc);
        }
        if (mail.getBccAddresses().size() > 0) {
            InternetAddress[] addressBcc = new InternetAddress[mail.getBccAddresses().size()];
            int count = 0;
            for (Object o : mail.getBccAddresses()) {
                String address = ((MailMessage.Address) o).getRfc2822Address();
                addressBcc[count++] = new InternetAddress(address);
            }
            msg.setRecipients(Message.RecipientType.BCC, addressBcc);
        }
        // Setting the Subject and Content Type
        msg.setSubject(mail.getSubject());
        msg.setContent(mail.getContent(), mail.getContentType() == null ? "text/plain" : mail.getContentType());
        if (mail.getSendDate() != null) {
            msg.setHeader("Date", DateFormatUtils.getDateHeader(mail.getSendDate()));
        } else {
            msg.setHeader("Date", DateFormatUtils.getDateHeader(new Date()));
        }
        // Send the message
        Transport.send(msg);
    } catch (MessagingException e) {
        throw new MailSenderException("Error while sending mail.", e);
    }
}
Also used : InternetAddress(javax.mail.internet.InternetAddress) MailSenderException(org.codehaus.plexus.mailsender.MailSenderException) Message(javax.mail.Message) MimeMessage(javax.mail.internet.MimeMessage) MailMessage(org.codehaus.plexus.mailsender.MailMessage) InternetAddress(javax.mail.internet.InternetAddress) MessagingException(javax.mail.MessagingException) Date(java.util.Date) MimeMessage(javax.mail.internet.MimeMessage) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Example 17 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)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