JavaMail认证

importjava.util.Properties;

importjavax.mail.Authenticator;
importjavax.mail.Folder;
importjavax.mail.Message;
importjavax.mail.PasswordAuthentication;
importjavax.mail.Session;
importjavax.mail.Store;

publicclassMainClass{

publicstaticvoidmain(String[]args)throwsException{

Propertiesprops=newProperties();

Stringhost="yourserver.edu";
Stringprovider="pop3";

Sessionsession=Session.getDefaultInstance(props,newMailAuthenticator());
Storestore=session.getStore(provider);
store.connect(host,null,null);

Folderinbox=store.getFolder("INBOX");
if(inbox==null){
System.out.println("NoINBOX");
System.exit(1);
}
inbox.open(Folder.READ_ONLY);

Message[]messages=inbox.getMessages();
for(inti=0;i<messages.length;i++){
System.out.println("Message"+(i+1));
messages[i].writeTo(System.out);
}
inbox.close(false);
store.close();
}
}

classMailAuthenticatorextendsAuthenticator{

publicMailAuthenticator(){
}

publicPasswordAuthenticationgetPasswordAuthentication(){
returnnewPasswordAuthentication("username","password");
}
}

你可能感兴趣的:(javamail)