Use JavaMail API to reveive all Google mails

package com.tutego.insel.mail;

import java.util.*;
import javax.mail.*;
import javax.mail.internet.ContentType;
import javax.swing.JOptionPane;

public class GetEMails
{
public static void getMail( final Properties props ) throws Exception
{
Session session = Session.getInstance( props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication( props.getProperty( "mail.pop3.user" ),
props.getProperty( "mail.pop3.password" ) );
}
} );
session.setDebug( true );

Store store = session.getStore( "pop3" );
store.connect();

Folder folder = store.getFolder( "INBOX" );
folder.open( Folder.READ_ONLY );

Message message[] = folder.getMessages();

for ( int i = 0; i < message.length; i++ )
{
Message m = message[i];

System.out.println( "-------------------------\nNachricht: " + i );
System.out.println( "Von: " + Arrays.toString(m.getFrom()) );
System.out.println( "Betreff: " + m.getSubject() );
System.out.println( "Gesendet am: " + m.getSentDate() );
System.out.println( "Content-Type: " + new ContentType(m.getContentType()) );

if ( m.isMimeType("text/plain") )
System.out.println( m.getContent() );
}
folder.close( false );
store.close();
}

public static void main( String[] args ) throws Exception
{
Properties props = new Properties();
props.setProperty( "mail.pop3.host", "pop.gmail.com" );
props.setProperty( "mail.pop3.user", JOptionPane.showInputDialog( "user" ) );
props.setProperty( "mail.pop3.password", JOptionPane.showInputDialog( "pass" ) );
props.setProperty( "mail.pop3.port", "995" );
props.setProperty( "mail.pop3.auth", "true" );
props.setProperty( "mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory" );

getMail( props );
}
}

Labels:

6 Antwort(en) auf ›Use JavaMail API to reveive all Google mails‹

  1. # Anonymous Anonym

    Interessanter Code-Schnipsel. Jetzt würde mich noch das Gegenstück interessieren: Das beliebige Einstellen von Mails in einen Mail-Account.
    Also meinetwegen ein Schnipsel, der alle diese Googlemails nimmt und in einen yahoo-Account kopiert. Oder so was.
    Matthias  

  2. # Blogger Daniel Manzke

    Mit knallhartem Copy&Paste habe ich mal den Schnipsel ausprobiert. Leider treffe ich auf einen Fehler. Garantiert nur ein Konfigurationsfehler. Ich benutze die Java EE 5 API um JavaMail benutzen zu können. Irgendwelche Vorschläge?

    Exception in thread "main" javax.mail.NoSuchProviderException: pop3

    Gruß & Danke,
    Daniel  

  3. # Blogger Christian Ullenboom

    Spontan keine Ahnung. Aber wenn ich den String "
    Exception in thread "main" javax.mail.NoSuchProviderException: pop3" bei Google eingebe bekomme ich ziemlich viele Hinweise. Ist da nichts verwertbares bei?  

  4. # Anonymous kunee

    Hallo Herr Ullenboom,

    funktioniert ihr Code-Schnipsel noch ordentlich? Ich bekomme folgende Fehlermeldung:
    [SYS/PERM] Your account is not enabled for POP access. Please visit your Gmail settings page and enable your account for POP access.

    Das selbe bei IMAP trotzdem beide enabled sind. Haben Sie vielleicht einen Tipp?  

  5. # Blogger Christian Ullenboom

    Hallo kunee. Ich habe das Beispiel gerade noch mal ausprobiert und es funktioniert. Die Exception bekomme ich nur dann, wenn "Enable POP for all mail" nicht aktiviert ist. Aaah. Ich weiß warum. Wenn der Name nicht mit @googlemail.com endet, gibt es die Exception -- auf die Idee brachte mich eine Google-Suche bei dieser Exception.  

  6. # Blogger zeeman

    Hi!

    Ich bin nun schon mehrfach auf deiner Seite gelandet.

    Die Codeschnipsel hier sind immer sehr, sehr hilfreich.

    Danke dir!  

Kommentar veröffentlichen