Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
- public static boolean SendEmail(String to, String subject, String msg)
- {
- final String username = "haythem.khlifi@gmail.tn";
- //"haythem.khlifi@esprit.tn";
- //String oauth2_access_token = null;
- final String password = "4/UwTcfs3QE2ua_t1nBln1HyVAIS9udqtWX9eRObo3Q2Q";
- boolean flag = false;
- Properties props = new Properties();
- //gamil OAuth2
- props.put("mail.imap.ssl.enable", "true"); // required for Gmail
- props.put("mail.imap.auth.mechanisms", "XOAUTH2");
- //************************************************
- props.put("mail.smtp.host", "smtp.gmail.com");
- props.put("mail.smtp.auth", "true");
- props.put("mail.debug", "true");
- props.put("mail.smtp.port", "587");
- props.put("mail.smtp.socketFactory.port", "587");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
- props.put("mail.smtp.socketFactory.fallback", "false");
- Session session = Session.getInstance(props, new javax.mail.Authenticator()
- {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(username, password);
- }
- });
- try {
- Message message = new MimeMessage(session);
- message.setFrom(new InternetAddress("haythem.khlifi@gmail.tn"));
- message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
- message.setSubject(subject);
- message.setContent(msg, "text/html; charset=utf-8");
- Transport.send(message);
- flag = true;
- System.out.println("Done");
- } catch (MessagingException e) {
- throw new RuntimeException(e);
- }
- return flag;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement