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: Insel

6 Comments:
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
By
Anonym, at September 14, 2007 10:56 PM
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
By
Daniel Manzke, at November 29, 2007 1:44 AM
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?
By
Christian Ullenboom, at November 29, 2007 8:27 AM
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?
By
kunee, at Dezember 24, 2007 3:18 AM
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.
By
Christian Ullenboom, at Dezember 24, 2007 10:24 AM
Hi!
Ich bin nun schon mehrfach auf deiner Seite gelandet.
Die Codeschnipsel hier sind immer sehr, sehr hilfreich.
Danke dir!
By
zeeman, at März 17, 2008 3:27 PM
Kommentar veröffentlichen
<< Home