Advertisement
Guest User

Untitled

a guest
Jun 28th, 2017
1,500
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.92 KB | None | 0 0
  1. package outlook;
  2.  
  3. import bl.model.Feedback;
  4.  
  5. import java.security.GeneralSecurityException;
  6. import java.util.*;
  7. import javax.mail.*;
  8. import javax.mail.internet.*;
  9.  
  10. public class Sender {
  11.  
  12. public static Sender instance;
  13. final String host = "smtp-mail.outlook.com";// host address and protocol : here smtp
  14. final String user = "polytechpi@outlook.fr";// mail address
  15. final String password = "Polytech2017";// Password
  16.  
  17. final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
  18.  
  19. private Sender(){
  20. }
  21.  
  22. public static Sender getInstance(){
  23. if (instance == null){
  24. instance = new Sender();
  25. }
  26. return instance;
  27. }
  28.  
  29. public void send(String json, String type) throws GeneralSecurityException {
  30. // Get system properties
  31. /*
  32. Properties properties = new Properties();
  33. properties.setProperty("mail.store.protocol", "imap");
  34. properties.setProperty("mail.imap.user", user);
  35. properties.setProperty("mail.imap.host", host);
  36. properties.setProperty("mail.imap.port", "993");
  37.  
  38. properties.setProperty("mail.imaps.socketFactory.class", SSL_FACTORY);
  39. properties.setProperty("mail.imaps.socketFactory.fallback", "false");
  40. properties.setProperty("mail.imaps.socketFactory.port", "993");
  41. properties.put("mail.imaps.host", "imap-mail.outlook.com");
  42. MailSSLSocketFactory sf = new MailSSLSocketFactory();
  43. sf.setTrustAllHosts(true);
  44. properties.put("mail.imaps.ssl.trust", "*");
  45. properties.put("mail.imaps.ssl.socketFactory", sf);
  46. System.out.println("Retrieving emails from " + user +" ...");
  47. Session session = Session.getDefaultInstance(properties);
  48.  
  49. Properties props = new Properties();
  50. props.put("mail.smtp.auth", "true");
  51. props.put("mail.smtp.starttls.enable", "true");
  52. props.put("mail.smtp.host", "smtp-mail.outlook.com");
  53. props.put("mail.smtp.port", "587");
  54. props.put("mail.transport.protocol", "smtp");
  55. props.put("mail.smtp.ssl.trust", "smtp.outlook.com");
  56. props.put("mail.smtp.socketFactory.class",
  57. "javax.net.ssl.SSLSocketFactory");
  58. Session session = Session.getInstance(props,
  59. new javax.mail.Authenticator() {
  60. protected PasswordAuthentication getPasswordAuthentication() {
  61. return new PasswordAuthentication(user, password);
  62. }
  63. });*/
  64.  
  65. //Set the host smtp address
  66. Properties props = new Properties();
  67. props.put("mail.smtp.host", host);
  68. props.put("mail.smtp.port", "587");
  69. props.put("mail.smtp.starttls.enable","true");
  70. props.put("mail.smtp.auth", "true");
  71. Authenticator auth = new SMTPAuthenticator();
  72. Session session = Session.getDefaultInstance(props, auth);
  73.  
  74. try {
  75. // Create a default MimeMessage object.
  76. MimeMessage message = new MimeMessage(session);
  77.  
  78. // Set From: header field of the header.
  79. message.setFrom(new InternetAddress(user));
  80.  
  81. // Set To: header field of the header.
  82. message.addRecipient(Message.RecipientType.TO, new InternetAddress("polytechpi@outlook.fr"));
  83.  
  84. // Set Subject: header field
  85. message.setSubject(type);
  86.  
  87. // Now set the actual message
  88. message.setText(json);
  89.  
  90. // Send message
  91. Transport.send(message);
  92. System.out.println("Sent message successfully....");
  93. }catch (MessagingException mex) {
  94. mex.printStackTrace();
  95. }
  96. }
  97.  
  98.  
  99. private class SMTPAuthenticator extends javax.mail.Authenticator
  100. {
  101. public PasswordAuthentication getPasswordAuthentication()
  102. {
  103. return new PasswordAuthentication(user, password);
  104. }
  105. }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement