Linux中国 Linux中国门户站!
设为主页 设为主页
收藏本站 收藏本站
 
当前位置 :首页 ->网络应用 ->邮件服务器 ->正文

企业级邮件服务器Apache James介绍(1)

来源:Linuxdby.com 作者:Webmaster 时间:2007-05-31 点击: [收藏] [投稿]

session = Session.getInstance(props, this);
}

public PasswordAuthentication getPasswordAuthentication()
{
return authentication;
}

public void sendMessage(
String to, String subject, String content)
throws MessagingException
{
System.out.println("SENDING message from " + from + " to " + to);
System.out.println();
MimeMessage msg = new MimeMessage(session);
msg.addRecipients(Message.RecipientType.TO, to);
msg.setSubject(subject);
msg.setText(content);
Transport.send(msg);
}

public void checkInbox(int mode)
throws MessagingException, IOException
{
if (mode == 0) return;
boolean show = (mode %26amp; SHOW_MESSAGES) > 0;
boolean clear = (mode %26amp; CLEAR_MESSAGES) > 0;
String action =
(show ? "Show" : "") +
(show %26amp;%26amp; clear ? " and " : "") +
(clear ? "Clear" : "");
System.out.println(action + " INBOX for " + from);
Store store = session.getStore();
store.connect();
Folder root = store.getDefaultFolder();
Folder inbox = root.getFolder("inbox");
inbox.open(Folder.READ_WRITE);
Message[] msgs = inbox.getMessages();
if (msgs.length == 0 %26amp;%26amp; show)
{
System.out.println("No messages in inbox");
}
for (int i = 0; i < msgs.length; i++)
{
MimeMessage msg = (MimeMessage)msgs[i];
if (show)
{
System.out.println(" From: " + msg.getFrom()[0]);
System.out.println(" Subject: " + msg.getSubject());
System.out.println(" Content: " + msg.getContent());
}
if (clear)
{
msg.setFlag(Flags.Flag.DELETED, true);
}
}
inbox.close(true);
store.close();
System.out.println();
}
}
===========================================
设计MailClient的主要目的是让我们发送消息,显示或者删除给定用户在服务器上的消息列表。我已经声明了一些有用的常量,让我们 显示消息SHOW_MESSAGES,清除消息 CLEAR_MESSAGES, 或者执行这两种操作。 MailClient类还实现了 Authenticator接口,以便在检索邮件时的在线处理更容易管理。
我创建了两个构造函数,其中一个显式的设置JavaMail的调试标志。在调试状态下,程序会把客户端/服务器的交互协议输出到控制台,这样就能看到发生了什么。 短的构造方法不打开调试标志,另外两个参数是用户名和主机,e-mail地址可以由用户名和主机得到。我们创建了一个在Authenticator接口中定义的 PasswordAuthentication 对象,这个对象可以由getPasswordAuthentication()方法获得。
构造函数的其余代码创建了JavaMail properties来保存所说明的用户名和主机,并且显式的说明了我们要用到的协议。一旦我们将这些值存入Properties对象,我们就能调用静态的Session 方法 getInstance() 来得到一个有效的Session 引用, 然后将它保存在局部变量中 。一旦构造函数被用于指定的用户,我们就准备好在指定的e-mail主机上发送或检索那个用户的e-mail。
sendMessage() 方法同样很简单。它用指定的接收者、主题和文本内容创建了一个 MimeMessage 对象,然后使用 JavaMail中Transport 类的静态方法 send() 发送这个消息。为了看清楚发生了什么,我们把这个过程也输出到控制台。
checkInbox() 方法做的工作比较多,因为它要列出消息列表,并可以根据选择删除它们。method does more work because it needs to list messages and, optionally, erase them. 也可以不查看直接删除消息,这取决于你在mode参数中使用的标志。It's also possible to just erase messages without looking at them, depending on the flags you use for the mode argument. 为了实际得到消息,我们需要通过session获得一个Store的引用。连接到服务器,然后打开收件箱文件夹。我们得到一个文件夹的引用之后,就可以循环显示或删除消息。
现在我们有了可重用的 MailClient 代码,我们可以为本机上的James服务器编写一个快速的测试代码了。列表5中的 JamesConfigTest 类创建了3个MailClient类的实例,分别代表我们创建的用户 (red、 green和 blue). 在你执行这段代码之前,确保那些用户在e-mail服务器上是有效的。
列表5. JamesConfigTest: James 服务器的一个快速测试
===========================================

public class JamesConfigTest
{
public static void main(String[] args)
throws Exception
{
// CREATE CLIENT INSTANCES
MailClient redClient = new MailClient("red", "localhost");
MailClient greenClient = new MailClient("green", "localhost");
MailClient blueClient = new MailClient("blue", "localhost");

// CLEAR EVERYBODY'S INBOX
redClient.checkInbox(MailClient.CLEAR_MESSAGES);
greenClient.checkInbox(MailClient.CLEAR_MESSAGES);
blueClient.checkInbox(MailClient.CLEAR_MESSAGES);
Thread.sleep(500); // Let the server catch up

// SEND A COUPLE OF MESSAGES TO BLUE (FROM RED AND GREEN)
redClient.sendMessage(
"blue@localhost",
"Testing blue from red",
"This is a test message");
greenClient.sendMessage(
"blue@localhost",
"Testing blue from green",
"This is a test message");
Thread.sleep(500); // Let the server catch up

// LIST MESSAGES FOR BLUE (EXPECT MESSAGES FROM RED AND GREEN)
blueClient.checkInbox(MailClient.SHOW_AND_CLEAR);
}
}
===========================================
创建了3个MailClient 实例之后, JamesConfigTest 先简单的用 checkInbox()方法以CLEAR_MESSAGES 模式清除每个邮箱,并且等待半秒以确保服务器处理完删除操作。接着分别从red和green发送一个消息到blue 。然后检查blue账号里的消息。当你运行JamesConfigTest的时候,你应该看到如列表6的输出:

 如果您对本文有任何疑问或者建议,请到讨论区发表您的意见: >> 论坛入口 <<



上一篇:如何用Apache James把E-mail从内网(内部局域网)发到外网(如263.net,hotmail.com)   下一篇:用apache james做简单的垃圾邮件过滤网关

文章评论】 【收藏本文】 【推荐好友】 【打印本文】 【我要投稿】 【论坛讨论
更多相关文章
Power by linux-cn.com 粤ICP备05006655号