package net.sf.pim.mail.reader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URL;
import java.util.HashMap;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Part;
import net.sf.pim.mail.MailUtil;
import org.eclipse.jface.layout.GridDataFactory;
import org.eclipse.jface.resource.JFaceResources;
import org.eclipse.swt.SWT;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Font;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.layout.RowData;
import org.eclipse.swt.layout.RowLayout;
import org.eclipse.swt.program.Program;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Link;
import org.eclipse.swt.widgets.Menu;
/**
* @author levin
* 邮件中contenttype的格式关系
+------------------------- multipart/mixed ----------------------------+
| |
| +----------------- multipart/related ------------------+ |
| | | |
| | +----- multipart/alternative ------+ +----------+ | +------+ |
| | | | | 内嵌资源 | | | 附件 | |
| | | +------------+ +------------+ | +----------+ | +------+ |
| | | | 纯文本正文 | | 超文本正文 | | | |
| | | +------------+ +------------+ | +----------+ | +------+ |
| | | | | 内嵌资源 | | | 附件 | |
| | +----------------------------------+ +----------+ | +------+ |
| | | |
| +------------------------------------------------------+ |
| |
+----------------------------------------------------------------------+
*/
public class FileMessageReader extends Composite {
private Label from;
private Label to;
private Label cc;
private Label subject;
private Label sent;
private Composite attachments;
private Composite main;
private Browser body;
Message messageShow;
public FileMessageReader(Composite parent, int style) {
super(parent, style);
setLayout(new FillLayout());
create();
}
public void create() {
main = new Composite(this, SWT.NONE);
GridLayout layout = new GridLayout();
layout.numColumns = 2;
main.setLayout(layout);
GridDataFactory.fillDefaults().grab(true, true).applyTo(main);
// 头区
Font boldFont = JFaceResources.getFontRegistry().getBold(JFaceResources.DEFAULT_FONT);
Label l = new Label(main, SWT.WRAP);
l.setText("发件人:");
l.setFont(boldFont);
from = new Label(main, SWT.NONE);
from.setText("<from>");
l = new Label(main, SWT.WRAP);
l.setText("收件人:");
l.setFont(boldFont);
to = new Label(main, SWT.WRAP);
to.setText("<to>");
l = new Label(main, SWT.WRAP);
l.setText("抄送:");
l.setFont(boldFont);
cc = new Label(main, SWT.WRAP);
cc.setText("<cc>");
l = new Label(main, SWT.WRAP);
l.setText("接收时间:");
l.setFont(boldFont);
sent = new Label(main, SWT.WRAP);
sent.setText("<send date>");
l = new Label(main, SWT.WRAP);
l.setText("主题:");
l.setFont(boldFont);
subject = new Label(main, SWT.WRAP);
subject.setText("<subject>");
l = new Label(main, SWT.WRAP);
GridDataFactory.fillDefaults().align(SWT.BEGINNING, SWT.TOP).applyTo(l);
l.setText("附件:");
l.setFont(boldFont);
attachments=new Composite(main,SWT.NULL);
//设置layoutdata很关键,让它在vertical上自动
GridDataFactory.fillDefaults().grab(true, false).applyTo(attachments);
RowLayout rowLayout = new RowLayout();
rowLayout.spacing=8;
attachments.setLayout(rowLayout);
body = new Browser(main, SWT.WRAP | SWT.READ_ONLY);
//禁止鼠标右键的菜单,设置一个空的POP_UP菜单
Menu menu = new Menu(body.getShell(), SWT.POP_UP);
body.setMenu(menu);
GridData gridData = new GridData(GridData.FILL_BOTH);
gridData.horizontalSpan=2;
body.setLayoutData(gridData);
body.setText("<无邮件>");
}
public void showMessage(Message msg) throws MessagingException {
this.messageShow =msg;
from.setText(MailUtil.getAddress(msg.getFrom()));
MailUtil.resetSize(from);
to.setText(MailUtil.getAddress(msg.getRecipients(Message.RecipientType.TO)));
MailUtil.resetSize(to);
cc.setText(MailUtil.getAddress(msg.getRecipients(Message.RecipientType.CC)));
MailUtil.resetSize(cc);
sent.setText(MailUtil.getSentTime(msg));
MailUtil.resetSize(sent);
subject.setText(msg.getSubject() == null ?"":msg.getSubject());
MailUtil.resetSize(subject);
try {
//附件
final HashMap<String,Part> atts=new HashMap<String, Part>();
MailUtil.dumpPart(msg, atts);
//清除以前
for(Control c:attachments.getChildren())
c.dispose();
for(final String f:atts.keySet()){
Composite composite=new Composite(attachments,SWT.FLAT);
RowLayout rowLayout = new RowLayout();
rowLayout.wrap=false;
rowLayout.spacing=0;
composite.setLayout(rowLayout);
Image image=MailUtil.getImage(f);
Label label=new Label(composite,SWT.NULL);
if(image != null){
label.setImage(image);
}
Link link=new Link(composite,SWT.NULL);
link.setText("<a>"+f+"</a>");
link.addSelectionListener(new SelectionAdapter(){
public void widgetSelected(SelectionEvent e) {
//点击附件进行显示
Thread th = new Thread(new AttachmentViewer(f,atts.get(f)));
th.start();
}
});
composite.setLayoutData(new RowData(composite.computeSize(SWT.DEFAULT, SWT.DEFAULT)));
}
//重新设置附件区的大小
MailUtil.resetSize(attachments);
//正文
Object messageBody = getMessageBody(msg);
if(messageBody instanceof String) {
body.setText((String) messageBody);
}else if(messageBody instanceof URL){
body.setUrl(((URL)messageBody).toString());
}
} catch (Exception e) {
e.printStackTrace();
}
main.layout();
}
//取邮件体
protected Object getMessageBody(Part msg) throws MessagingException, IOException {
String body="无法识别的的邮件";
//投机一下,直接用ie的mht规范显示邮件,先不考虑附件的优化
if(msg.getHeader("FILE_FULL_NAME") != null){
return new File(msg.getHeader("FILE_FULL_NAME")[0]).toURL();
}
return body;
}
//显示附件
class AttachmentViewer implements Runnable {
String cmd;
Part p;
public AttachmentViewer(String file, Part part) {
cmd = file;
p = part;
}
public void run() {
try {
// Simply ask windows to open the file in a command shell.
// Windows will then open the file in whatever way it
// usually does or ask the user how to open it.
String str = System.getProperty("java.io.tmpdir") + cmd;
MailUtil.copyIntput2Output(p.getInputStream(), new FileOutputStream(str));
Program.launch(str);
// String theCmd = "cmd /c \"start \"\" \"" + str + "\"\"";
// Runtime.getRuntime().exec(theCmd);
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
public Message getMessageShow() {
return messageShow;
}
public void setMessageShow(Message messageShow) {
this.messageShow = messageShow;
}
}
评论0