Advertisement
peony3000

Sending eMail with processing

Apr 2nd, 2012
434
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 6.57 KB | None | 0 0
  1. //Code adapted from http://www.javaworld.com/javaworld/javatips/jw-javatip36-p3.html
  2. //http://pages.prodigy.net/michael_santovec/pop3telnet.htm
  3. //and Craig Morrall <morrall at comp.lancs.ac.uk>
  4.  
  5. String mailServer = "post.gold.ac.uk";
  6.  
  7. String userName = "username";
  8. String password = "password";
  9. //String subject = "This message is coming from Processing";
  10. //String messageHeader = "Hello";
  11. //String fileName = "message.txt";
  12. //String[] message;
  13.  
  14. //strange java thing to get a correct carriage return
  15. String  carriageReturn = System.getProperty("line.separator");
  16.  
  17. //temp file for attachments
  18. byte[] attachmentBytes;
  19.  
  20. // now write the bytes to a file
  21. //saveBytes("numbers.dat", nums);
  22.  
  23. void setup()
  24. {
  25.   readMessage();
  26. }  
  27.  
  28. void readMessage()
  29. {
  30.   println("Trying...");
  31.   try {
  32.  
  33.     //connect to the mail server
  34.     Socket socket = new Socket(mailServer, 110);
  35.  
  36.     //create an in and out connection
  37.     DataOutputStream out = new DataOutputStream(socket.getOutputStream());
  38.     DataInputStream in = new DataInputStream(socket.getInputStream());
  39.  
  40.     //ask your machine what it's really called
  41.     String hostname = InetAddress.getLocalHost().getHostName();
  42.     println("HOST: "+hostname);
  43.  
  44.     //converse with the mail server
  45.     readIn(in, "OK");
  46.  
  47.     //tell the mail server who you are
  48.     sendCommand(out, in, "USER "+ userName + carriageReturn);
  49.     readIn(in, "OK");
  50.     //tell the mail server what your password is
  51.     sendCommand(out, in, "PASS "+ password + carriageReturn);
  52.     readIn(in, "OK");
  53.  
  54.     //ask for a list of all the emails on the mail server
  55.     sendCommand(out, in, "LIST" +  carriageReturn);
  56.     String[] messageListHeader = readIn(in, "OK");
  57.     int messageNumber = int(messageListHeader[0].substring(4,(messageListHeader[0].indexOf("messages")-1)));
  58.     println("There are " + messageNumber + " messages");
  59.     readIn(in, ".");
  60.  
  61.     //ask for the email headers
  62.     //for (int incrementMessageNumber=0; incrementMessageNumber<messageNumber; incrementMessageNumber++)
  63.     for (int incrementMessageNumber=messageNumber-2; incrementMessageNumber<messageNumber; incrementMessageNumber++)
  64.     {
  65.       sendCommand(out, in, "TOP " + incrementMessageNumber + " 0" +  carriageReturn);
  66.       //readIn(in, "OK");
  67.       readIn(in, ".");
  68.  
  69.     }
  70.  
  71.     sendCommand(out, in, "RETR " + messageNumber +  carriageReturn);
  72.     //readIn(in, "OK");
  73.     processMessageSource(readIn(in, "."));
  74.  
  75.     sendCommand(out, in, "RETR " + (messageNumber-1) +  carriageReturn);
  76.     //sendCommand(out, in, "RETR " + 156 +  carriageReturn);
  77.     //readIn(in, "OK");
  78.     readIn(in, ".");
  79.  
  80.  
  81.     //end the conversation
  82.     sendCommand(out, in, "QUIT" + carriageReturn);
  83.     readIn(in, "OK");
  84.  
  85.     //close the connections
  86.     in.close();
  87.     out.close();
  88.  
  89.     //println("Message '" + subject + "' sent to '" + reciever + "'");
  90.  
  91.   }//end of try statement
  92.   catch (UnknownHostException e)
  93.   {
  94.     println("Unknown Host Exception: " + e);
  95.   }//end of catch statement
  96.   catch(IOException e)
  97.   {
  98.     println("Send failure: " + e);
  99.   }//end of catch statement
  100.  
  101. }//end of method readMessage
  102.  
  103. void processMessageSource(String[] messageSourceHolder)
  104. {
  105.  
  106.   //Content-Type: multipart/mixed; boundary="----=_NextPart_000_00CE_01C66006.5EACC8D0"
  107.  
  108. //Message line 16 from mail server: Content-Type: multipart/alternative;
  109. //Message line 17 from mail server:     boundary="----=_NextPart_000_0045_01C660D4.0341E060"
  110.  
  111.  
  112.   //Content-Type: text/plain; format=flowed; charset=iso-8859-1; reply-type=original
  113. //Content-Transfer-Encoding: 7bit
  114.  
  115.  
  116. //Content-Type: application/octet-stream; name=BT_Word_Wrap_14.pde
  117.  
  118.  
  119. //Message line 204 from mail server: Content-Type: application/octet-stream; name=BT_Word_Wrap_14.pde
  120. //Message line 205 from mail server: Content-Transfer-Encoding: quoted-printable
  121. //Message line 206 from mail server: Content-Disposition: attachment; filename="BT_Word_Wrap_14.pde"
  122. //Message line 207 from mail server:
  123.  
  124. //Message line 375 from mail server: Content-Type: application/octet-stream; name=Dot-Matrix-16.mvlw
  125. //Message line 376 from mail server: Content-Transfer-Encoding: base64
  126. //Message line 377 from mail server: Content-Disposition: attachment; filename="Dot-Matrix-16.mvlw"
  127. //Message line 378 from mail server:
  128.  
  129. //Message line 2524 from mail server: Content-Type: image/jpeg; name=s&p2low.jpg
  130. //Message line 2525 from mail server: Content-Transfer-Encoding: base64
  131. //Message line 2526 from mail server: Content-Disposition: attachment; filename="s&p2low.jpg"
  132. //Message line 2527 from mail server:
  133.  
  134.  
  135. }
  136.  
  137. void sendCommand(DataOutputStream out, DataInputStream in, String commandHolder) throws IOException
  138. {
  139.  
  140.   if(commandHolder != null)
  141.   {
  142.     out.writeBytes(commandHolder);
  143.     println("");
  144.     println("Command: "+ commandHolder);
  145.   }//end of if statement
  146.  
  147. }//end of method sendCommand
  148.  
  149. String[] readIn(DataInputStream in, String breakHolder) throws IOException
  150. {
  151.   String record = null;
  152.   String[] returnRecord = new String[1];
  153.   String[] newRecord = new String[1];
  154.   int lineCount = 0;
  155.  
  156.   try
  157.   {
  158.     while((record = in.readLine()) != null)
  159.     {
  160.  
  161.       //add the line to the record array
  162.       newRecord[0] = record;
  163.  
  164.       if ( record.equals("Content-Transfer-Encoding: base64") )
  165.       {
  166.    
  167.     //  bytesIn = true;
  168.        
  169.       }
  170.       else
  171.       {//must be text based
  172.  
  173.       if (lineCount == 0)
  174.       {
  175.         returnRecord[0] = newRecord[0];
  176.         println("Message line " + lineCount + " from mail server: "+returnRecord[0]);
  177.         lineCount++;
  178.       }
  179.       else
  180.       {
  181.         returnRecord = concat(returnRecord, newRecord);
  182.         println("Message line " + lineCount + " from mail server: "+returnRecord[lineCount]);
  183.         lineCount++;
  184.       }
  185.  
  186.  
  187.       }//end of must be text based
  188.  
  189.       //if the read line contains the break character then stop
  190.       if (record.indexOf(breakHolder) != -1)
  191.       {
  192.         if (breakHolder.equals("."))
  193.         {
  194.           if ( record.length() == 1)
  195.           {
  196.             println("Terminated with a single stop break");
  197.  
  198.             break;
  199.           }//end of if statement
  200.         }//end of if statement
  201.         else
  202.         {
  203.           println("Terminated with a " + breakHolder + " break");
  204.           break;
  205.         }//end of else statement
  206.       }//end of if statement
  207.     }//end of while statement
  208.  
  209.     //return the record array
  210.     return returnRecord;
  211.  
  212.   }//end of try statement
  213.   catch (IOException e)
  214.   {
  215.     println("Read failure "+e);
  216.   }//end of catch statement
  217.  
  218.   //return the record array
  219.   return returnRecord;
  220.  
  221. }//end of method readIn
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement