Posted on 2010-12-11 00:23
penngo 阅读(2629)
评论(6) 编辑 收藏
现在的邮件服务提供商很多,而且邮箱的速度也很不错,相互之间也能互发信息,基于邮件服务器进行信息聊天,应该也不错。写个简易版的客户端工具测试下。
本文只做了纯文本内容的发送测试,图片和附件的没做。
功能实现的原理很简单:
客户端信息发送(邮件发送):和普通的smtp发送邮件一样。
客户端信息接收(邮件接收):因为要马上看到信息内容,所以需要用一个线程,死循环的不断登录邮箱收邮件。
代码实现也很简单,只是收发邮件的代码,这里不详说。
收发邮件的组件是javaMail,界面是swing做的。
收发信息实现类EmailUtil.java
public class EmailUtil {
private Account accout;
private static Session session;
private static Store store;
private Date latestSentDate;
public EmailUtil(){
if(session == null ){
initSession();
}
}
public void send(String mail_body){
try
{
if(session == null ){
boolean b = initSession();
if(b == false){
return;
}
}
MimeMessage mimeMsg = new MimeMessage(session);
mimeMsg.setSubject("即时信息回复");
Multipart mp = new MimeMultipart();
BodyPart bp1 = new MimeBodyPart();
bp1.setContent(mail_body, "text/html;charset=UTF-8");
mp.addBodyPart(bp1);
mimeMsg.setContent(mp);
mimeMsg.setFrom(new InternetAddress(accout.getFromEmail()));
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
.parse(accout.getToEmail()));
mimeMsg.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(accout.getSendHost(),
accout.getName(), accout.getPassword());
transport.sendMessage(mimeMsg, mimeMsg
.getRecipients(Message.RecipientType.TO));
transport.close();
} catch (Exception ex)
{
JOptionPane.showMessageDialog(null, "发送出错:" + ex.getMessage());
System.out.println("发送邮件出错:");
ex.printStackTrace();
}
}
public boolean receive(JEditorPane receivePane, StringBuffer sff){
try{
if(session == null ){
boolean b = initSession();
if(b == false){
return false;
}
}
if(store == null || store.isConnected() == false){
URLName urln = new URLName("pop3", accout.getReceiveHost(), 110, null,
accout.getName(), accout.getPassword());
store = session.getStore(urln);
store.connect();
}
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
System.out.println("邮件数:" + folder.getMessageCount());
if(latestSentDate == null){
latestSentDate = DateUtil.parseDate("2010-12-10 00:00:00");
}
Address fromEmail = new InternetAddress(accout.getToEmail());
SearchTerm term1 = new FromTerm(fromEmail);
SearchTerm st = new SentDateTerm(SentDateTerm.GT, latestSentDate);
SearchTerm term = new AndTerm(term1, st);
Message[] msgs = folder.search(term);
System.out.println("符合条件邮件数:" + msgs.length);
if(msgs.length > 0){
for(int i = 0; i < msgs.length; i++){
Message m = msgs[i];
String msg = "";
try{
if(m.isMimeType("multipart/*")){
Multipart mp=(Multipart)m.getContent();
Part part=mp.getBodyPart(0);
msg=(String)part.getContent();
}
else{
msg = (String)m.getContent();
}
latestSentDate = m.getSentDate();
String from = getFrom(m);
String sentUser = "<span style=\"color:#1E90FF;\">" + from + " " + DateUtil.dateToStr(latestSentDate) + "</span><br/>";
sff.append(sentUser);
sff.append(msg + "<br/><br/>");
}
catch(Exception e){
System.out.println("读取第" + i + "份邮件出错");
e.printStackTrace();
}
}
receivePane.setText(sff.toString());
}
}
catch(Exception e){
sff.append("<font color=\"red\">接收邮件出错:" + e.getMessage() + "</font><br/>");
receivePane.setText(sff.toString());
System.out.println("接收邮件出错:");
e.printStackTrace();
return false;
}
return true;
}
private boolean initSession(){
if(MessageDialog.getAccount() != null){
accout = MessageDialog.getAccount();
Properties props = new Properties(); // 获取系统环境
props.put("mail.smtp.host", accout.getSendHost());
props.put("mail.smtp.auth", "true");
session = Session.getDefaultInstance(props, null);
return true;
}
else{
return false;
}
}
private String getFrom(Message mimeMessage) throws Exception {
InternetAddress[] address = (InternetAddress[]) mimeMessage.getFrom();
String from = address[0].getAddress();
if (from == null){
from = "";
}
String personal = address[0].getPersonal();
if (personal == null){
personal = "";
}
String fromaddr = personal + " <" + from + ">";
return fromaddr;
}
public void getMailContent(Part part, StringBuffer sff) throws Exception {
String contenttype = part.getContentType();
int nameindex = contenttype.indexOf("name");
boolean conname = false;
if (nameindex != -1)
conname = true;
System.out.println("CONTENTTYPE: " + contenttype);
if (part.isMimeType("text/plain") && !conname) {
sff.append((String) part.getContent());
} else if (part.isMimeType("text/html") && !conname) {
sff.append((String) part.getContent());
} else if (part.isMimeType("multipart/*")) {
Multipart multipart = (Multipart) part.getContent();
int counts = multipart.getCount();
for (int i = 0; i < counts; i++) {
getMailContent(multipart.getBodyPart(i), sff);
}
} else if (part.isMimeType("message/rfc822")) {
getMailContent((Part) part.getContent(), sff);
} else {}
}
}
下面看效果,例子是用了QQ邮箱和网易邮箱测试。QQ邮箱在win 7系统使用,网易邮箱在Ubuntu使用
程序运行时,先点击“配置”按钮配置邮件服务器和帐号信息,配置完后保存,再点登录,如果配置没有填错的话,就可以正常的收发信息。
QQ邮箱
163邮箱
用这个客户端工具跑了一段时间,个人感觉还是不错。唯一不足就是跑了一个小时后,163邮箱出现登录失败的情况,估计是服务器端设了限制,禁止客户端连续不断的访问邮箱。
运行程序:
im.jar.rar