Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
Ricevi e-Mail  - Finestra.java

Finestra.java

Caricato da: Mutelg
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * finestra.java
  8.  *
  9.  * Created on 24-apr-2009, 16.58.31
  10.  */
  11.  
  12. package riceviemail;
  13. import java.io.*;
  14. import java.net.*;
  15. import javax.swing.text.html.HTMLEditorKit;
  16. /**
  17.  *
  18.  * @author Administrator
  19.  */
  20. public class Finestra extends javax.swing.JFrame {
  21.  
  22.     /** Creates new form finestra */
  23.     private static final int porta = 110;
  24.     private static BufferedReader dalServer;
  25.     private static PrintStream alServer;
  26.     private static Socket connect;
  27.     private static String user,
  28.                    pass,
  29.                    server;
  30.  
  31.     public Finestra() {
  32.         initComponents();
  33.     }
  34.  
  35.     /** This method is called from within the constructor to
  36.      * initialize the form.
  37.      * WARNING: Do NOT modify this code. The content of this method is
  38.      * always regenerated by the Form Editor.
  39.      */
  40.     @SuppressWarnings("unchecked")
  41.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  42.     private void initComponents() {
  43.  
  44.         User = new javax.swing.JTextField();
  45.         username = new javax.swing.JLabel();
  46.         Pass = new javax.swing.JPasswordField();
  47.         password = new javax.swing.JLabel();
  48.         Test = new javax.swing.JButton();
  49.         Server = new javax.swing.JTextField();
  50.         serverpop3 = new javax.swing.JLabel();
  51.         Login = new javax.swing.JButton();
  52.         risultati = new javax.swing.JLabel();
  53.         anteprima = new javax.swing.JComboBox();
  54.         Apri = new javax.swing.JButton();
  55.         Cancella = new javax.swing.JButton();
  56.         Logout = new javax.swing.JButton();
  57.         jScrollPane1 = new javax.swing.JScrollPane();
  58.         VisMail = new javax.swing.JEditorPane();
  59.  
  60.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  61.         setName("EnricMailRecieved"); // NOI18N
  62.  
  63.         username.setText("User   Es. \"mario@virgilio.it\"");
  64.  
  65.         password.setText("Pass  Es.  \"pippobaubau\"");
  66.  
  67.         Test.setText("TestConnessione");
  68.         Test.addActionListener(new java.awt.event.ActionListener() {
  69.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  70.                 TestConnessioneAction(evt);
  71.             }
  72.         });
  73.  
  74.         serverpop3.setText("Server POP3  Es.  \"in.virgilio.it\"");
  75.  
  76.         Login.setText("LogIn");
  77.         Login.setEnabled(false);
  78.         Login.addActionListener(new java.awt.event.ActionListener() {
  79.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  80.                 LoginAction(evt);
  81.             }
  82.         });
  83.  
  84.         anteprima.setFont(new java.awt.Font("Arial", 0, 9));
  85.  
  86.         Apri.setText("Apri");
  87.         Apri.setEnabled(false);
  88.         Apri.addActionListener(new java.awt.event.ActionListener() {
  89.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  90.                 ApriAction(evt);
  91.             }
  92.         });
  93.  
  94.         Cancella.setText("Cancella");
  95.         Cancella.setEnabled(false);
  96.         Cancella.addActionListener(new java.awt.event.ActionListener() {
  97.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  98.                 CancellaAction(evt);
  99.             }
  100.         });
  101.  
  102.         Logout.setText("LogOut");
  103.         Logout.setEnabled(false);
  104.         Logout.addActionListener(new java.awt.event.ActionListener() {
  105.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  106.                 LogoutAction(evt);
  107.             }
  108.         });
  109.  
  110.         VisMail.setEditable(false);
  111.         jScrollPane1.setViewportView(VisMail);
  112.  
  113.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  114.         getContentPane().setLayout(layout);
  115.         layout.setHorizontalGroup(
  116.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  117.             .addGroup(layout.createSequentialGroup()
  118.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  119.                     .addGroup(layout.createSequentialGroup()
  120.                         .addGap(96, 96, 96)
  121.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
  122.                             .addComponent(Pass)
  123.                             .addComponent(Server)
  124.                             .addComponent(User, javax.swing.GroupLayout.DEFAULT_SIZE, 154, Short.MAX_VALUE)
  125.                             .addGroup(layout.createSequentialGroup()
  126.                                 .addGap(8, 8, 8)
  127.                                 .addComponent(Test)))
  128.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  129.                         .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  130.                             .addComponent(username, javax.swing.GroupLayout.PREFERRED_SIZE, 159, javax.swing.GroupLayout.PREFERRED_SIZE)
  131.                             .addGroup(layout.createSequentialGroup()
  132.                                 .addComponent(Login)
  133.                                 .addGap(18, 18, 18)
  134.                                 .addComponent(Logout))
  135.                             .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
  136.                                 .addComponent(password, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  137.                                 .addComponent(serverpop3, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
  138.                     .addGroup(layout.createSequentialGroup()
  139.                         .addGap(18, 18, 18)
  140.                         .addComponent(Apri, javax.swing.GroupLayout.PREFERRED_SIZE, 69, javax.swing.GroupLayout.PREFERRED_SIZE)
  141.                         .addGap(36, 36, 36)
  142.                         .addComponent(Cancella))
  143.                     .addGroup(layout.createSequentialGroup()
  144.                         .addGap(38, 38, 38)
  145.                         .addComponent(risultati, javax.swing.GroupLayout.PREFERRED_SIZE, 430, javax.swing.GroupLayout.PREFERRED_SIZE)))
  146.                 .addGap(46, 46, 46))
  147.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  148.                 .addContainerGap()
  149.                 .addComponent(anteprima, 0, 494, Short.MAX_VALUE)
  150.                 .addContainerGap())
  151.             .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 514, Short.MAX_VALUE)
  152.         );
  153.         layout.setVerticalGroup(
  154.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  155.             .addGroup(layout.createSequentialGroup()
  156.                 .addGap(14, 14, 14)
  157.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  158.                     .addComponent(User, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  159.                     .addComponent(username))
  160.                 .addGap(21, 21, 21)
  161.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  162.                     .addComponent(Pass, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  163.                     .addComponent(password))
  164.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  165.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  166.                     .addComponent(Server, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
  167.                     .addComponent(serverpop3))
  168.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  169.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  170.                     .addComponent(Test)
  171.                     .addComponent(Login)
  172.                     .addComponent(Logout))
  173.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  174.                 .addComponent(risultati, javax.swing.GroupLayout.PREFERRED_SIZE, 24, javax.swing.GroupLayout.PREFERRED_SIZE)
  175.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  176.                 .addComponent(anteprima, javax.swing.GroupLayout.PREFERRED_SIZE, 20, javax.swing.GroupLayout.PREFERRED_SIZE)
  177.                 .addGap(13, 13, 13)
  178.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  179.                     .addComponent(Apri)
  180.                     .addComponent(Cancella))
  181.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  182.                 .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 172, javax.swing.GroupLayout.PREFERRED_SIZE)
  183.                 .addContainerGap(40, Short.MAX_VALUE))
  184.         );
  185.  
  186.         pack();
  187.     }// </editor-fold>//GEN-END:initComponents
  188.  
  189.     private void TestConnessioneAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_TestConnessioneAction
  190.       user = User.getText();
  191.       pass = Pass.getText();
  192.       server = Server.getText();
  193.       boolean conn = connessione.controllo(server, user, pass);
  194.       if(conn)
  195.       {
  196.         risultati.setText("Dati inseriti correttamente! Effettua il Login!");
  197.         Login.setEnabled(true);
  198.       }else{
  199.         risultati.setText("Dati inseriti non corretti! Prego controllare i dati!");
  200.         Login.setEnabled(false);
  201.       }
  202.     }//GEN-LAST:event_TestConnessioneAction
  203.  
  204.     private void LoginAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LoginAction
  205.  
  206.       user = User.getText();
  207.       pass = Pass.getText();
  208.       server = Server.getText();
  209.       try{
  210.           CreaStream();
  211.           if(connessione.login(dalServer, alServer, user, pass))
  212.           {
  213.            Logout.setEnabled(true);
  214.            Apri.setEnabled(true);
  215.            Cancella.setEnabled(true);
  216.            VisMail.setText("");
  217.            int numMail = Mail.contaMail(dalServer,alServer);
  218.            System.out.println("Letto Numero Mail");
  219.            for(int i=1; i<=numMail;i++)
  220.            {
  221.             //* Opzione 0 = Mittente
  222.             //* Opzione 1 = Oggetto
  223.             //* Opzione 2 = Messaggio
  224.             /*String mittente = Mail.mail(dalServer, alServer, i, 0),
  225.                    oggetto = Mail.mail(dalServer, alServer, i, 1);
  226.              */
  227.              String mittente = Mail.leggiMittente(dalServer, alServer, i),
  228.                     oggetto = Mail.leggiOggetto(dalServer, alServer, i);
  229.  
  230.             anteprima.addItem(i+": Mittente: "+mailMittente(mittente)+"     Oggetto: "+oggetto);
  231.             System.out.println("Letta con successo mail numero "+i);
  232.            }
  233.            
  234.           }else{
  235.             risultati.setText("Errore nel Login! Controllare i dati!");
  236.             Login.setEnabled(false);
  237.             Logout.setEnabled(false);
  238.             Apri.setEnabled(false);
  239.             Cancella.setEnabled(false);
  240.             VisMail.setText("");
  241.             anteprima.removeAllItems();
  242.           }
  243.       }catch(Exception e)
  244.       {
  245.        risultati.setText("Errore connessione! Controllare la connessione!");
  246.        System.out.println(e+"");
  247.        Login.setEnabled(false);
  248.        Logout.setEnabled(false);
  249.        Apri.setEnabled(false);
  250.        Cancella.setEnabled(false);
  251.        VisMail.setText("");
  252.        anteprima.removeAllItems();
  253.       }
  254.     }//GEN-LAST:event_LoginAction
  255.  
  256.     private void LogoutAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_LogoutAction
  257.      connessione.logout(dalServer,alServer);
  258.      Login.setEnabled(false);
  259.      Logout.setEnabled(false);
  260.      Apri.setEnabled(false);
  261.      Cancella.setEnabled(false);
  262.      VisMail.setText("");
  263.      risultati.setText("LogOut effettuato!");
  264.     }//GEN-LAST:event_LogoutAction
  265.  
  266.     private void CancellaAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_CancellaAction
  267.      int n = anteprima.getSelectedIndex();
  268.      VisMail.setText("");
  269.      System.out.println("Stai cancellando la Mail n° "+(n+1));
  270.      if(!connessione.controlloLogin(dalServer, alServer))
  271.      {
  272.        CreaStream();
  273.        connessione.login(dalServer, alServer, user, pass);
  274.      }
  275.      if(Mail.cancellaMail(dalServer, alServer, n+1))
  276.      {
  277.       risultati.setText("E-mail Eliminata! Verra cancellata del tutto al primo LogOut!");
  278.       anteprima.removeItemAt(n);
  279.      }else{
  280.       risultati.setText("Impossibile Cancellare l'e-Mail");
  281.      }
  282.     }//GEN-LAST:event_CancellaAction
  283.  
  284.     private void ApriAction(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_ApriAction
  285.     int n = anteprima.getSelectedIndex();
  286.     VisMail.setEditorKit(new HTMLEditorKit());
  287.      System.out.println("Stai aprendo la Mail n° "+(n+1));
  288.      if(!connessione.controlloLogin(dalServer, alServer))
  289.      {
  290.        CreaStream();
  291.        connessione.login(dalServer, alServer, user, pass);
  292.      }
  293.      if((n+1)>Mail.contaMail(dalServer, alServer))
  294.      {
  295.        VisMail.setText("");
  296.        risultati.setText("La Mail non Esiste!");
  297.      }else{
  298.             //* Opzione 0 = Mittente
  299.             //* Opzione 1 = Oggetto
  300.             //* Opzione 2 = Messaggio
  301.       /* String mittente = Mail.mail(dalServer, alServer, n+1, 0),
  302.               oggetto = Mail.mail(dalServer, alServer, n+1, 1),
  303.               messaggio = Mail.mail(dalServer, alServer, n+1, 2);
  304.        */
  305.        String mittente = Mail.leggiMittente(dalServer, alServer, n+1),
  306.               oggetto = Mail.leggiOggetto(dalServer, alServer, n+1),
  307.               messaggio = Mail.leggiMessaggio(dalServer, alServer, n+1);
  308.        
  309.        VisMail.setText("Da : "+mittente+"<br>A : "+user+"<br><br>Oggetto : "+oggetto+"<br>---------------------------------------------------------<br>"+messaggio);
  310.      }
  311.     }//GEN-LAST:event_ApriAction
  312.  
  313.     /**
  314.     * @param args the command line arguments
  315.     */
  316.     public static void main(String args[]) {
  317.         java.awt.EventQueue.invokeLater(new Runnable() {
  318.             public void run() {
  319.                 new Finestra().setVisible(true);
  320.             }
  321.         });
  322.     }
  323.     static void CreaStream()
  324.     {
  325.        try{
  326.           connect = new Socket(server,porta);
  327.           alServer = new PrintStream(connect.getOutputStream());
  328.           dalServer = new BufferedReader(new InputStreamReader(connect.getInputStream()));
  329.           System.out.println("Connessione creata: "+connessione.risposta(dalServer));
  330.        }catch(IOException e)
  331.        {
  332.         System.out.println("Impossibile creare stream!!");
  333.        }
  334.     }
  335.     static String mailMittente(String mittente)
  336.     {
  337.         String mail="";
  338.         if(mittente.charAt(mittente.length()-1)=='>')
  339.         {
  340.           mail = mittente.substring(mittente.indexOf("<")+1,mittente.length()-1);
  341.           return mail;
  342.         }else
  343.           return mittente;
  344.     }
  345.     // Variables declaration - do not modify//GEN-BEGIN:variables
  346.     private javax.swing.JButton Apri;
  347.     private javax.swing.JButton Cancella;
  348.     private javax.swing.JButton Login;
  349.     private javax.swing.JButton Logout;
  350.     private javax.swing.JPasswordField Pass;
  351.     private javax.swing.JTextField Server;
  352.     private javax.swing.JButton Test;
  353.     private javax.swing.JTextField User;
  354.     private javax.swing.JEditorPane VisMail;
  355.     private javax.swing.JComboBox anteprima;
  356.     private javax.swing.JScrollPane jScrollPane1;
  357.     private javax.swing.JLabel password;
  358.     private javax.swing.JLabel risultati;
  359.     private javax.swing.JLabel serverpop3;
  360.     private javax.swing.JLabel username;
  361.     // End of variables declaration//GEN-END:variables
  362.  
  363. }