3.2 构建邮件监控对话框
当然你需要建立一个 MailConfig 类用来表示一个邮箱的具体设置已及相关信息,这里就不在累述说明,详情参见参考资料中的代码。需要说明的式 MailConfig 除了要记录一个邮箱的地址,用户名和密码外,还提供 2 个关键字数组,如果为空,不加过滤,如果关键字有值,系统会根据发件人和邮件标题中是否包含关键字来进行显示邮件信息,已保证你的绝对自由。
首先我们需要实现一个 MailConfig 类,表示邮件配置,每个 MailConfig 的对象代表一个邮件帐户,我们的系统将能显示多个邮箱的配置,每个 MailConfig 中使用一个数组来保存需要过滤的收件人和邮件地址。
MailConfig 类的中的变量如下:
String popServer;
String popUser;
String popPassword;
// 设 置 需 要 过 滤 的 关 键 字 : 发 件 人 和 邮 件 主 题
String [] strFrom;
String [] strSubject;
// 是 否 显 示 邮 件 内 容
boolean isViewContent=false;
同样,我们将使用一个对话框来显示邮件信息,MailDialog 需要引用 javaMail.jar, 和 activation.jar 这两个类包,确保已经有这两个类包并加入到项目的类路径中。最后的 MailDialog 代码如下:
package myplugin;
import java.io.IOException;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.InternetAddress;
import javax.swing.JDialog;
import javax.swing.JEditorPane;
import javax.swing.JTextPane;
/**
* @author zhaoyong
*
* TODO To change the template for this generated type comment go to
* Window - Preferences - Java - Code Style - Code Templates
*/
public class MailDialog extends JDialog
{
private JEditorPane jEditorPane = null;
private JTextPane jTextPane = null;
// 可以显示多个邮件配置
MailConfig[] mc= null;
/**
* This method initializes
* 构造函数
* @param mc : 需要显示的多个邮箱配置对象。
*/
public MailDialog(MailConfig[] mc)
{
super();
if(mc!=null)
this.mc = mc;
else
System.err.println("邮件配置错误!") ;
initialize();
}
/**
* This method initializes this
* 初始化
* @return void
*/
private void initialize()
{
try
{
// 设定显示内容的面板
this.setContentPane(getJTextPane());
// 取得所有的新邮件信息
String s= getAllMailInfo();
// 将信息显示在对话框中
this.jTextPane .setText(s);
this.setTitle("邮件信息");
this.setSize(251, 100);
}
catch (Exception e)
{
// 发生错误显示错误信息
this.jTextPane .setText(e.toString());
e.printStackTrace();
}
}
/** 取得所有的邮箱的需要监控的邮件信息
*
* @return String
*/
private String getAllMailInfo()
{
String allMailInfo="";
if (mc.length <1)
allMailInfo="没有配置邮箱!";
else
{
for(int i=0;i
{
// 循环获取每个邮箱的邮件信息
allMailInfo=allMailInfo+getMailInfo(mc[i]);
}
}
// 还没有收到相关的邮件
if (allMailInfo.trim().length() ==0)
allMailInfo="未检测到相关新邮件!";
return allMailInfo;
}
/*
* 得到一个邮箱中满足条件的所有新邮件的字符串形式
**/
private String getMailInfo(MailConfig mc)
{
// 最终输出的邮件信息
String mailInfo="";
// 每个邮箱服务器上的 Store 和 Folder 对象
Store store=null;
Folder folder=null;
try
{
Properties props = System.getProperties();
// 与邮件服务器生成一个 Session
Session session = Session.getDefaultInstance( props,null);
// 给出服务器,用户名,密码连接服务器
store = session.getStore("pop3");
store.connect(mc.getPopServer(), mc.getPopUser(),mc.getPopPassword());
// 取得默认的邮件 Folder
folder = store.getDefaultFolder();
if (folder == null)
throw new Exception("No default folder");
// 取得收件箱
folder = folder.getFolder("INBOX");
if (folder == null)
throw new Exception("No POP3 INBOX");
// 以只读方式打开收件箱
folder.open(Folder.READ_ONLY);
// 获取所有新邮件并处理
Message[] msgs = folder.getMessages();
for (int i = 0; i < msgs.length; i++)
{
Message message= msgs[i];
// 取得每封邮件的信息,需要引用 MailConfig 对象进行关键字过滤
mailInfo = mailInfo+ getMessageInfo( message,mc);
}
}
catch (Exception ex)
{
ex.printStackTrace();
}
finally
{
// 安全的关闭邮件服务器资源
try
{
if (folder!=null) folder.close(true);
if (store!=null) store.close();
}
catch (Exception ex2) {ex2.printStackTrace();}
}
return mailInfo;
}
/**
* 得到一封邮件的信息,需要根据 MailConfig 过滤
* @param mailInfo
* @param message
* @return 邮件信息
* @throws MessagingException
* @throws IOException
*/
private String getMessageInfo( final Message message ,final MailConfig mc)
throws MessagingException, IOException
{
// 返回的改邮件信息
String mailInfo="";
String from=((InternetAddress)message.getFrom()[0]).getPersonal();
if (from==null)
from=((InternetAddress)message.getFrom()[0]).getAddress();
String subject=message.getSubject();
// 如果满足过滤信息则显示,否则返回空
if(isElementinString(from,mc.getStrFrom())
||isElementinString(subject,mc.getStrSubject()) )
{
mailInfo=mailInfo+"发件人 : "+from+"\n";
mailInfo=mailInfo+"邮件主题 : "+subject+"\n";
mailInfo=mailInfo+"发送时间 : "+message.getSentDate() +"\n";
// 如果显示内容,则打印内容
if(mc.isViewContent)
mailInfo=mailInfo+message.getContent() +"\n";
mailInfo=mailInfo+"------------------------------------\n";
}
return mailInfo;
}
private JTextPane getJTextPane()
{
if (jTextPane == null)
{
jTextPane = new JTextPane();
}
return jTextPane;
}
/**
* 判断目标关键字数组中是否有指定的字符串 , 进行过滤
* @param targetStr :
* @param keys :
* @return 如果有,返回 true, 否则返回 false
*/
private boolean isElementinString(String targetStr,String [] keys)
{
// 没指定过滤条件,显示所有
if (keys==null)
return true;
// 指定字符串为空,直接返回 false
if (targetStr==null)
return false;
for(int i=0;i
{
if (targetStr.indexOf(keys[i])>-1)
return true;
}
return false;
}
}
// @jve:decl-index=0:visual-constraint="10,10"--说明,这是 Visual Editor 添加的控制信息
以上代码的注释已经保证你能够看清楚,这里就不加累述,有兴趣的可以自己试试,体验一切尽在掌握的快感。当然这个例子做的实在简单,因此也为你的进一步开发留有足够的余地。
3.3 打包和发布
到此,在 mypulgin 中增加了邮件信息菜单和对话框,系统的 plugin.xml 如下:
id="myplugin"
name="Myplugin Plug-in"
version="1.0.0"
provider-name=""
class="myplugin.MypluginPlugin">
point="org.eclipse.ui.actionSets">
label="Sample Action Set"
visible="true"
id="myplugin.actionSet">
label="我的空间"
id="sampleMenu">
name="sampleGroup">
label="天气预报"
icon="icons/sample.gif"
class="myplugin.actions.SampleAction"
tooltip="Hello, Eclipse world"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.SampleAction">
label="邮件信息"
icon="icons/sample.gif"
class="myplugin.actions.MailAction"
tooltip="邮件信息"
menubarPath="sampleMenu/sampleGroup"
toolbarPath="sampleGroup"
id="myplugin.actions.MailAction">
实际上,我们在一个插件中加入了 2 个功能,因此就实现了我们的开发环境的自我扩展和定制。 同样,参考 Eclipse 的帮助,你可以轻松的再次将插件打包导出,并将其加入自己的 Eclipse 的 plugins 目录(可能需要解压缩),或通过 help 菜单的 Update 选项进行安装,注意导出时需要选定相关的类包。重新启动,你将发现自己的 IDE 已经多了自己的菜单,开发环境已经随着自己的意愿在改变了,程序员天生的满足感油然而生。
现在,你可以在需要的时候点击菜单,了解你希望监控的邮件情况或者最近的天气情况,一切轻松的尽在掌握,Eclipse 的插件,就是这样全能