通过pop3接受email
在前面,我介绍了由javax.mail.message实现的javax.mail.part接口。我现在将解释它的消息部分,它在这个例子中很重要。我们先看图3。
图3 mail.part接口的uml图
图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所示的消息(注意:消息由文本和一个附件组成)
图4 用于simplereceiver的测试消息
一旦接收到消息,就把该消息认为是一个多部分消息。打印的文本如下:
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]);
speakme
闽公网安备 35060202000074号