Welcome to Dream.In.Code
Getting Java Help is Easy!

Join 132,650 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,143 people online right now. Registration is fast and FREE... Join Now!




Java mail server code part1

 
Reply to this topicStart new topic

Java mail server code part1, just check weather code working or not

prajayshetty
post 5 Aug, 2008 - 11:14 PM
Post #1


D.I.C Head

Group Icon
Joined: 27 Apr, 2007
Posts: 228



Dream Kudos: 25
My Contributions


guys i am getting connection timed out error plzz u guys checkit out weather getting list just put the username ,password. Host name as pop.gmail.com
CODE

import java.awt.event.ActionEvent;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.*;
import java.awt.*;


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

public class Ranger extends JFrame {

    exit aexit;
    ok aok;
    String protocol;
    String host;
    String username, password; //enter host , username and password directly in to string host username
    // and password
    Store store;
    Folder folder;
    Session session;
    Properties props;
    Scanner reader;
    JTextField tusername, tpassword, thost, tprotocol;
    JLabel lusername, lpassword, lhost, lprotocol;
    JButton cok, cexit;
    final int port = 955;

    Ranger() {

        setTitle("MailStorage");
        BufferedReader reader;
        tusername = new JTextField();
        tpassword = new JTextField();
        thost = new JTextField();
        tprotocol = new JTextField();
        lusername = new JLabel("EMAIL-ID");
        lpassword = new JLabel("PASSWORD");
        lhost = new JLabel("HOST");
        lprotocol = new JLabel("EMAIL-ID");
        cok = new JButton("OK");
        cexit = new JButton("EXIT");
        Container pane = getContentPane();
        pane.setLayout(new GridLayout(9, 9));
        pane.add(lusername);
        pane.add(tusername);
        pane.add(lpassword);
        pane.add(tpassword);
        pane.add(lhost);
        pane.add(thost);
        pane.add(lprotocol);
        pane.add(tprotocol);
        pane.add(cok);
        aok = new ok();

        cok.addActionListener(aok);



        pane.add(cexit);
        aexit = new exit();
        cexit.addActionListener(aexit);

        setSize(700, 700);
        setVisible(true);



    }

    class exit implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            System.exit(0);
        }
    }

    class ok implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            try {
                initialize();
            } catch (Exception ex) {
                Logger.getLogger(Ranger.class.getName()).log(Level.SEVERE, null, ex);
            }
            System.out.println("Reached");


        }
    }

    void initialize() {

        props = new Properties();


        session = Session.getDefaultInstance(props, null);
        try {


            store = session.getStore("pop3");
        } catch (NoSuchProviderException ex) {
            Logger.getLogger(Ranger.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {


            store.connect(host, username, password);
        } catch (MessagingException ex) {
            Logger.getLogger(Ranger.class.getName()).log(Level.SEVERE, null, ex);
        }




        folder = store.getFolder("INBOX");





        folder.open(Folder.READ_ONLY);
        BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));

        Message message[] = folder.getMessages();

        for (int i = 0, n = message.length; i < n; i++) {

            System.out.println(i + ": " + message[i].getFrom()[0] + "\t" + message[i].getSubject());



        }




        // Close connection
        folder.close(false);


        store.close();

        System.out.println("End");


    }

    public static void main(String args[]) {

        Ranger m = new Ranger();



    }
}

and hit ok and dont enter anthing in the textboxes since gui is not yet fully implemented

This post has been edited by prajayshetty: 5 Aug, 2008 - 11:16 PM
User is offlineProfile CardPM

Go to the top of the page

pbl
post 6 Aug, 2008 - 04:29 PM
Post #2


D.I.C Lover

Group Icon
Joined: 6 Mar, 2008
Posts: 2,982



Thanked 190 times

Dream Kudos: 75
My Contributions


you should try with another service provider than gmail

or try my version with gmail... it works well with Sympatico

java

import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;

/*
* This Package emplements a PopMailServer that retreives and stores messages from Pop server(s)
* The reason I wrote this program is that I use Sympatico as mail agent
* My laptop is connected to the Internet through Cogeco
* So in Outlook on my laptop my:
* - POP server is Sympatico
* - SMTP server is Cogeco
* When the connection from Cogeco to Sympatico times out... Outlook displays an error message and stays
* locked on it.
* This server is built to be robust. It fetches the mail from Sympatico and stores them on the local file system
* (it does not hang when the connection to Sympatico times out)
* This program is now the POP server to my Outlook to which it relays the mail fetched on the real Sympatico POP
* Server
*/
public class PopMailServer {

String host = "pop1.sympatico.ca";
String username = "***********************"; // your ISP Username
String password = "************************"; // and pasword here?
Authenticator auth = new PopAuthenticator(username, password);

int port = 955;
Properties props;

PopMailServer() {
props = new Properties();
props.put("mail.pop3.host", host);
Session session = Session.getDefaultInstance(props, auth);

// Then make a connection to the Store with the getStore() method,
// passing the method the appropriate mail protocol for your server
// -- either "pop3" or "imap". Here's how you would make the
// appropriate connection to a POP3 server:

// Get the store

Store store;
try {
store = session.getStore("pop3");
store.connect();
}
catch (Exception e) {
System.out.println("Connect failed: " + e);
return;
}
System.out.println("Connect worked");

// Next, you need to open the Folder with the open() method. You can
// open it in either Folder.READ_WRITE mode or Folder.READ_ONLY
// mode. To read messages, you can use read-only, although if you
// want to delete messages, you need to use READ_WRITE.

Folder folder;
try {
folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
}
catch (Exception e) {
System.out.println("Open failed: " + e);
return;
}
System.out.println("Open worked");

// Connecting to the Folder gives you access to the individual
// Message objects. You can use the getMessage() method to get an
// individual message, or as in the following example, use the
// getMessages() method to get all the messages:

// Get directory
Message[] message;
try {
message = folder.getMessages();
}
catch(Exception e) {
System.out.println("getMessages failed: " + e);
return;
}
System.out.println("getMessage() worked " + message.length + " messages");
for(int i = 0; i < message.length; i++) {
Address[] addr = null;
String subject = null;
int size = 0;
Date date = null;
try {
addr = message[i].getFrom();
subject = message[i].getSubject();
size = message[i].getSize();
date = message[i].getSentDate();
}
catch(Exception e) {
System.out.println("Exception getInfo: " + e);
}
System.out.println(" " + (i+1) + ") " + addr[0] + "\t" + subject + "\t" + date.toString() + "\tSize:" + size);
}


}

public static void main(String[] arg) {
PopMailServer pms = new PopMailServer();
}

}


and

java

import javax.mail.*;

public class PopAuthenticator extends Authenticator {

String username, password;
PopAuthenticator(String username, String password) {
this.username = username;
this.password = password;
}

public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
}


This post has been edited by pbl: 6 Aug, 2008 - 04:37 PM
User is offlineProfile CardPM

Go to the top of the page

lordms12
post 7 Aug, 2008 - 07:19 AM
Post #3


D.I.C Regular

Group Icon
Joined: 16 Feb, 2008
Posts: 312



Thanked 13 times

Dream Kudos: 225
My Contributions


I found it hard to continue on your code so I wrote mine and tested it but you have to get sure that you enabling pop on your gmail account setting.
java
import javax.mail.*;

public class Ranger{

public static void main(String args[]) {
try {
String messages[] = Ranger.getMailViaPOP();
for(int i = 0; i < messages.length; i++){
System.out.println(messages[i]);
}
} catch (MessagingException e) {
e.printStackTrace();
}
}

public static String [] getMailViaPOP() throws MessagingException {
java.util.Properties props = new java.util.Properties();
props.setProperty("mail.pop3.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.pop3.socketFactory.fallback", "false");
props.setProperty("mail.pop3.socketFactory.port", "995");

Session session = Session.getDefaultInstance(props);

Store store = session.getStore("pop3");
store.connect("pop.gmail.com", "user", "pass");

Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);

Message[] message = folder.getMessages();
String [] titles = new String[message.length];
for (int i = 0, n = message.length; i < n; i++) {
titles[i] = message[i].getSubject();
}
// Close connection
folder.close(false);
store.close();
return titles;
}
}


This post has been edited by lordms12: 7 Aug, 2008 - 07:39 AM
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 11/23/08 05:09AM

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month