Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Laby;
- import javax.mail.*;
- import javax.mail.internet.AddressException;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.io.IOException;
- import java.sql.SQLOutput;
- import java.util.Properties;
- import java.util.Scanner;
- public class Main {
- public static void send(String smtpHost, int smtpPort, String from, String to, String subject,
- String content) throws AddressException, MessagingException {
- Properties props = new Properties();
- props.put("mail.smtp.host", smtpHost);
- props.put("mail.smtp.port", "" + smtpPort);
- Session session = Session.getDefaultInstance(props, null);
- Message msg = new MimeMessage(session);
- msg.setFrom(new InternetAddress(from));
- msg.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
- msg.setSubject(subject);
- msg.setText(content);
- Transport.send(msg);
- }
- public static String getMail(String host, String username, String password, String provider) throws NoSuchProviderException, MessagingException, IOException {
- Properties props = new Properties();
- Session session = Session.getDefaultInstance(props, null);
- Store store = session.getStore(provider);
- store.connect(host, username, password);
- Folder inbox = store.getFolder("INBOX");
- if (inbox == null) {
- System.out.println("No INBOX");
- System.exit(1);
- }
- inbox.open(Folder.READ_ONLY);
- Message[] messages = inbox.getMessages();
- for (int i = 0; i < messages.length; i++) {
- System.out.println("Message " + (i + 1));
- messages[i].writeTo(System.out);
- }
- inbox.close(false);
- store.close();
- if (messages.length > 0 && messages[0].getContent() instanceof String) {
- return (String) messages[0].getContent();
- }
- return null;
- }
- public static void main(String[] args) {
- String a, b, c, d, f, g;
- Scanner sc = new Scanner(System.in);
- int wybor = 1;
- while (wybor != 0) {
- System.out.println("1.Wyslij wiadomosc");
- System.out.println("2.Odbierz wiadomosc");
- System.out.print("Wybierz:");
- wybor = sc.nextInt();
- switch (wybor) {
- case 1:
- System.out.println("Podaj nadawece: ");
- a = sc.next();
- System.out.print("Podaj odbiorce: ");
- b = sc.next();
- System.out.print("Podaj temat: ");
- c = sc.next();
- System.out.print("Podaj tresc: ");
- d = sc.next();
- Wiadomosc w1 = new Wiadomosc(a, b, c, d);
- try {
- send(w1.getHost(), w1.getPort(), w1.getNadawca(), w1.getOdbiorca(), w1.getTemat(), w1.getZawartosc());
- } catch (MessagingException e) {
- e.printStackTrace();
- }
- break;
- case 2:
- System.out.println("Podaj login: ");
- f = sc.next();
- System.out.print("Podaj haslo: ");
- g = sc.next();
- Odbierz odbierz1 = new Odbierz(f, g);
- try {
- getMail(odbierz1.getHost(),odbierz1.getUser(),odbierz1.getPassword(),odbierz1.getProvider());
- } catch (MessagingException e) {
- e.printStackTrace();
- } catch (IOException e) {
- e.printStackTrace();
- }
- break;
- }
- }
- /*
- try {
- send("localhost", 25, "user1@localhost", "user1@localhost", "Gra kółko i krzyżyk", "Start Gry");
- } catch (AddressException ex) {
- ex.printStackTrace();
- } catch (MessagingException ex) {
- ex.printStackTrace();
- }
- try {
- String wiadomosc = getMail("localhost", "user1", "user1", "pop3");
- System.out.println(wiadomosc);
- } catch (NoSuchProviderException ex) {
- ex.printStackTrace();
- } catch (MessagingException ex) {
- ex.printStackTrace();
- } catch (IOException ex) {
- ex.printStackTrace();
- }
- */
- }
- }
- package Laby;
- public class Wiadomosc {
- private String nadawca, odbiorca, temat, zawartosc, host;
- private int port;
- public Wiadomosc(String nadawca, String odbiorca, String temat, String zawartosc) {
- this.nadawca = nadawca + "@localhost";
- this.odbiorca = odbiorca + "@localhost";
- this.temat = temat;
- this.zawartosc = zawartosc;
- this.host = "localhost";
- this.port = 25;
- }
- public void setNadawca(String nadawca) {
- this.nadawca = nadawca;
- }
- public void setOdbiorca(String odbiorca) {
- this.odbiorca = odbiorca;
- }
- public void setTemat(String temat) {
- this.temat = temat;
- }
- public void setZawartosc(String zawartosc) {
- this.zawartosc = zawartosc;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public void setPort(int port) {
- this.port = port;
- }
- public String getNadawca() {
- return nadawca;
- }
- public String getOdbiorca() {
- return odbiorca;
- }
- public String getTemat() {
- return temat;
- }
- public String getZawartosc() {
- return zawartosc;
- }
- public String getHost() {
- return host;
- }
- public int getPort() {
- return port;
- }
- }
- package Laby;
- public class Odbierz {
- String host,user,password,provider;
- public Odbierz(String user, String password) {
- this.host = "localhost";
- this.user = user;
- this.password = password;
- this.provider = "pop3";
- }
- public String getHost() {
- return host;
- }
- public String getUser() {
- return user;
- }
- public String getPassword() {
- return password;
- }
- public String getProvider() {
- return provider;
- }
- public void setHost(String host) {
- this.host = host;
- }
- public void setUser(String user) {
- this.user = user;
- }
- public void setPassword(String password) {
- this.password = password;
- }
- public void setProvider(String provider) {
- this.provider = provider;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement