Advertisement
Guest User

Untitled

a guest
Dec 6th, 2019
332
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 16.82 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3. import javax.mail.*;
  4. import javax.mail.internet.*;
  5. import javax.mail.search.*;
  6. public class Client {
  7. static class SMTPAuthenticator extends javax.mail.Authenticator {
  8. String _user, _password;
  9. public SMTPAuthenticator(String user, String pw){
  10. _user = user;
  11. _password = pw;
  12. }
  13. public PasswordAuthentication getPasswordAuthentication() {
  14. return new PasswordAuthentication(_user, _password);
  15. }
  16. }
  17. // coffeeK102@yahoo.com PW: anfxgtqdobprtzrv
  18. // sakurino84@yahoo.com PW: pgywouyccoerecdv
  19. static Scanner scan=new Scanner(System.in);
  20. static Properties prop=new Properties();
  21. static String imapHost = "imap.mail.yahoo.com";
  22. static String smtpHost = "smtp.mail.yahoo.com";
  23. static String popHost = "pop.mail.yahoo.com";
  24. static String nutzer = "sakurino84@yahoo.com ";
  25. static String passwort = "pgywouyccoerecdv";
  26. static String smtpPort = "587";
  27. static String popPort = "995";
  28. static String imapProtokoll = "imaps";
  29. static String smtpProtokoll = "smtp";
  30. static String popProtokoll = "pop3s";
  31. static boolean moreInfo = true;
  32. static String absender,empfänger,datum,betreff,inhalt,volltext,ordnerName;
  33. static boolean oder;
  34. static int size=-1; // für die Suche nach der Zeilengröße
  35.  
  36. public static void main(String arg[]) throws Exception {
  37. System.out.println("MailClient");
  38. Session sitzung = createSitzung();
  39. while(true){
  40. System.out.print("1) Nachricht versenden 2) Ordner ansehen 3) Mails ansehen 4) Mail(s) suchen q) quit : ");
  41. String s=scan.nextLine();
  42. if(s.charAt(0)=='q')
  43. return;
  44. while(!isInteger(s) || s.length()>1){
  45. System.out.println("Bitte nur eine natürliche Zahl eingeben :");
  46. s = scan.nextLine();
  47. }
  48. switch(s.charAt(0)){
  49. case '1': sendeMail(sitzung ); break;
  50. case '2': leseOrdner(sitzung); break;
  51. case '3': leseMails(sitzung,"INBOX"); break;
  52. case '4': sucheMails(sitzung); break;
  53. case 'q': return;
  54. default: break;
  55.  
  56. }
  57. }
  58. }
  59.  
  60. static Session createSitzung(){
  61. //SMTP
  62. prop.put("mail.smtp.host",smtpHost);
  63. prop.put("mail.transport.protocol",smtpProtokoll);
  64. prop.put("mail.smtp.port",smtpPort);
  65. prop.put("mail.smtp.starttls.enable", "true");
  66. prop.put("mail.smtp.auth","true");
  67. //POP
  68. prop.put("pop.mail.yahoo.com", popHost);
  69. prop.put("pop.mail.port", popPort);
  70. Authenticator auth = new SMTPAuthenticator(nutzer,passwort);
  71. Session sitzung = Session.getInstance(prop,auth);
  72. // session.setDebug(true);
  73. return sitzung;
  74. }
  75. static void sendeMail(Session sitzung) throws IOException {
  76. setzeSendeDetails();
  77. Message msg = new MimeMessage(sitzung);
  78. try{
  79. msg.setFrom(new InternetAddress(nutzer));
  80. msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(empfänger));
  81. msg.setSubject(betreff);
  82. msg.setText(inhalt);
  83. msg.setSentDate(new Date());
  84. System.out.println("\nSenden...");
  85. Transport.send(msg);
  86. }
  87. catch (MessagingException e) {
  88. throw new RuntimeException(e);
  89. }
  90. System.out.println("\nMail wurde versendet.\n");
  91. }
  92. static void setzeSendeDetails() throws IOException{
  93. /*absender = nutzer;
  94. empfänger = "coffeek102@yahoo.com";
  95. betreff = "Eismann";
  96. System.out.printf("Von: %s\n",absender);
  97. System.out.printf("nach: %s\n",empfänger);
  98. System.out.printf("Betreff: %s\n",betreff);*/
  99. System.out.print("Absender: ");
  100. System.out.flush();
  101. absender = nutzer = scan.nextLine();
  102. while(!isMail(absender,'@')){
  103. System.out.println("Bitte das @ - Zeichen nicht vergessen.\nVon: ");
  104. absender = scan.nextLine();
  105. }
  106.  
  107. System.out.printf("Passwort für %s eingeben: ",nutzer);
  108. passwort = scan.nextLine();
  109. System.out.println("PW = "+passwort);
  110. System.out.print("Empfänger: ");
  111. System.out.flush();
  112. empfänger = scan.nextLine();
  113. while(!isMail(empfänger,'@')){
  114. System.out.println("Bitte das @ - Zeichen nicht vergessen.\nNach: ");
  115. empfänger = scan.nextLine();
  116. }
  117. System.out.print("Betreff: ");
  118. System.out.flush();
  119. betreff = scan.nextLine();
  120.  
  121. System.out.print("Inhalt:\n\n");
  122. System.out.flush();
  123. inhalt = setzeInhalt();
  124. }
  125. static void leseOrdner(Session sitzung) throws NoSuchProviderException, MessagingException, Exception{
  126. System.out.println("\nLaden...\n");
  127. try (Store store = sitzung.getStore(imapProtokoll)) {
  128. store.connect(imapHost, nutzer, passwort);
  129. System.out.println("STORE: "+store);
  130. //Um jeden Ordner anzusehen
  131. Folder []n = store.getDefaultFolder().list();
  132. String names[]=new String[n.length+1];
  133. int i=1;
  134. for(Folder ordner : n){
  135. System.out.println("* "+ordner.getName());
  136. names[i++]=ordner.getName();
  137. if(moreInfo){
  138. if ((ordner.getType() & Folder.HOLDS_MESSAGES) != 0) {
  139. System.out.println("Nachrichten Gesamt : " + ordner.getMessageCount());
  140. System.out.println("Neue Nachrichten : " + ordner.getNewMessageCount());
  141. System.out.println("Ungelesene Nachrichten : " + ordner.getUnreadMessageCount());
  142. System.out.println("");
  143. }
  144. }
  145. }
  146. System.out.print("Ordner Auswählen? y | n : ");
  147. String auswahl=scan.nextLine();
  148. while(!isBuchstabe(auswahl) || auswahl.length() > 1){
  149. System.out.print("Ordner Auswählen? y | n : ");
  150. auswahl=scan.nextLine();
  151. }
  152. if(auswahl.charAt(0)=='n')
  153. return;
  154. else{
  155. for( i=1;i<names.length;i++){
  156. System.out.printf("%d) %s ",i,names[i]);
  157. }
  158. System.out.print("\nOrdnerwahl: ");
  159. auswahl=scan.nextLine();
  160. while(!isInteger(auswahl) || auswahl.length()>1){
  161. System.out.print("Ordnerwahl: ");
  162. auswahl=scan.nextLine();
  163. }
  164. }
  165. ordnerName=names[Integer.parseInt(auswahl)];
  166. leseMails(sitzung,ordnerName);
  167. }
  168. }
  169. //diese Methode liest die mails mit POP3
  170. static void leseMails(Session sitzung,String ordnerName) throws NoSuchProviderException, MessagingException, Exception{
  171. System.out.println("\nLaden...\n");
  172.  
  173. try (Store korb = sitzung.getStore(imapProtokoll)) {
  174. korb.connect(imapHost,nutzer,passwort);
  175. Folder ordner = korb.getFolder(ordnerName);
  176. ordner.open(Folder.READ_ONLY);
  177.  
  178. Message[] nachrichten = ordner.getMessages();
  179. System.out.printf("\n\n%s\n",ordnerName+" von\n"+nutzer+"\n"+nachrichten.length+" Nachrichten:\n");
  180. int i,n;
  181. for (i = 0, n = nachrichten.length; i < n; i++) {
  182. Message nachricht = nachrichten[i];
  183. System.out.println("---------------------------------");
  184. System.out.printf("#%d. Nachricht\n", (i + 1));
  185. System.out.println("Von: " + nachricht.getFrom()[0]);
  186. System.out.println("Am: " + nachricht.getSentDate());
  187. System.out.println("Betreff: " + nachricht.getSubject());
  188. if(moreInfo)
  189. System.out.println("Inhalt: \n" + nachricht.getContent().toString());
  190. }
  191. ordner.close(false);
  192. }
  193. }
  194. static void sucheMails (Session sitzung)throws NoSuchProviderException, MessagingException {
  195. while(true){
  196. System.out.println("\n - - - Email(s) suchen - - - Suchkriteriums-Auswahl");
  197. if(!sucheFragen())
  198. return;
  199. System.out.println("\nSuchen...");
  200. Store korb = sitzung.getStore(imapProtokoll);
  201. korb.connect(imapHost,nutzer,passwort);
  202.  
  203. Folder ordner = korb.getDefaultFolder();
  204. ordner = ordner.getFolder("INBOX");
  205. ordner.open(Folder.READ_ONLY);
  206.  
  207. SearchTerm schlüssel = null;
  208. //Volltextsuche
  209. if(volltext != null){
  210. schlüssel =new BodyTerm(volltext);
  211. }
  212. //Suche nach Betreff
  213. if(betreff != null)
  214. schlüssel = new SubjectTerm(betreff);
  215. //Suche nach Absender
  216. if(absender != null) {
  217. FromStringTerm sucheAbs = new FromStringTerm(absender);
  218. if(schlüssel != null) {
  219. if( oder )
  220. schlüssel = new OrTerm(schlüssel,sucheAbs);
  221. else
  222. schlüssel = new AndTerm(schlüssel, sucheAbs);
  223. }
  224. else
  225. schlüssel = sucheAbs;
  226. }// Suche nach Datum
  227. if( datum != null){
  228. //Extrahiere das Datum aus String
  229. String [] tmp = datum.split("\\.");
  230. int tt = Integer.parseInt(tmp[0]);
  231. int mm = Integer.parseInt(tmp[1]);
  232. int jj = Integer.parseInt(tmp[2]);
  233. //Setze das Datum (Januar ist Monat 0)
  234. Calendar c = Calendar.getInstance();
  235. c.set(jj, mm-1, tt);
  236. //Setze das Suchintervall auf +- 1 Tag des angegebenen Datums
  237. ReceivedDateTerm morgen = new ReceivedDateTerm(ComparisonTerm.GE, c.getTime());
  238. c.add(Calendar.DATE, 1); // Tag +1
  239. ReceivedDateTerm abend = new ReceivedDateTerm(ComparisonTerm.LT, c.getTime());
  240. //Setze Schlüssel auf
  241. SearchTerm tag = new AndTerm(morgen, abend);
  242. schlüssel = tag;
  243.  
  244. } // Suche nach Zeilengröße
  245. if (size >= 0) {
  246. SizeTerm sizeTerm = new SizeTerm(ComparisonTerm.GT, size);
  247. schlüssel = sizeTerm;
  248. }
  249. //suche
  250. Message [] msgs = ordner.search(schlüssel);
  251. if(msgs.length < 1)
  252. System.out.println("\nLeider Keine Übereinstimmung gefunden\n");
  253. else{
  254. System.out.printf("\n%d Übereinstimmungen gefunden\n",msgs.length);
  255.  
  256. FetchProfile fp = new FetchProfile();
  257. fp.add(FetchProfile.Item.ENVELOPE);
  258. ordner.fetch(msgs, fp);
  259. try{
  260. int i;
  261. for (i = 0; i < msgs.length; i++) {
  262. System.out.println("--------------------------");
  263. System.out.println("Nachricht #" + (i + 1) + ":");
  264. gesuchteMails(msgs[i]);
  265. }
  266. }catch(Exception ex){
  267. System.out.println("Error " + ex.getMessage());
  268. }
  269. ordner.close(false);
  270. korb.close();
  271. }
  272. }
  273. }
  274. static boolean sucheFragen(){
  275. absender = empfänger = datum = betreff = null;
  276. System.out.print("1) Volltextsuche 2) Absender 3) Betreff 4) Datum 5) Zeilenanzahl q) Suche beenden\n--> :");
  277. String x = scan.nextLine();
  278. if(x.charAt(0) == 'q')
  279. return false;
  280. while(!isInteger(x) || x.length() > 1 ){
  281. System.out.print("Bitte nur einen Buchstabe eingeben\n--> : ");
  282. x = scan.nextLine();
  283. }
  284. switch(x.charAt(0)){
  285. case '1' : System.out.println("Bitte geben Sie ein Schlüsselwort-, oder -satz ein: ");
  286. volltext = scan.nextLine();
  287. break;
  288. case '2' : System.out.print("(Such) Absender eingeben : ");
  289. absender = scan.nextLine();
  290. break;
  291. case '3' : System.out.print("(Such) Betreff eingeben : ");
  292. betreff = scan.nextLine();
  293. break;
  294. case '4' : System.out.print("(Such) Datum eingeben (tt.mm.jjjj) : ");
  295. datum = scan.nextLine();
  296. //Prüfe eingabe
  297. while(!isDatum(datum)){
  298. System.out.print("Datum bitte genau in diesem Format eingeben: (tt.mm.jjjj) : ");
  299. datum = scan.nextLine();
  300. }
  301. break;
  302. case '5' : System.out.print("Maximal-Zeilenanzahl des Inhaltes eingeben : ");
  303. String s = scan.nextLine();
  304. //Prüfe eingabe
  305. while(!isInteger(s) || Integer.parseInt(s) < 1){
  306. System.out.print("Bitte nur natürliche Zahlen eingeben : ");
  307. s = scan.nextLine();
  308. }
  309. size = Integer.parseInt(s);
  310. break;
  311. default : break;
  312. }
  313. return true;
  314. }
  315. public static void gesuchteMails(Part p) throws Exception {
  316. if (p instanceof Message) {
  317. Message msg = (Message)p;
  318. Address[] a;
  319. if ((a = msg.getFrom()) != null) {
  320. int j;
  321. for ( j = 0; j < a.length; j++)
  322. System.out.println("Von: " + a[j].toString());
  323. }
  324. if ((a = msg.getRecipients(Message.RecipientType.TO)) != null) {
  325. int j;
  326. for ( j = 0; j < a.length; j++)
  327. System.out.println("Nach: " + a[j].toString());
  328. }
  329. Date d = msg.getSentDate();
  330. System.out.println("Am: " + d.toLocaleString());
  331. System.out.println("Betreff: : " + msg.getSubject());
  332. System.out.println("Inhalt: \n" + msg.getContent().toString());
  333.  
  334. }
  335. }
  336. static String setzeInhalt() throws IOException{
  337. BufferedReader in =new BufferedReader(new InputStreamReader(System.in));
  338. StringBuilder sb=new StringBuilder();
  339. String s,x;
  340. while((s = in.readLine()).length()>0){
  341. sb.append(s);
  342. sb.append("\n");
  343. System.out.flush();
  344. while((s = in.readLine()).length()>0){
  345. sb.append(s);
  346. sb.append("\n");
  347. System.out.flush();
  348. }
  349. System.out.print("Absenden? y | n : ");
  350. x = scan.nextLine();
  351. if(x.charAt(0)=='y')
  352. return sb.toString();
  353. else
  354. System.out.printf("(weiter im text:)\n\n"+sb.toString()+"\n");
  355. }
  356. return sb.toString();
  357. }
  358. //Ab hier stehen die Fehlerbehandlungsfunktionen
  359. static boolean isInteger(String s){
  360. try {
  361. Integer.parseInt(s);
  362. }
  363. catch(NumberFormatException nfx){
  364. return false;
  365. }catch(NullPointerException nx){
  366. return false;
  367. } return true;
  368. }
  369. static boolean isBuchstabe(String s){
  370. return s.matches("[a-zA-Z]+");
  371. }
  372. static boolean isDatum(String s){
  373. if(s.length() < 10 || s.length() > 10)
  374. return false;
  375. //Prüfe auf Zahlen im Datumsformat
  376. StringBuilder x=new StringBuilder();
  377. x.append(s.charAt(0));
  378. x.append(s.charAt(1));
  379. if(!isInteger(x.toString())){
  380. return false;}
  381. x.append(s.charAt(3));
  382. x.append(s.charAt(4));
  383. if(!isInteger(x.toString())){
  384. return false;}
  385. x.append(s.charAt(6));
  386. x.append(s.charAt(7));
  387. x.append(s.charAt(8));
  388. x.append(s.charAt(9));
  389. if(!isInteger(x.toString())){
  390. return false;}
  391. x.delete(0, 10);
  392. System.out.println(""+x);
  393. x.append(s.charAt(2));
  394. x.append(s.charAt(5));
  395. System.out.println(""+x);
  396. // Prüfe auf Trenn-Punkt im Datumsformat
  397. for(int i=0;i<2;i++){
  398. if(x.charAt(i) != '.')
  399. return false;
  400. }
  401. // Nur wenn das Datum korrekt eingegeben wurde
  402. return true;
  403. }
  404. static boolean isMail(String s, char at) {
  405. //mindestens 5 Zeichen sollte die Mail schon lang sein :D
  406. if (s.length() < 5)
  407. return false;
  408. int i;
  409. for(i=0;i<s.length();i++){
  410. if(s.charAt(i) == at)
  411. return true; // @ - Zeichen gefunden
  412. }
  413. return false; // @ - Zeichen nicht gefunden
  414. }
  415. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement