Advertisement
Guest User

Untitled

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