这样就可以啦。 // Create empty properties Properties props = new Properties(); props.put("mail.host", host);// Setup authentication, get session Authenticator auth = new PasswordAuthentication(username,passwd); Session session = Session.getInstance( props, auth);// Get the store Store store = session.getStore("pop3"); store.connect();
或者Session的方法 requestPasswordAuthentication public PasswordAuthentication requestPasswordAuthentication(java.net.InetAddress addr, int port, java.lang.String protocol, java.lang.String prompt, java.lang.String defaultUserName) Call back to the application to get the needed user name and password. The application should put up a dialog something like: Connecting to <protocol> mail service on host <addr>, port <port>. <prompt> User Name: <defaultUserName> Password:
Parameters: addr - InetAddress of the host. may be null. protocol - protocol scheme (e.g. imap, pop3, etc.) prompt - any additional String to show as part of the prompt; may be null. defaultUserName - the default username. may be null. Returns: the authentication which was collected by the authenticator; may be null.
// Create empty properties
Properties props = new Properties();
props.put("mail.host", host);// Setup authentication, get session
Authenticator auth = new PasswordAuthentication(username,passwd);
Session session = Session.getInstance(
props, auth);// Get the store
Store store = session.getStore("pop3");
store.connect();
requestPasswordAuthentication
public PasswordAuthentication requestPasswordAuthentication(java.net.InetAddress addr,
int port,
java.lang.String protocol,
java.lang.String prompt,
java.lang.String defaultUserName)
Call back to the application to get the needed user name and password. The application should put up a dialog something like: Connecting to <protocol> mail service on host <addr>, port <port>.
<prompt> User Name: <defaultUserName>
Password:
Parameters:
addr - InetAddress of the host. may be null.
protocol - protocol scheme (e.g. imap, pop3, etc.)
prompt - any additional String to show as part of the prompt; may be null.
defaultUserName - the default username. may be null.
Returns:
the authentication which was collected by the authenticator; may be null.