Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.net.*;
- public class POP3 {
- private final static String DEF_POP = "ziggo.nl";
- private final static String DEF_USER = "jorrit.de.boer";
- private final static String DEF_PASS = "netwerken12345";
- private final static int DEF_PORT = 110;
- private String[] actions = {"STAT", "LIST", "USER", "PASS", "DELE", "RETR", "QUIT"};
- private String pop;
- private Socket sock;
- private int port;
- private BufferedReader rdr;
- private BufferedReader socketRdr;
- private BufferedWriter socketWrtr;
- public POP3(){
- rdr = new BufferedReader(new InputStreamReader(System.in));
- sock = new Socket();
- }
- public boolean connect(){
- String servMes;
- try{
- sock.connect(new InetSocketAddress(pop, port));
- socketRdr = new BufferedReader(new InputStreamReader(sock.getInputStream()));
- socketWrtr = new BufferedWriter(new OutputStreamWriter(sock.getOutputStream()));
- servMes = readResponse();
- if(servMes.contains("-ERR")){
- System.out.println("An error has occurred.");
- return false;
- }
- socketWrtr.write("USER " + actions[2] +"\n");
- socketWrtr.flush();
- servMes = readResponse();
- if(servMes.contains("-ERR")){
- System.out.println("Invalid username.");
- return false;
- }
- socketWrtr.write("PASS " + actions[3] +"\n");
- socketWrtr.flush();
- servMes = readResponse();
- if(servMes.contains("-ERR")){
- System.out.println("Invalid password.");
- return false;
- }
- return true;
- } catch(IOException e) {
- System.out.println("Could not establish a connection with these settings: " + pop + ":" + port + "\n");
- }
- return false;
- }
- public boolean isConnected() {
- return sock != null && sock.isConnected();
- }
- public void setPop() throws IOException {
- System.out.println("Enter pop address.");
- System.out.println("If unspecified, [pop." + DEF_POP + "] will be used.");
- pop = input();
- if(pop.isEmpty()){
- pop = DEF_POP;
- }
- System.out.println("Enter port address.");
- System.out.println("If unspecified, [" + DEF_PORT + "] will be used.");
- String portStr = input();
- if(portStr.isEmpty()){
- port = DEF_PORT;
- } else {
- port = Integer.parseInt(portStr);
- }
- System.out.println("Enter username.");
- System.out.println("If unspecified, [" + DEF_USER + "] will be used.");
- actions[2] = input();
- if(actions[2].isEmpty()){
- actions[2] = DEF_USER;
- }
- System.out.println("Enter password.");
- System.out.println("If unspecified, [" + DEF_PASS + "] will be used.");
- actions[3] = input();
- if(actions[3].isEmpty()){
- actions[3] = "mail";
- }
- }
- public String input() throws IOException{
- String str = new String();
- str = rdr.readLine();
- return str;
- }
- public boolean execute(String inputStr) {
- String[] tmpStr = inputStr.split("@");
- int option = 6;
- if(!inputStr.isEmpty()){
- option = Integer.parseInt(tmpStr[0]);
- }
- switch (option) {
- case 1:
- return stat();
- case 2:
- return list();
- case 3:
- return inputStr.contains("@") ? retrieve(tmpStr[1]) : false;
- case 4:
- return inputStr.contains("@") ? delete(tmpStr[1]) : false;
- case 5:
- return quit();
- default:
- return false;
- }
- }
- private boolean stat() {
- try {
- socketWrtr.write("STAT"+"\n");
- socketWrtr.flush();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- private boolean list() {
- try {
- socketWrtr.write("LIST"+"\n");
- socketWrtr.flush();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- private boolean retrieve(String str) {
- str = str.trim();
- try {
- socketWrtr.write("RETR " + str + "\n");
- socketWrtr.flush();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- private boolean delete(String str) {
- try {
- socketWrtr.write("DELE " + str + "\n");
- socketWrtr.flush();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- public String readResponse() {
- String str = "";
- if(!sock.isClosed()){
- try {
- str = socketRdr.readLine() + "\n";
- while(socketRdr.ready()){
- str += socketRdr.readLine() + "\n";
- }
- System.out.println("Server response: " + str);
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- return str;
- }
- public boolean quit() {
- try {
- socketWrtr.write("QUIT"+"\n");
- socketWrtr.flush();
- } catch (IOException e) {
- e.printStackTrace();
- return false;
- }
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement