JavaMail快速入门2

来源:互联网 发布:tpshop2.0 源码下载 编辑:程序博客网 时间:2024/05/10 00:57
原文:http://www.javazy.com/contentex/2005622172315.shtml 
在前面,我介绍了由javax.mail.Message实现的javax.mail.Part接口。我现在将解释它的消息部分,它在这个例子中很重要。

           
   图3表示在前面例子中建立的一个Message,它既可以是一个消息,也可以是一个消息部分,因为它实现了Part接口。对于任何部分,你都能得到它的 内容(任何Java对象),并且在发送的是一个简单文本消息的情况下,内容对象可能是一个String。对于多部分消息,内容可能是类型 Multipart,由此我们可以得到单独的正文部分,它本身就实现了Part接口

  实际上,当你看过SimpleReceiver类 的代码之后,你会发现一切都变得很明朗。我们用三部分内容来介绍SimpleReceiver类:第一部分,类的定义以及从命令行获取连接细节信息的 main()方法;第二部分,捕获和查看进来消息的receive()方法;第三部分,打印头信息和每个消息内容的printMessage()方法。

  下面是第一部分:

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);

}

  现在我们使用命令行来运行它(记住用你的email设置替换命令行参数):

 java com.lotontech.mail.SimpleReceiver pop.myIsp.net myUserName myPassword   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();}

 }

}



  请注意:你从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();

  }

 }

}

  为了简单起见,我假设消息本身或者消息正文的第一部份是可以打印的。对于真正的应用软件,可能你想要依次检查消息正文的每一部分,并且对每一部分采取相应的行动-打印或者是保存到磁盘,这取决于内容的类型。

  当你从消息存储中得到每个消息时,你实际上已经得到了一个轻量级的封装器。数据内容的获取是每申请一次就读取一次-这对于你只想下载消息头时很有用。
  SimpleReceiver测试

  让我们对SimpleReceiver做一次测试。为了让它有东西可以接收,我发送图4所示的消息(注意:消息由文本和一个附件组成)


    
  一旦接收到消息,就把该消息认为是一个多部分消息。打印的文本如下:

FROM: Tony Loton

SUBJECT: Number 1

[ Multipart Message ]

CONTENT:text/plain;

charset="iso-8859-1"

Attachment 1

from Tony Loton.

-----------------------------

  把你的消息送出去

  为了有趣一点,并且说明JavaMail APIs的一个新颖的用法,我现在简要介绍一下我的谈话email项目。在做这个试验之前你需要得到lotontalk.jar文件,并把它加到你的classpath中去,添加方法如下:

  set CLASSPATH=%CLASSPATH%;lotontalk.jar
  你也需要在SimpleReceiver类中两个地方做代码修改。首先在receive()方法里面,把以下代码:

// -- 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(),它与最初的printMessage()方法相似。

/**

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

  }

 }

  因为在说话之前,你正在把整个消息积累到一个字符串中,所以这个方案可能只适合小的消息。作为一种选择,你可以读一行然后再讲一行。

  当然,我不可能把结果显示给你看,因此你必须亲自来做实验。

  你还可以做一些小的试验,当然不是在这个试验中,来发现语音合成的一些有趣的特征:怎样处理数字,以及怎样把全部大些的单词假想成只取首字母的缩写词,然后一个一个字母地把它们拼出来。

  结论

  我们已经通过对发送和接收email消息的应用程序的各个基本构造块分别讲解,涉及到了收发email的方方面面。如果你是第一次接触JavaMail,是不是发现在应用程序中收发email不是一件困难的事情。
原创粉丝点击