WEB开发网
开发学院软件开发Java JavaMail快速入门-6 阅读

JavaMail快速入门-6

 2007-12-23 12:25:48 来源:WEB开发网   
核心提示:receive()方法从main()方法中调用,它依次打开你的POP3信箱检查消息,JavaMail快速入门-6,每次都调用PRintMessage(),代码如下:/*** "receive" method to fetch messages and process them.*/public st

  receive()方法从main()方法中调用,它依次打开你的POP3信箱检查消息,每次都调用PRintMessage()。代码如下:

/**

* "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();}

 }

}

(出处:http://www.cncms.com)


Tags:JavaMail 快速 入门

编辑录入:爽爽 [复制链接] [打 印]
赞助商链接