Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Code adapted from http://www.javaworld.com/javaworld/javatips/jw-javatip36-p3.html
- //http://pages.prodigy.net/michael_santovec/pop3telnet.htm
- //and Craig Morrall <morrall at comp.lancs.ac.uk>
- String mailServer = "post.gold.ac.uk";
- String userName = "username";
- String password = "password";
- //String subject = "This message is coming from Processing";
- //String messageHeader = "Hello";
- //String fileName = "message.txt";
- //String[] message;
- //strange java thing to get a correct carriage return
- String carriageReturn = System.getProperty("line.separator");
- //temp file for attachments
- byte[] attachmentBytes;
- // now write the bytes to a file
- //saveBytes("numbers.dat", nums);
- void setup()
- {
- readMessage();
- }
- void readMessage()
- {
- println("Trying...");
- try {
- //connect to the mail server
- Socket socket = new Socket(mailServer, 110);
- //create an in and out connection
- DataOutputStream out = new DataOutputStream(socket.getOutputStream());
- DataInputStream in = new DataInputStream(socket.getInputStream());
- //ask your machine what it's really called
- String hostname = InetAddress.getLocalHost().getHostName();
- println("HOST: "+hostname);
- //converse with the mail server
- readIn(in, "OK");
- //tell the mail server who you are
- sendCommand(out, in, "USER "+ userName + carriageReturn);
- readIn(in, "OK");
- //tell the mail server what your password is
- sendCommand(out, in, "PASS "+ password + carriageReturn);
- readIn(in, "OK");
- //ask for a list of all the emails on the mail server
- sendCommand(out, in, "LIST" + carriageReturn);
- String[] messageListHeader = readIn(in, "OK");
- int messageNumber = int(messageListHeader[0].substring(4,(messageListHeader[0].indexOf("messages")-1)));
- println("There are " + messageNumber + " messages");
- readIn(in, ".");
- //ask for the email headers
- //for (int incrementMessageNumber=0; incrementMessageNumber<messageNumber; incrementMessageNumber++)
- for (int incrementMessageNumber=messageNumber-2; incrementMessageNumber<messageNumber; incrementMessageNumber++)
- {
- sendCommand(out, in, "TOP " + incrementMessageNumber + " 0" + carriageReturn);
- //readIn(in, "OK");
- readIn(in, ".");
- }
- sendCommand(out, in, "RETR " + messageNumber + carriageReturn);
- //readIn(in, "OK");
- processMessageSource(readIn(in, "."));
- sendCommand(out, in, "RETR " + (messageNumber-1) + carriageReturn);
- //sendCommand(out, in, "RETR " + 156 + carriageReturn);
- //readIn(in, "OK");
- readIn(in, ".");
- //end the conversation
- sendCommand(out, in, "QUIT" + carriageReturn);
- readIn(in, "OK");
- //close the connections
- in.close();
- out.close();
- //println("Message '" + subject + "' sent to '" + reciever + "'");
- }//end of try statement
- catch (UnknownHostException e)
- {
- println("Unknown Host Exception: " + e);
- }//end of catch statement
- catch(IOException e)
- {
- println("Send failure: " + e);
- }//end of catch statement
- }//end of method readMessage
- void processMessageSource(String[] messageSourceHolder)
- {
- //Content-Type: multipart/mixed; boundary="----=_NextPart_000_00CE_01C66006.5EACC8D0"
- //Message line 16 from mail server: Content-Type: multipart/alternative;
- //Message line 17 from mail server: boundary="----=_NextPart_000_0045_01C660D4.0341E060"
- //Content-Type: text/plain; format=flowed; charset=iso-8859-1; reply-type=original
- //Content-Transfer-Encoding: 7bit
- //Content-Type: application/octet-stream; name=BT_Word_Wrap_14.pde
- //Message line 204 from mail server: Content-Type: application/octet-stream; name=BT_Word_Wrap_14.pde
- //Message line 205 from mail server: Content-Transfer-Encoding: quoted-printable
- //Message line 206 from mail server: Content-Disposition: attachment; filename="BT_Word_Wrap_14.pde"
- //Message line 207 from mail server:
- //Message line 375 from mail server: Content-Type: application/octet-stream; name=Dot-Matrix-16.mvlw
- //Message line 376 from mail server: Content-Transfer-Encoding: base64
- //Message line 377 from mail server: Content-Disposition: attachment; filename="Dot-Matrix-16.mvlw"
- //Message line 378 from mail server:
- //Message line 2524 from mail server: Content-Type: image/jpeg; name=s&p2low.jpg
- //Message line 2525 from mail server: Content-Transfer-Encoding: base64
- //Message line 2526 from mail server: Content-Disposition: attachment; filename="s&p2low.jpg"
- //Message line 2527 from mail server:
- }
- void sendCommand(DataOutputStream out, DataInputStream in, String commandHolder) throws IOException
- {
- if(commandHolder != null)
- {
- out.writeBytes(commandHolder);
- println("");
- println("Command: "+ commandHolder);
- }//end of if statement
- }//end of method sendCommand
- String[] readIn(DataInputStream in, String breakHolder) throws IOException
- {
- String record = null;
- String[] returnRecord = new String[1];
- String[] newRecord = new String[1];
- int lineCount = 0;
- try
- {
- while((record = in.readLine()) != null)
- {
- //add the line to the record array
- newRecord[0] = record;
- if ( record.equals("Content-Transfer-Encoding: base64") )
- {
- // bytesIn = true;
- }
- else
- {//must be text based
- if (lineCount == 0)
- {
- returnRecord[0] = newRecord[0];
- println("Message line " + lineCount + " from mail server: "+returnRecord[0]);
- lineCount++;
- }
- else
- {
- returnRecord = concat(returnRecord, newRecord);
- println("Message line " + lineCount + " from mail server: "+returnRecord[lineCount]);
- lineCount++;
- }
- }//end of must be text based
- //if the read line contains the break character then stop
- if (record.indexOf(breakHolder) != -1)
- {
- if (breakHolder.equals("."))
- {
- if ( record.length() == 1)
- {
- println("Terminated with a single stop break");
- break;
- }//end of if statement
- }//end of if statement
- else
- {
- println("Terminated with a " + breakHolder + " break");
- break;
- }//end of else statement
- }//end of if statement
- }//end of while statement
- //return the record array
- return returnRecord;
- }//end of try statement
- catch (IOException e)
- {
- println("Read failure "+e);
- }//end of catch statement
- //return the record array
- return returnRecord;
- }//end of method readIn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement