Java - No puedo read el buzon de entrada mi correo JAVA

 
Vista:

No puedo read el buzon de entrada mi correo JAVA

Publicado por AnDrew (1 intervención) el 15/01/2021 22:34:17
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
//<editor-fold defaultstate="collapsed" desc="ZONA DE PAQUETES">
package javaapplication25;
//</editor-fold>
//<editor-fold defaultstate="collapsed" desc="ZONA DE IMPORTACIONES">
 
import javax.mail.Part;
import javax.mail.Store;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.Session;
import javax.swing.JFrame;
import javax.swing.JLabel;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Multipart;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import java.io.FileOutputStream;
import java.io.FileNotFoundException;
import javax.mail.MessagingException;
//</editor-fold>
/**
    * Ejemplo de recepción de mensajes con JavaMail
    *
    * @author Chuidiang
    *
 */
//<editor-fold defaultstate="collapsed" desc="CLASE">
public class RecibirMail
{
    /**
        * main de la clase.
        *
        * @param args Se ignoran
    */
    public static void main(String[] args)
    {
        //<editor-fold defaultstate="collapsed" desc="ACTIONS">
        // Se obtiene la Session
        Properties prop=new Properties();
        prop.setProperty("mail.pop3.starttls.enable","false");
        prop.setProperty("mail.pop3.socketFactory.class","javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.pop3.socketFactory.fallback","false");
        prop.setProperty("mail.pop3.port","995");
        prop.setProperty("mail.pop3.socketFactory.port","995");
        Session sesion=Session.getInstance(prop);
        //sesion.setDebug(true);
        try
        {
            //<editor-fold defaultstate="collapsed" desc="ACTIONS">
            // Se obtiene el Store y el Folder,para poder leer el correo.
            Store store=sesion.getStore("pop3");
            store.connect
            (
                "mail.gmail.com",
                "MAIL_ACCOUNT",
                "USER_PASSWORD"
            );
            Folder folder=store.getFolder("INBOX");
            folder.open(Folder.READ_ONLY);
            // Se obtienen los mensajes.
            Message[] mensajes=folder.getMessages();
            // Se escribe from y subject de cada mensaje
            for (int i=0; i < mensajes.length; i++)
            {
                System.out.println("From:" + mensajes[i].getFrom()[0].toString());
                System.out.println("Subject:" + mensajes[i].getSubject());
                // Se visualiza,si se sabe como,el contenido de cada mensaje
                analizaParteDeMensaje(mensajes[i]);
            }//ff
            folder.close(false);
            store.close();
            //</editor-fold>
        }//ft
        catch(Exception e)
        {
            e.printStackTrace();
        }//fc
        //</editor-fold>
    }//fmp
    /**
     * Metodo recursivo. Si la parte que se pasa es compuesta,se extrae cada
     * una de las subpartes y el metodo se llama a si mismo con cada una de
     * ellas. Si la parte es un text,se escribe en pantalla. Si la parte es una
     * image,se guarda en un fichero y se visualiza en un JFrame. En cualquier
     * otro caso,simplemente se escribe el tipo recibido,pero se ignora el
     * mensaje.
     *
     * @param unaParte Parte del mensaje a analizar.
     */
    private static void analizaParteDeMensaje(Part unaParte)
    {
        try
        {
            // Si es multipart,se analiza cada una de sus partes recursivamente.
            if (unaParte.isMimeType("multipart/*")) {
                Multipart multi;
                multi=(Multipart) unaParte.getContent();
                for (int j=0; j < multi.getCount(); j++) {
                    analizaParteDeMensaje(multi.getBodyPart(j));
                }
            } else {
                // Si es texto,se escribe el texto.
                if (unaParte.isMimeType("text/*")) {
                    System.out.println("Texto " + unaParte.getContentType());
                    System.out.println(unaParte.getContent());
                    System.out.println("---------------------------------");
                } else {
                    // Si es imagen,se guarda en fichero y se visualiza en JFrame
                    if (unaParte.isMimeType("image/*")) {
                        System.out.println(
                                "Imagen " + unaParte.getContentType());
                        System.out.println("Fichero=" + unaParte.getFileName());
                        System.out.println("---------------------------------");
                        salvaImagenEnFichero(unaParte);
                        visualizaImagenEnJFrame(unaParte);
                    } else {
                        // Si no es ninguna de las anteriores,se escribe en pantalla
                        // el tipo.
                        System.out.println(
                                "Recibido " + unaParte.getContentType());
                        System.out.println("---------------------------------");
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * Presupone que unaParte es una foto adjunta a un correo. Recoge la imagen
     * y la visualiza en un JFrame
     *
     * @param unaParte Parte de un correo correspondiente a una imagen.
     *
     * @throws IOException
     * @throws MessagingException
     */
    private static void visualizaImagenEnJFrame(Part unaParte)
            throws IOException,MessagingException {
        JFrame v=new JFrame();
        ImageIcon icono=new ImageIcon(
                ImageIO.read(unaParte.getInputStream()));
        JLabel l=new JLabel(icono);
        v.getContentPane().add(l);
        v.pack();
        v.setVisible(true);
    }
    /**
        * Supone que unaParte corresponde a una imagen de un fichero y que
        * getFileName() esta relleno. Salva la imagen en d:\getFileName().
        *
        * @param unaParte Parte de un correo correspondiente a una imagen.
        *
        * @throws FileNotFoundException
        * @throws MessagingException
        * @throws IOException
    */
    private static void salvaImagenEnFichero(Part unaParte)throws FileNotFoundException,MessagingException,IOException
    {
        //<editor-fold defaultstate="collapsed" desc="ACTIONS">
        FileOutputStream fichero=new FileOutputStream("C:/Users/Administrador/Desktop/Adjunto");
        InputStream imagen=unaParte.getInputStream();
        byte[] bytes=new byte[1000];
        int leidos=0;
        while((leidos=imagen.read(bytes))>0)
            fichero.write(bytes,0,leidos);
        //</editor-fold>
    }//fm
}//fcla
//</editor-fold>
he probado demasiado muchos algoritmos y todos no me funcionan alguien tiene alguna clase que funcione para gmail o yahoo etc Gracias!
Valora esta pregunta
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder