Advertisement
Guest User

Untitled

a guest
Dec 4th, 2018
613
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.52 KB | None | 0 0
  1. import java.io.IOException;
  2. import java.util.ArrayList;
  3. import java.util.Date;
  4. import java.util.List;
  5. import java.util.Properties;
  6. import javax.mail.Session;
  7. import javax.mail.Store;
  8. import javax.mail.Address;
  9. import javax.mail.BodyPart;
  10. import javax.mail.Folder;
  11. import javax.mail.Message;
  12. import javax.mail.MessagingException;
  13. import javax.mail.NoSuchProviderException;
  14. import javax.mail.Transport;
  15. import javax.mail.internet.InternetAddress;
  16. import javax.mail.internet.MimeMessage;
  17. import javax.mail.internet.MimeMultipart;
  18. import javax.swing.DefaultListModel;
  19.  
  20. import org.springframework.util.concurrent.ListenableFutureAdapter;
  21.  
  22. import twitter4j.Status;
  23.  
  24. public class MailAPI {
  25. private static String username;
  26. private static String password;
  27. private static String from;
  28. private static String pass;
  29. MailAPI mail;
  30. private Address from1;
  31. private String subj;
  32. public DefaultListModel<String> listaDeEmails = new DefaultListModel<String>();
  33. public DefaultListModel<String> listaDeProcuraDeEmails = new DefaultListModel<String>();
  34. public DefaultListModel<String> post_24h = new DefaultListModel<String>();
  35.  
  36. private Message[] messages;
  37.  
  38. public static void main(String[] args) {
  39. }
  40.  
  41. public Message[] getMessages() {
  42. return messages;
  43. }
  44.  
  45. public void setMessages(Message[] messages) {
  46. this.messages = messages;
  47. }
  48.  
  49. /**
  50. * @return the listaDeEmails
  51. */
  52. public DefaultListModel<String> getlistaDeEmails() {
  53. return listaDeEmails;
  54. }
  55.  
  56. /**
  57. * @param listaDeEmails the listaDeEmails to set
  58. */
  59. public void setlistaDeEmails(DefaultListModel<String> listaDeEmails) {
  60. this.listaDeEmails = listaDeEmails;
  61. }
  62.  
  63. public void getMailCredentials(String user, String pass) {
  64. MailAPI.username = user;
  65. MailAPI.password = pass;
  66. }
  67.  
  68. public void getEmail() throws Exception {
  69. mail = new MailAPI();
  70.  
  71. from = mail.getUsername();
  72. pass = mail.getPass();
  73.  
  74. try {
  75. Properties properties = new Properties();
  76.  
  77. properties.put("mail.pop3.host", "outlook.office365.com");
  78. properties.put("mail.pop3.port", "995");
  79. properties.put("mail.pop3s.ssl.trust", "*"); // This is the most IMP property
  80.  
  81. Session emailSession = Session.getDefaultInstance(properties);
  82.  
  83. // create the POP3 store object and connect with the pop server
  84.  
  85. Store store = emailSession.getStore("pop3s"); // try imap or impas
  86. store.connect("outlook.office365.com", from, password);
  87.  
  88. // create the folder object and open it
  89. Folder emailFolder = store.getFolder("INBOX");
  90. emailFolder.open(Folder.READ_ONLY);
  91.  
  92. // retrieve the messages from the folder in an array and print it
  93. messages = emailFolder.getMessages();
  94.  
  95. for (int i = 0; i < (messages.length + 60) - messages.length; i++) {
  96.  
  97. Message message = messages[i];
  98. if (message.getFrom()[0].toString().contains("iscte-iul.pt")) {
  99. from1 = message.getFrom()[0];
  100. subj = message.getSubject();
  101. System.out.println(from1);
  102. System.out.println(subj);
  103. if (message != null) {
  104.  
  105. listaDeEmails.addElement("FROM: " + from1 + " " + "SUBJECT: " + subj );
  106.  
  107. }
  108. }
  109. }
  110. emailFolder.close(false);
  111. store.close();
  112. } catch (NoSuchProviderException nspe) {
  113. nspe.printStackTrace();
  114. } catch (MessagingException me) {
  115. me.printStackTrace();
  116. }
  117. }
  118.  
  119. /**
  120. * @return the from1
  121. */
  122. public Address getFrom1() {
  123. return from1;
  124. }
  125.  
  126. /**
  127. * @param from1 the from1 to set
  128. */
  129. public void setFrom1(Address from1) {
  130. this.from1 = from1;
  131. }
  132.  
  133. /**
  134. * @return the subj
  135. */
  136. public String getSubj() {
  137. return subj;
  138. }
  139.  
  140. /**
  141. * @param subj the subj to set
  142. */
  143. public void setSubj(String subj) {
  144. this.subj = subj;
  145. }
  146.  
  147. public void sendEmail(String address, String subject, String message) throws Exception {
  148. mail = new MailAPI();
  149. from = mail.getUsername();
  150. pass = mail.getPass();
  151. System.out.println();
  152. String[] to = { address };
  153. String host = "smtp-mail.outlook.com";
  154.  
  155. Properties prop = System.getProperties();
  156. prop.put("mail.smtp.starttls.enable", "true");
  157. prop.put("mail.smtp.ssl.trust", host);
  158. prop.put("mail.smtp.host", host);
  159. prop.put("mail.smtp.user", from);
  160. prop.put("mail.smtp.password", pass);
  161. prop.put("mail.smtp.port", "587");
  162. prop.put("mail.smtp.auth", "true");
  163. prop.put("mail.debug", "true");
  164.  
  165. Session session = Session.getDefaultInstance(prop);
  166. MimeMessage msg = new MimeMessage(session);
  167. msg.setFrom(new InternetAddress(from));
  168. InternetAddress[] toaddress = new InternetAddress[to.length];
  169. for (int i = 0; i < to.length; i++) {
  170. toaddress[i] = new InternetAddress(to[i]);
  171.  
  172. }
  173. for (int i = 0; i < toaddress.length; i++) {
  174. msg.setRecipient(Message.RecipientType.TO, toaddress[i]);
  175. }
  176.  
  177. msg.setSubject(subject);
  178. msg.setContent(message, "text/html; charset=utf-8");
  179. Transport transport = session.getTransport("smtp");
  180. transport.connect(host, from, pass);
  181. transport.sendMessage(msg, msg.getAllRecipients());
  182. transport.close();
  183. }
  184.  
  185. public String getTextFromMessage(Message message) throws MessagingException, IOException {
  186. String result = "";
  187. if (message.isMimeType("text/plain")) {
  188. result = message.getContent().toString();
  189. } else if (message.isMimeType("multipart/*")) {
  190. MimeMultipart mimeMultipart = (MimeMultipart) message.getContent();
  191. result = getTextFromMimeMultipart(mimeMultipart);
  192. }
  193. return result;
  194. }
  195.  
  196. private String getTextFromMimeMultipart(MimeMultipart mimeMultipart) throws MessagingException, IOException {
  197. String result = "";
  198. int count = mimeMultipart.getCount();
  199. for (int i = 0; i < count; i++) {
  200. BodyPart bodyPart = mimeMultipart.getBodyPart(i);
  201. if (bodyPart.isMimeType("text/plain")) {
  202. result = result + "\n" + bodyPart.getContent();
  203. break; // without break same text appears twice in my tests
  204. } else if (bodyPart.isMimeType("text/html")) {
  205. String html = (String) bodyPart.getContent();
  206. result = result + "\n" + org.jsoup.Jsoup.parse(html).text();
  207. } else if (bodyPart.getContent() instanceof MimeMultipart) {
  208. result = result + getTextFromMimeMultipart((MimeMultipart) bodyPart.getContent());
  209. }
  210. }
  211. return result;
  212. }
  213.  
  214. public String getPass() {
  215. return password;
  216. }
  217.  
  218. public void setPass(String pass) {
  219. this.password = pass;
  220. }
  221.  
  222. public String getUsername() {
  223. return username;
  224. }
  225.  
  226. public static void setUsername(String username) {
  227. MailAPI.username = username;
  228. }
  229.  
  230. public void searchForTagInMailBox(String tag) {
  231. listaDeProcuraDeEmails.clear();
  232. for (int tweet = 0; tweet < listaDeEmails.size(); tweet++) {
  233. String element = listaDeEmails.getElementAt(tweet);
  234. String[] partes = element.split(" ");
  235. for (int palavras_do_tweet = 0; palavras_do_tweet < partes.length; palavras_do_tweet++) {
  236. if (partes[palavras_do_tweet].equals(tag)) {
  237. listaDeProcuraDeEmails.addElement(element);
  238. }
  239. }
  240. }
  241. }
  242.  
  243. public void showListMailsDirector() throws Exception {
  244. listaDeProcuraDeEmails.clear();
  245. String mailDaReitora = "<reitora@iscte-iul.pt>";
  246. for (int tweet = 0; tweet < listaDeEmails.size(); tweet++) {
  247. String element = listaDeEmails.getElementAt(tweet);
  248. String[] partes = element.split(" ");
  249. for (int palavras_do_tweet = 0; palavras_do_tweet < partes.length; palavras_do_tweet++) {
  250. if (partes[palavras_do_tweet].equals(mailDaReitora)) {
  251. listaDeProcuraDeEmails.addElement(element);
  252. }
  253. }
  254. }
  255. }
  256.  
  257. public void showListMailsISCTE() throws IOException {
  258. from = mail.getUsername();
  259. pass = mail.getPass();
  260.  
  261. try {
  262. Properties properties = new Properties();
  263.  
  264. properties.put("mail.pop3.host", "outlook.office365.com");
  265. properties.put("mail.pop3.port", "995");
  266. properties.put("mail.pop3s.ssl.trust", "*"); // This is the most IMP property
  267.  
  268. Session emailSession = Session.getDefaultInstance(properties);
  269.  
  270. // create the POP3 store object and connect with the pop server
  271.  
  272. Store store = emailSession.getStore("pop3s"); // try imap or impas
  273. store.connect("outlook.office365.com", from, password);
  274.  
  275. // create the folder object and open it
  276. Folder emailFolder = store.getFolder("INBOX");
  277. emailFolder.open(Folder.READ_ONLY);
  278.  
  279. // retrieve the messages from the folder in an array and print it
  280. Message[] messages = emailFolder.getMessages();
  281.  
  282. for (int i = 0; i < (messages.length + 60) - messages.length; i++) {
  283. Message message = messages[i];
  284. if (message.getSubject().toString().contains("ISCTE-IUL")) {
  285. String result;
  286. result = getTextFromMessage(message);
  287. System.out.println("From: " + message.getFrom()[0]);
  288. from1 = message.getFrom()[0];
  289. subj = message.getSubject();
  290. if (message != null) {
  291. listaDeEmails.addElement("FROM: " + from1 + " " + "SUBJECT: " + subj);
  292. }
  293. }
  294. }
  295. emailFolder.close(false);
  296. store.close();
  297. } catch (NoSuchProviderException nspe) {
  298. nspe.printStackTrace();
  299. } catch (MessagingException me) {
  300. me.printStackTrace();
  301. }
  302. }
  303.  
  304. public void filtrarUltimas24horas() {
  305. Date today = new Date();
  306. Long dateInLong = today.getTime();
  307. for (int mail = 0; mail < listaDeEmails.size(); mail++) {
  308. String element = listaDeEmails.getElementAt(mail);
  309. String[] partes = element.split(" ");
  310. int last_index = partes.length - 1;
  311. Long millie = Long.parseLong(partes[last_index]);
  312. Long periodo_24 = dateInLong - 86400000;
  313. // ultimas 24horas
  314. if (millie >= periodo_24)
  315. post_24h.addElement(element);
  316. }
  317. if (post_24h.isEmpty())
  318. post_24h.addElement("::Não existe nenhum Tweet nas últimas 24h!::");
  319. }
  320. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement