Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
515
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.77 KB | None | 0 0
  1. private static final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  2.  
  3. public static boolean SendEmail(String to, String subject, String msg)
  4.  
  5. {
  6.  
  7. final String username = "haythem.khlifi@gmail.tn";
  8. //"haythem.khlifi@esprit.tn";
  9. //String oauth2_access_token = null;
  10. final String password = "4/UwTcfs3QE2ua_t1nBln1HyVAIS9udqtWX9eRObo3Q2Q";
  11.  
  12. boolean flag = false;
  13.  
  14. Properties props = new Properties();
  15. //gamil OAuth2
  16. props.put("mail.imap.ssl.enable", "true"); // required for Gmail
  17. props.put("mail.imap.auth.mechanisms", "XOAUTH2");
  18.  
  19.  
  20. //************************************************
  21. props.put("mail.smtp.host", "smtp.gmail.com");
  22.  
  23. props.put("mail.smtp.auth", "true");
  24.  
  25. props.put("mail.debug", "true");
  26.  
  27. props.put("mail.smtp.port", "587");
  28.  
  29. props.put("mail.smtp.socketFactory.port", "587");
  30.  
  31. props.put("mail.smtp.starttls.enable", "true");
  32.  
  33. props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
  34.  
  35. props.put("mail.smtp.socketFactory.fallback", "false");
  36.  
  37. Session session = Session.getInstance(props, new javax.mail.Authenticator()
  38.  
  39. {
  40. @Override
  41. protected PasswordAuthentication getPasswordAuthentication() {
  42. return new PasswordAuthentication(username, password);
  43. }
  44. });
  45.  
  46. try {
  47.  
  48. Message message = new MimeMessage(session);
  49. message.setFrom(new InternetAddress("haythem.khlifi@gmail.tn"));
  50. message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
  51. message.setSubject(subject);
  52. message.setContent(msg, "text/html; charset=utf-8");
  53.  
  54. Transport.send(message);
  55. flag = true;
  56. System.out.println("Done");
  57.  
  58. } catch (MessagingException e) {
  59. throw new RuntimeException(e);
  60. }
  61. return flag;
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement