Example usage for javax.mail PasswordAuthentication PasswordAuthentication

List of usage examples for javax.mail PasswordAuthentication PasswordAuthentication

Introduction

In this page you can find the example usage for javax.mail PasswordAuthentication PasswordAuthentication.

Prototype

public PasswordAuthentication(String userName, String password) 

Source Link

Document

Initialize a new PasswordAuthentication

Usage

From source file:org.eclipse.ecr.automation.core.mail.Mailer.java

/**
 * Create a mailer using a session that lookup for the session in JNDI
 * under the given session name. If the JNDI binding doesn't exists use the
 * given properties to cinfiugure the session.
 *
 * @param sessionName//  w  ww.  j a v  a 2 s.c  o m
 * @param config
 */
public Mailer(String sessionName, Properties config) {
    this.config = config;
    this.sessionName = sessionName;
    final String user = config.getProperty("mail.smtp.user");
    final String pass = config.getProperty("mail.smtp.password");
    if (user != null && pass != null) {
        setAuthenticator(new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, pass);
            }
        });
    }
}

From source file:org.unitime.commons.Email.java

public Email() {
    Properties p = ApplicationProperties.getProperties();
    if (p.getProperty("mail.smtp.host") == null && p.getProperty("tmtbl.smtp.host") != null)
        p.setProperty("mail.smtp.host", p.getProperty("tmtbl.smtp.host"));

    final String user = ApplicationProperties.getProperty("mail.smtp.user", ApplicationProperties
            .getProperty("unitime.email.user", ApplicationProperties.getProperty("tmtbl.mail.user")));
    final String password = ApplicationProperties.getProperty("mail.smtp.password", ApplicationProperties
            .getProperty("unitime.email.password", ApplicationProperties.getProperty("tmtbl.mail.pwd")));

    Authenticator a = null;/*from   w  ww .j  a  v  a2 s  . c  o  m*/
    if (user != null && password != null) {
        p.setProperty("mail.smtp.user", user);
        p.setProperty("mail.smtp.auth", "true");
        a = new Authenticator() {
            public PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(user, password);
            }
        };
    }

    iMailSession = javax.mail.Session.getDefaultInstance(p, a);
    iMail = new MimeMessage(iMailSession);
    iBody = new MimeMultipart();
}

From source file:org.geoserver.wps.mail.SendMail.java

/**
 * Send an EMail to a specified address.
 * //www.  jav a 2s .c o  m
 * @param address the to address
 * @param subject the email address
 * @param body message to send
 * @throws MessagingException the messaging exception
 * @throws IOException Signals that an I/O exception has occurred.
 */
public void send(String address, String subject, String body) {
    try {
        // Session session = Session.getDefaultInstance(props, null);
        Session session = Session.getDefaultInstance(props,
                (conf.getMailSmtpAuth().equalsIgnoreCase("true") ? new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(conf.getUserName(), conf.getPassword());
                    }
                } : null));

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress(conf.getFromAddress(), conf.getFromAddressname()));
        message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(address));
        message.setSubject(subject);
        message.setText(body.toString());

        Transport.send(message);

    } catch (Exception e) {
        if (LOGGER.isLoggable(Level.SEVERE)) {
            LOGGER.log(Level.SEVERE, e.getLocalizedMessage(), e);
        }
    }

}

From source file:org.awknet.commons.mail.Mail.java

public void send() throws AddressException, MessagingException, FileNotFoundException, IOException {
    int count = recipientsTo.size() + recipientsCc.size() + recipientsBcc.size();
    if (count == 0)
        return;/*from   w ww  .j a v a 2 s . c o m*/

    deleteDuplicates();

    Properties javaMailProperties = new Properties();

    if (fileName.equals("") || fileName == null)
        fileName = DEFAULT_PROPERTIES_FILE;

    javaMailProperties.load(getClass().getResourceAsStream(fileName));

    final String mailUsername = javaMailProperties.getProperty("mail.autentication.username");
    final String mailPassword = javaMailProperties.getProperty("mail.autentication.password");
    final String mailFrom = javaMailProperties.getProperty("mail.autentication.mail_from");

    Session session = Session.getInstance(javaMailProperties, new Authenticator() {
        @Override
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(mailUsername, mailPassword);
        }
    });

    final MimeMessage msg = new MimeMessage(session);

    msg.setFrom(new InternetAddress(mailFrom));
    msg.setRecipients(Message.RecipientType.TO, getToRecipientsArray());
    msg.setRecipients(Message.RecipientType.CC, getCcRecipientsArray());
    msg.setRecipients(Message.RecipientType.BCC, getBccRecipientsArray());
    msg.setSentDate(new Date());
    msg.setSubject(mailSubject);
    msg.setText(mailText, "UTF-8", "html");
    // msg.setText(mailText); //OLD WAY
    new Thread(new Runnable() {
        public void run() {
            try {
                Transport.send(msg);
                Logger.getLogger(Mail.class.getName()).log(Level.INFO, "email was sent successfully!");
            } catch (MessagingException ex) {
                Logger.getLogger(Mail.class.getName()).log(Level.SEVERE, "Cant send email!", ex);
            }
        }
    }).start();
}

From source file:frameworkcontentspeed.Utils.SendEmailAtachament.java

public static void SendEmailSephoraPassed(String adresaSephora, String from, String grupTestContent,
        String grupSephora, String subject, String filename) throws FileNotFoundException, IOException {

    //Get properties object    
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.port", "465");
    //get Session   
    Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, "anda.cristea");
        }/* ww  w .  j av  a2s .  c  om*/
    });
    //compose message    
    try {
        MimeMessage message = new MimeMessage(session);
        message.addRecipient(Message.RecipientType.TO, new InternetAddress(grupTestContent));
        message.addRecipient(Message.RecipientType.BCC, new InternetAddress(grupSephora));

        message.setSubject(subject);
        // message.setText(msg);

        BodyPart messageBodyPart = new MimeBodyPart();

        messageBodyPart.setText("Raport teste automate");

        Multipart multipart = new MimeMultipart();

        multipart.addBodyPart(messageBodyPart);

        messageBodyPart = new MimeBodyPart();

        DataSource source = new FileDataSource(filename);

        messageBodyPart.setDataHandler(new DataHandler(source));

        messageBodyPart.setFileName(filename);

        multipart.addBodyPart(messageBodyPart);

        message.setContent(multipart);

        //send message  
        Transport.send(message);
        //  System.out.println("message sent successfully");
    } catch (Exception ex) {
        System.out.println("eroare trimitere email-uri");
        System.out.println(ex.getMessage());

    }

}

From source file:com.meg7.emailer.EmailerManager.java

@Override
protected void processCycle(int cycle) {
    // Do your thing here.
    final String username = PreferenceUtils.getEmailUsername(mContext);
    final String password = PreferenceUtils.getEmailPassword(mContext);
    if (TextUtils.isEmpty(username) || TextUtils.isEmpty(password)) {
        // TODO :: Populate some sort of error to UI.
        return;/*from w ww  .ja va  2s  . c o  m*/
    }

    Properties properties = new Properties();
    properties.put("mail.smtp.auth", "true");
    properties.put("mail.smtp.starttls.enable", "true");
    properties.put("mail.smtp.host", "smtp.gmail.com");
    properties.put("mail.smtp.port", "587");

    Session session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
    });

    MLog.v(TAG, "Session created for :: " + username);

    final String fromEmail = PreferenceUtils.getFromEmail(mContext);
    final String fromName = PreferenceUtils.getFromName(mContext);
    final String subject = PreferenceUtils.getEmailSubject(mContext);
    final String message = PreferenceUtils.getEmailMessage(mContext);

    List<String> all = readEmailsFromFile(cycle + ".txt");
    List<String> emails;
    List<Message> messages = new ArrayList<Message>();

    int size = all.size();
    int messagesCount = (int) Math.ceil(size / (MAX_RECIPIENTS_PER_EMAIL * 1d));
    MLog.v(TAG, "Emails :: " + size + ", messages count :: " + messagesCount);
    MLog.v(TAG, "Creating messages");
    for (int i = 0; i < messagesCount; i++) {
        try {
            emails = all.subList(i * MAX_RECIPIENTS_PER_EMAIL,
                    Math.min(i * MAX_RECIPIENTS_PER_EMAIL + MAX_RECIPIENTS_PER_EMAIL, size));

            messages.add(createMessage(emails, fromEmail, fromName, subject, message, session));
        } catch (Exception e) {
            MLog.e(TAG, e.toString());
        }

    }

    sendMessages(messages);

    // Reschedule the alarm
    if (getCyclesProcessedTodaySoFar() < EmailerManager.MAX_TASKS_PER_DAY) {
        Scheduler.scheduleNextWake(mContext);
    } else {
        Scheduler.scheduleNextWake(mContext, Constants.THRESHOLD_DAY);

        resetCyclesProcessedTodaySoFar();
    }
}

From source file:org.mobicents.servlet.restcomm.email.EmailService.java

public EmailService(final Configuration config) {
    this.observers = new ArrayList<ActorRef>();
    configuration = config;//from   w w w.  j  a  v a 2 s . c  om
    host = configuration.getString("host");
    port = configuration.getString("port");
    user = configuration.getString("user");
    password = configuration.getString("password");
    final Properties properties = System.getProperties();
    properties.setProperty("mail.smtp.host", host);
    if (user != null && !user.isEmpty()) {
        properties.setProperty("mail.smtp.user", user);
    }
    if (password != null && !password.isEmpty()) {
        properties.setProperty("mail.smtp.password", password);
    }
    if (port != null && !port.isEmpty()) {
        properties.setProperty("mail.smtp.port", port);
    }

    properties.setProperty("mail.smtp.starttls.enable", "true");
    properties.setProperty("mail.transport.protocol", "smtps");
    // properties.setProperty("mail.smtp.ssl.enable", "true");
    properties.setProperty("mail.smtp.auth", "true");

    session = Session.getInstance(properties, new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(user, password);
        }
    });
}

From source file:quickforms.sme.UseFulMethods.java

static public void sendEmail(String d_email, String pwd, String m_to, String m_subject, String message)
        throws Exception {
    final String from = d_email;
    final String password = pwd;
    class SMTPAuthenticator extends Authenticator {

        public PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(from, password);
        }//from  w w w .  j a  v a 2s  . co  m
    }

    //String d_uname = "email";
    //String d_password = "password";
    String d_host = "smtp.gmail.com";
    String d_port = "465"; //465,587

    Properties props = new Properties();
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.host", d_host);
    props.put("mail.smtp.port", d_port);
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.debug", "true");
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.socketFactory.port", d_port);
    props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
    props.put("mail.smtp.socketFactory.fallback", "false");

    SMTPAuthenticator auth = new SMTPAuthenticator();
    Session session = Session.getInstance(props, auth);
    session.setDebug(true);

    MimeMessage msg = new MimeMessage(session);

    //msg.setText(message);
    // Send the actual HTML message, as big as you like

    msg.setContent(message, "text/html");
    msg.setSubject(m_subject);
    msg.setFrom(new InternetAddress(from));
    msg.addRecipient(Message.RecipientType.TO, new InternetAddress(m_to));

    Transport transport = session.getTransport("smtps");
    transport.connect(d_host, 465, from, password);
    transport.sendMessage(msg, msg.getAllRecipients());
    transport.close();

}

From source file:com.enjoyxstudy.selenium.autoexec.mail.MailSender.java

/**
 * @param config/*from   w  ww.  ja va  2s .  c  o  m*/
 * @throws Exception
 */
public MailSender(final MailConfiguration config) throws Exception {

    this.config = config;

    Properties prop = new Properties();

    if (config.isSsl()) {
        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.socketFactory.port", String.valueOf(config.getPort()));
    }

    prop.setProperty("mail.transport.protocol", config.getProtocol());
    prop.setProperty("mail.smtp.host", config.getHost());
    prop.setProperty("mail.smtp.port", String.valueOf(config.getPort()));
    log.info("smtp host:" + config.getHost() + " port:" + config.getPort());
    prop.setProperty("mail.smtp.connectiontimeout", String.valueOf(config.getConnectionTimeout()));
    prop.setProperty("mail.smtp.timeout", String.valueOf(config.getReadTimeout()));

    prop.setProperty("mail.debug", String.valueOf(config.isDebug()));

    if (config.getUsername() != null && config.getUsername().length() != 0) {
        prop.setProperty("mail.smtp.auth", "true");

        session = Session.getInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(config.getUsername(), config.getPassword());
            }
        });
    } else {
        session = Session.getInstance(prop);
    }
    log.info("smtp host:" + config.getHost());
}

From source file:ru.codemine.ccms.mail.EmailService.java

@Override
public PasswordAuthentication getPasswordAuthentication() {
    return new PasswordAuthentication(user, password);
}