Advertisement
Guest User

Untitled

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