| package com.lotontech.mail; import javax.mail.*; import javax.mail.internet.*; import java.util.*; import java.io.*; /** * A simple email receiver class. */ public class SimpleReceiver { /** * Main method to receive messages from the mail server specified * as command line arguments. */ public static void main(String args[]) { try { String popServer=args[0]; String popUser=args[1]; String popPassword=args[2]; receive(popServer, popUser, popPassword); } catch (Exception ex) { System.out.println("Usage: java com.lotontech.mail.SimpleReceiver" +" popServer popUser popPassword"); } System.exit(0); } |
| java com.lotontech.mail.SimpleReceiver pop.myIsp.net myUserName myPassword |
| /** * "receive" method to fetch messages and process them. */ public static void receive(String popServer, String popUser , String popPassword) { Store store=null; Folder folder=null; try { // -- Get hold of the default session -- Properties props = System.getProperties(); Session session = Session.getDefaultInstance(props, null); // -- Get hold of a POP3 message store, and connect to it -- store = session.getStore("pop3"); store.connect(popServer, popUser, popPassword); // -- Try to get hold of the default folder -- folder = store.getDefaultFolder(); if (folder == null) throw new Exception("No default folder"); // -- ...and its INBOX -- folder = folder.getFolder("INBOX"); if (folder == null) throw new Exception("No POP3 INBOX"); // -- Open the folder for read only -- folder.open(Folder.READ_ONLY); // -- Get the message wrappers and process them -- Message[] msgs = folder.getMessages(); for (int msgNum = 0; msgNum < msgs.length; msgNum++) { printMessage(msgs[msgNum]); } } catch (Exception ex) { ex.printStackTrace(); } finally { // -- Close down nicely -- try { if (folder!=null) folder.close(false); if (store!=null) store.close(); } catch (Exception ex2) {ex2.printStackTrace();} } } |
请注意:你从session中得到一个POP3消息存储封装器,然后使用最初在命令行上键入的mail设置跟它连接。
一旦连接成功,你就得到了一个默认文件夹的句柄,在这里使用的是INBOX文件夹,它保存了进来的消息。你可以打开这个只读的INBOX信箱,然后一个一个的读取消息。
另外,你可能想知道是否你能够以写的方式打开这个INBOX信箱。如果你想为这些消息做标记或者从服务器上删除,你可以做得到。不过在我们的这个例子中,你只能查看消息。
最后,在上面的代码中,你做到了当查看完毕后关闭文件夹以及消息存储,然后留下printMessage()方法来完成这个类的剩余部分。
打印消息
在这一部分,很有必要讨论前面提到的javax.mail.Part接口。
下面的代码让你明白怎样隐含地把消息转换为它的Part接口并且把它赋给messagePart变量。对于只有一部分的消息,你现在需要打印一些信息。
假如调用messagePart.getContent()来生成一个Multipart实例,你知道你正在处理一个多部分消息;在这种情况下,你正在通过getBodyPart(0)来得到第一个多部分消息并且打印它。
当然你还要知道是否你已经得到了这个消息本身,还是仅仅是消息正文的第一部份。只有当内容是普通文本或者HTML时,你才可以打印该消息,这是通过一个InputStream来完成的。
| /** * "printMessage()" method to print a message. */ public static void printMessage(Message message) { try { // Get the header information String from=((InternetAddress)message.getFrom()[0]).getPersonal(); if (from==null) from=((InternetAddress)message.getFrom()[0]) .getAddress(); System.out.println("FROM: "+from); String subject=message.getSubject(); System.out.println("SUBJECT: "+subject); // -- Get the message part (i.e. the message itself) -- Part messagePart=message; Object content=messagePart.getContent(); // -- or its first body part if it is a multipart message -- if (content instanceof Multipart) { messagePart=((Multipart)content).getBodyPart(0); System.out.println("[ Multipart Message ]"); } // -- Get the content type -- String contentType=messagePart.getContentType(); // -- If the content is plain text, we can print it -- System.out.println("CONTENT:"+contentType); if (contentType.startsWith("text/plain")|| contentType.startsWith("text/html")) { InputStream is = messagePart.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(is)); String thisLine=reader.readLine(); while (thisLine!=null) { System.out.println(thisLine); thisLine=reader.readLine(); } } System.out.println("-----------------------------"); } catch (Exception ex) { ex.printStackTrace(); } } } |
| FROM: Tony Loton SUBJECT: Number 1 [ Multipart Message ] CONTENT:text/plain; charset="iso-8859-1" Attachment 1 from Tony Loton. ----------------------------- |
| set CLASSPATH=%CLASSPATH%;lotontalk.jar |
| // -- Get the message wrappers and process them -- Message[] msgs = folder.getMessages(); for (int msgNum = 0; msgNum < msgs.length; msgNum++) { printMessage(msgs[msgNum]); } |
| // -- Get the message wrappers and process them -- Message[] msgs = folder.getMessages(); for (int msgNum = 0; msgNum < msgs.length; msgNum++) { printMessage(msgs[msgNum]); speakMessage(msgs[msgNum]); } |
| /** * "speakMessage", a talking version of printMessage(). */ public static void speakMessage(Message message) { String speech=""; try { com.lotontech.talk.LOTONtalk speaker=new com.lotontech.talk.LOTONtalk(); String from=((InternetAddress)message.getFrom()[0]).getPersonal(); if (from==null) from=((InternetAddress)message.getFrom()[0]).getAddress(); speech=speech+"from "+from+", "; String subject=message.getSubject(); speech=speech+"subject "+subject+", "; // -- Get the message part (i.e., the message itself) -- Part messagePart=message; Object content=messagePart.getContent(); // -- ...or its first body part if it is a multipart message -- if (content instanceof Multipart) messagePart=((Multipart)content).getBodyPart(0); String contentType=messagePart.getContentType(); if (contentType.startsWith("text/plain")|| contentType.startsWith("text/html")) { InputStream is = messagePart.getInputStream(); BufferedReader reader=new BufferedReader(new InputStreamReader(is)); String thisLine=reader.readLine(); while (thisLine!=null) { speech=speech+thisLine+". "; thisLine=reader.readLine(); } // -- SPEAK -- speaker.speak(speech,true); } } catch (Exception ex) { ex.printStackTrace(); } } |
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/374079/viewspace-132216/ ,如需转载,请注明出处,否则将追究法律责任。