Welcome to Dream.In.Code
Become a Java Expert!

Join 150,140 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,298 people online right now. Registration is fast and FREE... Join Now!




Java mail server

 
Reply to this topicStart new topic

Java mail server, connection timed out

prajayshetty
19 Jul, 2008 - 12:02 AM
Post #1

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
i am trying to retriving subjects from gmail getting connection timed out when i reached to the line "store.connect" i had changed settings of mine gmail account to allow pop clients in retriving gmail account
CODE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/* aurthor prajay shetty & dic members
* devolopment  version 0.1
*website= www.dreamincode.net
*/
package javamailstore;

import javax.mail.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Properties;

public class Main extends JFrame {

    Aok okss;
    Store store;
    JLabel password, username, localdir, folders, attachments, dname, lport;
    JButton bok, bexit;
    JTextField tpassword, tusername, tlocaldir, tfolders, tattachments, tdname, tport;
    Aexit acexit;
    String spassword, susername, slocaldir, sfolders, sattachments, sdname, sport;
    String serverfolder = "INBOX";
    Folder dfolder;
    Properties props;
    AccountAuthentication auth;

    Main() {
        setTitle("java mail");
        password = new JLabel("password");
        username = new JLabel("username");
        localdir = new JLabel("localdir");
        folders = new JLabel("folder");
        attachments = new JLabel("Attachments");
        tpassword = new JTextField();
        tusername = new JTextField();
        tlocaldir = new JTextField();
        tfolders = new JTextField();
        tattachments = new JTextField();
        bok = new JButton("ok");
        bexit = new JButton("exits");
        dname = new JLabel("domain-name");
        tdname = new JTextField("tdname");
        lport = new JLabel("Server Port");
        tport = new JTextField();
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(8, 8));
        //add compnets container

        pane.add(dname);
        pane.add(tdname);
        pane.add(username);
        pane.add(tusername);
        pane.add(password);
        pane.add(tpassword);
        pane.add(localdir);
        pane.add(tlocaldir);
        pane.add(folders);
        pane.add(tfolders);
        pane.add(attachments);
        pane.add(tattachments);
        pane.add(lport);
        pane.add(tport);

        pane.add(bok);

        acexit = new Aexit();
        okss = new Aok();
        pane.add(bexit);
        bexit.addActionListener(acexit);
        bok.addActionListener(okss);
        setVisible(true);


    }
    //action listners for exit button
    class Aexit implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            //exit with ok status
            System.out.println("exited");
            System.exit(0);

        }
    }
    //including actionlistener
    class Aok implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            spassword = tpassword.getText();
            susername = tusername.getText();
            slocaldir = tlocaldir.getText();
            sfolders = tfolders.getText();
            sdname = tdname.getText();
            sattachments = tattachments.getText();
            sport = tport.getText();

            initialize();
            System.out.println("initialization of properties is complete");

            mailreader();

        }
    }

    public class AccountAuthentication extends javax.mail.Authenticator {

        @Override
        public PasswordAuthentication getPasswordAuthentication() {

            return new PasswordAuthentication(susername, spassword);
        }
    }

    public void initialize() //copying message to a folder
    {
        //initializing properties
        props = new Properties();
        props.put("mail.pop3.host", dname);
        props.put("mail.pop3.user", susername);
        props.put("mail.pop3.port", sport);
        props.put("mail.pop3.starttls.enable", "true");
        props.put("mail.pop3.auth", "true");
        props.put("mail.pop3.socketFactory.port", sport);
        props.put("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.pop3.socketFactory.fallback", "false");


    }

    void mailreader() {
        System.out.println("authenthicating");
        auth = new AccountAuthentication();
        Session session = Session.getDefaultInstance(props, auth);
        System.out.println("authenctication complete got instance");

        try {

            //get session for the code from the server

            store = session.getStore("pop3");
            System.out.println("connecting to store");
            try {

                store.connect(sdname, susername, spassword);
                System.out.println("authentication with user , password and dname complete");
            } catch (MessagingException ex) {
                ex.printStackTrace();
            }

            try {
                if (store.isConnected()) {

                    Folder inboxFolder = store.getFolder("INBOX");
                    inboxFolder.open(Folder.READ_WRITE);
                    System.out.println("opened inbox folder");
                    System.out.println("Message Count:" + inboxFolder.getMessageCount());

                    Message[] msg = inboxFolder.getMessages();

                    int len = msg.length;


                    for (int i = 0; len < len; i++) {

                        System.out.println("subject: " + msg[i].getSubject());

                    }

                }

            } catch (MessagingException ex) {
                ex.printStackTrace();
            }


        } catch (NoSuchProviderException ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String args[]) {
        Main m = new Main();
    }
}


This post has been edited by prajayshetty: 19 Jul, 2008 - 12:06 AM
User is offlineProfile CardPM
+Quote Post

JeroenFM
RE: Java Mail Server
19 Jul, 2008 - 01:06 AM
Post #2

D.I.C Head
Group Icon

Joined: 30 Jun, 2008
Posts: 191



Thanked: 9 times
Dream Kudos: 100
My Contributions
I can't see anything particularly wrong with your code at first glance, so I must ask: what port are you connecting to?
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Java Mail Server
19 Jul, 2008 - 02:14 AM
Post #3

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
QUOTE(JeroenFM @ 19 Jul, 2008 - 02:06 AM) *

I can't see anything particularly wrong with your code at first glance, so I must ask: what port are you connecting to?

955
now it works but now i cant get the total number of messages in mine inbox folder how ever i know i have 20 messages in it

This post has been edited by prajayshetty: 19 Jul, 2008 - 02:33 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Mail Server
19 Jul, 2008 - 06:12 AM
Post #4

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
gmail requires a SSL connection

http://www.dreamincode.net/forums/showtopic56237.htm


User is offlineProfile CardPM
+Quote Post

fsloke
RE: Java Mail Server
19 Jul, 2008 - 09:14 AM
Post #5

D.I.C Regular
***

Joined: 19 Dec, 2007
Posts: 258



Thanked: 4 times
My Contributions
pbl that is my post link smile.gif

Go to JAVA World to read the tutorial....

http://www.javaworld.com/javaworld/jw-06-1...6-javamail.html

not mistake google server is pop.gmail.com
see here
http://www.emailaddressmanager.com/tips/mail-settings.html

smile.gif

Enjoy ya...
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Java Mail Server
19 Jul, 2008 - 10:49 AM
Post #6

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
CODE

/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
/* aurthor dic members
* devolopment  version 0.1
*website= www.dreamincode.net
*/

import com.sun.mail.pop3.POP3SSLStore;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.URLName;
import javax.mail.*;
import java.awt.event.ActionEvent;
import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionListener;
import java.util.Properties;

public class Javamail extends JFrame {

    Session session;
    Aok okss;
    Store store;
    JLabel password, username, localdir, folders, attachments, dname, lport;
    JButton bok, bexit;
    JTextField tpassword, tusername, tlocaldir, tfolders, tattachments, tdname, tport;
    Aexit acexit;
    String spassword, susername, slocaldir, sfolders, sattachments, sdname;
    String serverfolder = "INBOX";
    Folder folder;
    Properties pop3Props;
    URLName url;

    Javamail() {
        setTitle("java mail");
        password = new JLabel("password");
        username = new JLabel("username");
        localdir = new JLabel("localdir");
        folders = new JLabel("folder");
        attachments = new JLabel("Attachments");
        tpassword = new JTextField();
        tusername = new JTextField();
        tlocaldir = new JTextField();
        tfolders = new JTextField();
        tattachments = new JTextField();
        bok = new JButton("ok");
        bexit = new JButton("exits");
        dname = new JLabel("domain-name");
        tdname = new JTextField("tdname");
        lport = new JLabel("Server Port");
        tport = new JTextField();
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(8, 8));
        //add compnets container


        pane.add(dname);
        pane.add(tdname);
        pane.add(username);
        pane.add(tusername);
        pane.add(password);
        pane.add(tpassword);
        pane.add(localdir);
        pane.add(tlocaldir);
        pane.add(folders);
        pane.add(tfolders);
        pane.add(attachments);
        pane.add(tattachments);
        pane.add(lport);
        pane.add(tport);

        pane.add(bok);

        acexit = new Aexit();
        okss = new Aok();
        pane.add(bexit);
        bexit.addActionListener(acexit);
        bok.addActionListener(okss);
        setVisible(true);


    }
    //action listners for exit button
    class Aexit implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            //exit with ok status
            System.out.println("exited");
            System.exit(0);

        }
    }
    //including actionlistener
    class Aok implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            spassword = tpassword.getText();
            susername = tusername.getText();
            slocaldir = tlocaldir.getText();
            sfolders = tfolders.getText();
            sdname = tdname.getText();
            sattachments = tattachments.getText();


            initialize();
            System.out.println("initialization of properties is complete");

            mailreader();

        }
    }
    //copying message to a folder
    public void initialize() {

        String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

        pop3Props = new Properties();

        pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
        pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
        pop3Props.setProperty("mail.pop3.port", "995");
        pop3Props.setProperty("mail.pop3.socketFactory.port", "995");



        url = new URLName("pop3", "pop.gmail.com", 995, "",
                susername, spassword);


    }

    void mailreader() {
        System.out.println("authenthicating");
        session = Session.getInstance(pop3Props, null);
        store = new POP3SSLStore(session, url);
        System.out.println("authenctication complete got instance");



        //get session for the code from the server

        

        try {

            
            store.connect();

            System.out.println("authentication with user , password and dname complete");
        } catch (MessagingException ex) {
            ex.printStackTrace();
        }

        try {
            if (store.isConnected()) {

                folder = store.getFolder("INBOX");
                folder.open(Folder.READ_WRITE);
                System.out.println("opened inbox folder");



                System.out.println("total nuber of messages" + folder.getMessageCount());
                Message[] msg = folder.getMessages();

                int len = msg.length;


                for (int i = 0; len < len; i++) {

                    System.out.println("subject: " + msg[i].getSubject());

                }

            }

        } catch (MessagingException ex) {
            ex.printStackTrace();
        }




    }

    public static void main(String args[]) {
        Javamail m = new Javamail();
    }
}
    


still cant retrtive messages
[EDIT] removed double lines in the code

This post has been edited by prajayshetty: 19 Jul, 2008 - 11:29 AM
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Mail Server
19 Jul, 2008 - 05:36 PM
Post #7

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
Prajay
you will have to add a JRadioButton to your GUI
use POP3 or use POP3SSL biggrin.gif
I am anxious to see that project working
Thanks
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Java Mail Server
19 Jul, 2008 - 08:47 PM
Post #8

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
ya let me figure out about this pop3ssl some how and then some good gui icon_down.gif
User is offlineProfile CardPM
+Quote Post

pbl
RE: Java Mail Server
20 Jul, 2008 - 08:02 PM
Post #9

D.I.C Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 3,587



Thanked: 233 times
Dream Kudos: 75
My Contributions
A French sitation from I do not remeber who

"A vaincre sans peril on triomphe sans gloire"

can be translate to:

who have win without peril triumph without glory

I will add that to my "signature"

This post has been edited by pbl: 20 Jul, 2008 - 08:11 PM
User is offlineProfile CardPM
+Quote Post

prajayshetty
RE: Java Mail Server
5 Aug, 2008 - 03:25 AM
Post #10

D.I.C Head
Group Icon

Joined: 27 Apr, 2007
Posts: 230



Thanked: 1 times
Dream Kudos: 25
My Contributions
weee i had finally got it now i can read mail from the gmail server will post code soon and then move to the second part of the project

This post has been edited by prajayshetty: 5 Aug, 2008 - 03:26 AM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/9/09 02:13AM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month