Wednesday 24 June 2015

Sending E-Mail

public void mailSending(int i){
        final String username = "User name";
        final String password = "Password";
       
        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");

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

        try {
             Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("from mail@gmail.com"));//From mail-id
            message.setRecipients(Message.RecipientType.TO,
                    InternetAddress.parse(excelrwObj.fis_getCellValueByColumnName(i, "mailTo")));//To mail-id
            message.setSubject("Re: Apllication sync status");
            message.setText("Hi, \n\n"+
                    "Application Name : "+excelrwObj.fis_getCellValueByColumnName(i, "ApkName")+
                    ",\nVersion info : "+excelrwObj.fis_getCellValueByColumnName(i, "Version")+
                    ",\nUser Name : "+excelrwObj.fis_getCellValueByColumnName(i, "userid")+
                    ",\nPassword : "+excelrwObj.fis_getCellValueByColumnName(i, "password")+
                    ",\nSync status : "+excelrwObj.fis_getCellValueByColumnName(i, "LastExeStatus")+
                    ",\nRun At : "+excelrwObj.fis_getCellValueByColumnName(i, "LastRunAt")+
                    "\n\nRegards, \nYotility Team.");

            Transport.send(message);
             System.out.println("Done");
         } catch (Exception e) {
            e.printStackTrace();
        }
    }

No comments:

Post a Comment