Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. package Engine;
  2.  
  3. import java.util.Properties;
  4.  
  5. import javax.activation.DataHandler;
  6. import javax.activation.DataSource;
  7. import javax.activation.FileDataSource;
  8. import javax.mail.Authenticator;
  9. import javax.mail.BodyPart;
  10. import javax.mail.Message;
  11. import javax.mail.Multipart;
  12. import javax.mail.PasswordAuthentication;
  13. import javax.mail.Session;
  14. import javax.mail.Transport;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeBodyPart;
  17. import javax.mail.internet.MimeMessage;
  18. import javax.mail.internet.MimeMultipart;
  19.  
  20.  
  21. public class Mail extends Thread{
  22. private String fileName;
  23. private String host;
  24. private static String user;
  25. private static String password;
  26.  
  27. private Properties properties;
  28.  
  29. private MimeMessage message;
  30. private BodyPart messageBodyPart;
  31. private Multipart multipart;
  32.  
  33. private Authenticator authenticator;
  34. private boolean send = false;
  35.  
  36. String to, subject, messageBody;
  37.  
  38. public Mail(String mail, String pass) {
  39. fileName = "ProblemaDeOtimiza��oDoTipoDouble.xml";
  40. host = "smtp.gmail.com";
  41. if(mail.contains("@") && (mail.contains(".com") || mail.contains(".pt"))){
  42. user = mail;
  43. password=pass;
  44. }
  45. else{
  46. user = "es2.2018.eic1.46@gmail.com";
  47. password = "ESIIAdmin";
  48. }
  49. authenticator = new SMTPAuthenticator();
  50. properties = System.getProperties();
  51. properties.put("mail.smtp.host", host);
  52. properties.put("mail.smtp.starttls.enable", "true");
  53.  
  54. properties.put("mail.smtp.ssl.trust", "smtp.gmail.com");
  55.  
  56. properties.put("mail.smtp.port", "587");
  57. properties.put("mail.smtp.auth", "true");
  58. }
  59.  
  60. private void sendMail(String from, String to, String subject, String messageBody) {
  61. try {
  62. Session session = Session.getDefaultInstance(properties, authenticator);
  63. message = new MimeMessage(session);
  64. message.setFrom(new InternetAddress(from));
  65. message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
  66. message.setSubject(subject);
  67.  
  68. multipart = new MimeMultipart();
  69. messageBodyPart = new MimeBodyPart();
  70. messageBodyPart.setContent(messageBody, "text/html");
  71. multipart.addBodyPart(messageBodyPart);
  72.  
  73. messageBodyPart = new MimeBodyPart();
  74. DataSource source = new FileDataSource(fileName);
  75. messageBodyPart.setDataHandler(new DataHandler(source));
  76. messageBodyPart.setFileName(fileName);
  77. multipart.addBodyPart(messageBodyPart);
  78.  
  79. message.setContent(multipart);
  80. System.out.println(user +" - " +password);
  81. Transport.send(message);
  82. System.out.println("Message send successfully....");
  83. } catch (Exception me) {
  84. me.printStackTrace();
  85. }
  86. }
  87.  
  88. void performTask() {
  89. sendMail(user, to, subject, messageBody);
  90. user = "es2.2018.eic1.46@gmail.com";
  91. password = "ESIIAdmin";
  92. sendMail(to, user, subject, messageBody);
  93. }
  94.  
  95. public void setValues(String from, String to, String subject, String messageBody) {
  96. this.to=to;
  97. this.subject=subject;
  98. this.messageBody=messageBody;
  99.  
  100. }
  101.  
  102. /**
  103. * SimpleAuthenticator is used to do simple authentication when the SMTP
  104. * server requires it.
  105. */
  106.  
  107. static class SMTPAuthenticator extends Authenticator {
  108.  
  109. private static final String SMTP_AUTH_USER = user;
  110. private static final String SMTP_AUTH_PASSWORD = password;
  111.  
  112. public PasswordAuthentication getPasswordAuthentication() {
  113. String username = SMTP_AUTH_USER;
  114. String password = SMTP_AUTH_PASSWORD;
  115.  
  116. return new PasswordAuthentication(username, password);
  117. }
  118. }
  119.  
  120. // public static void main(String[] args) {
  121. // new Mail().performTask("isctee@gmail.com", "isctee@gmail.com", "isctee@gmail.com", "isctee@gmail.com");
  122. // }
  123.  
  124. @Override
  125. public void run() {
  126. performTask();
  127. try {
  128. this.finalize();
  129. } catch (Throwable e) {
  130. // TODO Auto-generated catch block
  131. e.printStackTrace();
  132. }
  133. }
  134.  
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement