Advertisement
Guest User

Untitled

a guest
Feb 26th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.70 KB | None | 0 0
  1. package Laby;
  2.  
  3. import javax.mail.*;
  4. import javax.mail.internet.AddressException;
  5. import javax.mail.internet.InternetAddress;
  6. import javax.mail.internet.MimeMessage;
  7. import java.io.IOException;
  8. import java.sql.SQLOutput;
  9. import java.util.Properties;
  10. import java.util.Scanner;
  11.  
  12. public class Main {
  13.  
  14. public static void send(String smtpHost, int smtpPort, String from, String to, String subject,
  15. String content) throws AddressException, MessagingException {
  16. Properties props = new Properties();
  17. props.put("mail.smtp.host", smtpHost);
  18. props.put("mail.smtp.port", "" + smtpPort);
  19. Session session = Session.getDefaultInstance(props, null);
  20. Message msg = new MimeMessage(session);
  21. msg.setFrom(new InternetAddress(from));
  22. msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
  23. msg.setSubject(subject);
  24. msg.setText(content);
  25. Transport.send(msg);
  26. }
  27.  
  28. public static String getMail(String host, String username, String password, String provider) throws NoSuchProviderException, MessagingException, IOException {
  29. Properties props = new Properties();
  30. Session session = Session.getDefaultInstance(props, null);
  31. Store store = session.getStore(provider);
  32. store.connect(host, username, password);
  33. Folder inbox = store.getFolder("INBOX");
  34. if (inbox == null) {
  35. System.out.println("No INBOX");
  36. System.exit(1);
  37. }
  38. inbox.open(Folder.READ_ONLY);
  39. Message[] messages = inbox.getMessages();
  40. for (int i = 0; i < messages.length; i++) {
  41. System.out.println("Message " + (i + 1));
  42. messages[i].writeTo(System.out);
  43. }
  44. inbox.close(false);
  45. store.close();
  46. if (messages.length > 0 && messages[0].getContent() instanceof String) {
  47. return (String) messages[0].getContent();
  48. }
  49. return null;
  50. }
  51.  
  52. public static void main(String[] args) {
  53.  
  54. String a, b, c, d, f, g;
  55.  
  56. Scanner sc = new Scanner(System.in);
  57.  
  58. int wybor = 1;
  59. while (wybor != 0) {
  60.  
  61. System.out.println("1.Wyslij wiadomosc");
  62. System.out.println("2.Odbierz wiadomosc");
  63. System.out.print("Wybierz:");
  64.  
  65. wybor = sc.nextInt();
  66.  
  67. switch (wybor) {
  68. case 1:
  69. System.out.println("Podaj nadawece: ");
  70. a = sc.next();
  71. System.out.print("Podaj odbiorce: ");
  72. b = sc.next();
  73. System.out.print("Podaj temat: ");
  74. c = sc.next();
  75. System.out.print("Podaj tresc: ");
  76. d = sc.next();
  77. Wiadomosc w1 = new Wiadomosc(a, b, c, d);
  78. try {
  79. send(w1.getHost(), w1.getPort(), w1.getNadawca(), w1.getOdbiorca(), w1.getTemat(), w1.getZawartosc());
  80. } catch (MessagingException e) {
  81. e.printStackTrace();
  82. }
  83. break;
  84.  
  85. case 2:
  86. System.out.println("Podaj login: ");
  87. f = sc.next();
  88. System.out.print("Podaj haslo: ");
  89. g = sc.next();
  90. Odbierz odbierz1 = new Odbierz(f, g);
  91. try {
  92. getMail(odbierz1.getHost(),odbierz1.getUser(),odbierz1.getPassword(),odbierz1.getProvider());
  93. } catch (MessagingException e) {
  94. e.printStackTrace();
  95. } catch (IOException e) {
  96. e.printStackTrace();
  97. }
  98. break;
  99.  
  100. }
  101. }
  102.  
  103. /*
  104. try {
  105. send("localhost", 25, "user1@localhost", "user1@localhost", "Gra kółko i krzyżyk", "Start Gry");
  106. } catch (AddressException ex) {
  107. ex.printStackTrace();
  108. } catch (MessagingException ex) {
  109. ex.printStackTrace();
  110. }
  111. try {
  112. String wiadomosc = getMail("localhost", "user1", "user1", "pop3");
  113. System.out.println(wiadomosc);
  114. } catch (NoSuchProviderException ex) {
  115. ex.printStackTrace();
  116. } catch (MessagingException ex) {
  117. ex.printStackTrace();
  118. } catch (IOException ex) {
  119. ex.printStackTrace();
  120. }
  121. */
  122.  
  123.  
  124. }
  125.  
  126.  
  127. }
  128.  
  129.  
  130.  
  131. package Laby;
  132.  
  133. public class Wiadomosc {
  134.  
  135. private String nadawca, odbiorca, temat, zawartosc, host;
  136. private int port;
  137.  
  138. public Wiadomosc(String nadawca, String odbiorca, String temat, String zawartosc) {
  139. this.nadawca = nadawca + "@localhost";
  140. this.odbiorca = odbiorca + "@localhost";
  141. this.temat = temat;
  142. this.zawartosc = zawartosc;
  143. this.host = "localhost";
  144. this.port = 25;
  145. }
  146.  
  147. public void setNadawca(String nadawca) {
  148. this.nadawca = nadawca;
  149. }
  150.  
  151. public void setOdbiorca(String odbiorca) {
  152. this.odbiorca = odbiorca;
  153. }
  154.  
  155. public void setTemat(String temat) {
  156. this.temat = temat;
  157. }
  158.  
  159. public void setZawartosc(String zawartosc) {
  160. this.zawartosc = zawartosc;
  161. }
  162.  
  163. public void setHost(String host) {
  164. this.host = host;
  165. }
  166.  
  167. public void setPort(int port) {
  168. this.port = port;
  169. }
  170.  
  171. public String getNadawca() {
  172. return nadawca;
  173. }
  174.  
  175. public String getOdbiorca() {
  176. return odbiorca;
  177. }
  178.  
  179. public String getTemat() {
  180. return temat;
  181. }
  182.  
  183. public String getZawartosc() {
  184. return zawartosc;
  185. }
  186.  
  187. public String getHost() {
  188. return host;
  189. }
  190.  
  191. public int getPort() {
  192. return port;
  193. }
  194. }
  195.  
  196.  
  197.  
  198. package Laby;
  199.  
  200. public class Odbierz {
  201.  
  202. String host,user,password,provider;
  203.  
  204. public Odbierz(String user, String password) {
  205. this.host = "localhost";
  206. this.user = user;
  207. this.password = password;
  208. this.provider = "pop3";
  209. }
  210.  
  211. public String getHost() {
  212. return host;
  213. }
  214.  
  215. public String getUser() {
  216. return user;
  217. }
  218.  
  219. public String getPassword() {
  220. return password;
  221. }
  222.  
  223. public String getProvider() {
  224. return provider;
  225. }
  226.  
  227. public void setHost(String host) {
  228. this.host = host;
  229. }
  230.  
  231. public void setUser(String user) {
  232. this.user = user;
  233. }
  234.  
  235. public void setPassword(String password) {
  236. this.password = password;
  237. }
  238.  
  239. public void setProvider(String provider) {
  240. this.provider = provider;
  241. }
  242. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement