Search in sources :

Example 1 with POP3SSLStore

use of com.sun.mail.pop3.POP3SSLStore in project Gargoyle by callakrsos.

the class MailPopReceiver method receiveEmail.

public void receiveEmail() {
    try {
        //1) get the session object  
        //new Properties();
        Properties properties = System.getProperties();
        properties.put("mail.pop3.host", popHost);
        properties.put("mail.user", userName);
        properties.put("mail.from", hostEmailAddr);
        properties.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        properties.put("mail.pop3.socketFactory.port", "995");
        properties.put("mail.pop3.port", "995");
        properties.setProperty("mail.pop3.socketFactory.fallback", "false");
        if (this.proxy != null) {
            String hostName = proxy.getHostName();
            int port = proxy.getPort();
            String value = port + "";
            properties.put("proxySet", "true");
            properties.put("socksProxyHost", hostName);
            properties.put("socksProxyPort", value);
            properties.put("http.proxyHost", hostName);
            properties.put("http.proxyPort", port + "");
            properties.put("https.proxyHost", hostName);
            properties.put("https.proxyPort", port + "");
            //#####################################
            properties.setProperty("proxySet", "true");
            properties.setProperty("socksProxyHost", hostName);
            properties.setProperty("socksProxyPort", port + "");
            properties.setProperty("http.proxyHost", hostName);
            properties.setProperty("http.proxyPort", port + "");
            properties.setProperty("https.proxyHost", hostName);
            properties.setProperty("https.proxyPort", port + "");
        }
        Authenticator authenticator = new Authenticator() {

            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                return new PasswordAuthentication(hostEmailAddr, password);
            }
        };
        Session emailSession = Session.getInstance(properties, authenticator);
        //			Session emailSession = Session.getDefaultInstance(properties);
        emailSession.setDebug(debug);
        //2) create the POP3 store object and connect with the pop server  
        //			POP3Store emailStore = (POP3Store) emailSession.getStore(type);
        URLName url = new URLName(type, popHost, 995, "", hostEmailAddr, password);
        POP3SSLStore emailStore = new POP3SSLStore(emailSession, url);
        System.out.println(emailStore.isSSL());
        emailStore.connect();
        System.out.println("connected.");
        //3) create the folder object and open it  
        Folder emailFolder = emailStore.getFolder("INBOX");
        emailFolder.open(Folder.READ_ONLY);
        //4) retrieve the messages from the folder in an array and print it  
        Message[] messages = emailFolder.getMessages();
        for (int i = 0; i < messages.length; i++) {
            Message message = messages[i];
            System.out.println("---------------------------------");
            System.out.println("Email Number " + (i + 1));
            System.out.println("Subject: " + message.getSubject());
            System.out.println("From: " + message.getFrom()[0]);
            System.out.println("Text: " + message.getContent().toString());
        }
        //5) close the store and folder objects  
        emailFolder.close(false);
        emailStore.close();
    } catch (NoSuchProviderException e) {
        e.printStackTrace();
    } catch (MessagingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Also used : Message(javax.mail.Message) MessagingException(javax.mail.MessagingException) URLName(javax.mail.URLName) IOException(java.io.IOException) Properties(java.util.Properties) Folder(javax.mail.Folder) POP3SSLStore(com.sun.mail.pop3.POP3SSLStore) NoSuchProviderException(javax.mail.NoSuchProviderException) Authenticator(javax.mail.Authenticator) PasswordAuthentication(javax.mail.PasswordAuthentication) Session(javax.mail.Session)

Aggregations

POP3SSLStore (com.sun.mail.pop3.POP3SSLStore)1 IOException (java.io.IOException)1 Properties (java.util.Properties)1 Authenticator (javax.mail.Authenticator)1 Folder (javax.mail.Folder)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1 NoSuchProviderException (javax.mail.NoSuchProviderException)1 PasswordAuthentication (javax.mail.PasswordAuthentication)1 Session (javax.mail.Session)1 URLName (javax.mail.URLName)1