【背景】
近日在研究domino web郵件下載功能,下載的郵件可以導入foxmail、outlook郵件客戶端,下載的郵件格式為eml。
【參考代碼】(需要下載jmail.jar包)
jar包下載地址:jar包下載
------------------------------------------分割線------------------------------------------
免費下載地址在 http://linux.linuxidc.com/
用戶名與密碼都是www.linuxidc.com
具體下載目錄在 /2014年資料/7月/24日/Java代理實現domino web郵件下載
下載方法見 http://www.linuxidc.com/Linux/2013-07/87684.htm
------------------------------------------分割線------------------------------------------
private void makeEml(String unid) {
Document memo=null;
RichTextItem body=null;
try {
memo = maildb.getDocumentByUNID(unid);
String mailtime=((String) memo.getCreated().getLocalTime());
String d_email = memo.getItemValueString("From");
Vector v_to = memo.getItemValue("SendTo");
Vector v_cc = memo.getItemValue("CopyTo");
Vector v_bcc = memo.getItemValue("BlindCopyTo");
String m_subject = memo.getItemValueString("Subject");
String m_text = memo.getItemValueString("Body");
Properties props = new Properties();
Session session = Session.getInstance(props, null);
//System.out.println("-----00000000------");
try {
MimeMessage msg = new MimeMessage(session);
msg.setSubject(m_subject);
//msg.setFrom(new InternetAddress(javax.mail.internet.MimeUtility.encodeText(d_email,"UTF-8", "B").replaceAll(" ", "")));
msg.setFrom(new InternetAddress(javax.mail.internet.MimeUtility.encodeText((String)d_email,"UTF-8", "B").replaceAll(" ", "")));
//System.out.println("-----111111------");
for (int i=0; i<v_to.size(); i++){
System.out.println("-----111111---0000---="+javax.mail.internet.MimeUtility.encodeText((String) v_to.elementAt(i),"UTF-8", "B").replaceAll(" ", ""));
InternetAddress[] toaddress = InternetAddress.parse(javax.mail.internet.MimeUtility.encodeText((String) v_to.elementAt(i),"UTF-8", "B").replaceAll(" ", ""));
msg.addRecipients(Message.RecipientType.TO, toaddress);
//msg.addRecipients(Message.RecipientType.TO, toaddress);
//System.out.println("-----111111---1111--222-");
}
for (int i=0; i<v_cc.size(); i++){
InternetAddress[] ccaddress = InternetAddress.parse(javax.mail.internet.MimeUtility.encodeText((String) v_cc.elementAt(i),"UTF-8", "B").replaceAll(" ", ""));
msg.addRecipients(Message.RecipientType.CC, ccaddress);
}
//System.out.println("-----111111--333----");
for (int i=0; i<v_bcc.size(); i++){
InternetAddress[] bccaddress = InternetAddress.parse(javax.mail.internet.MimeUtility.encodeText((String) v_bcc.elementAt(i),"UTF-8", "B").replaceAll(" ", ""));
msg.addRecipients(Message.RecipientType.BCC, bccaddress);
}
MimeBodyPart p1 = new MimeBodyPart();
p1.setText(m_text);
Multipart mp = new MimeMultipart();
mp.addBodyPart(p1);
String extractMimePath=extractPath+memo.getNoteID()+"/";
File extractDir2 = new File(extractMimePath);
extractDir2.mkdirs();
MIMEEntity mime = memo.getMIMEEntity();
if (mime != null) {
MimeBodyPart p2 = new MimeBodyPart(mime.getInputStream());
mp.addBodyPart(p2);
} else {
//System.out.println("Body="+m_text);
//System.out.println("Body="+memo.getFirstItem("Body").getType())
body = (RichTextItem) memo.getFirstItem("Body");
Vector v = body.getEmbeddedObjects();
Enumeration e = v.elements();
while (e.hasMoreElements()) {
eo = (EmbeddedObject) e.nextElement();
filename = eo.getName();
eo.extractFile(extractMimePath + filename);
FileDataSource fds = new FileDataSource(extractMimePath
+ filename);
MimeBodyPart p2 = new MimeBodyPart();
p2.setDataHandler(new DataHandler(fds));
p2.setHeader("Content-Transfer-Encoding", "Base64");
p2.setFileName(MimeUtility.encodeText(filename));
mp.addBodyPart(p2);
}
}
msg.setContent(mp);
File emlFile = new File(extractPath+StringFilter(m_subject+" "+mailtime)+".eml");
//File emlFile = new File(extractPath+memo.getNoteID()+".eml");
emlFile.createNewFile();
FileOutputStream outFile = new FileOutputStream(emlFile);
msg.writeTo(outFile);
outFile.close();
deleteDir(extractDir2);
} catch (MessagingException mex) {
deleteDir(extractDir2);
mex.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}finally{
recycleRes(eo);
recycleRes(body);
recycleRes(memo);
}
}
public static boolean deleteDir(File dir) {
if (dir.isDirectory()) {
String[] children = dir.list();
for (int i = 0; i < children.length; i++) {
boolean success = deleteDir(new File(dir, children[i]));
if (!success) {
return false;
}
}
}
return dir.delete();
}
public static String StringFilter(String str) {
String regEx="[`~!@#$%^&*()+=|{}':;',\\[\\]\\\\.<>/?~!@#¥%……&*()——+|{}【】‘;:”“’。,、?]";
Pattern p = Pattern.compile(regEx);
Matcher m = p.matcher(str);
//m=m.replaceAll("\\\\","");
return m.replaceAll("").trim();
}
--------------------------------------分割線 --------------------------------------
編寫高質量代碼 改善Java程序的151個建議 PDF高清完整版 http://www.linuxidc.com/Linux/2014-06/103388.htm
Java 8簡明教程 http://www.linuxidc.com/Linux/2014-03/98754.htm
Java對象初始化順序的簡單驗證 http://www.linuxidc.com/Linux/2014-02/96220.htm
Java對象值傳遞和對象傳遞的總結 http://www.linuxidc.com/Linux/2012-12/76692.htm
Java對象序列化ObjectOutputStream和ObjectInputStream示例 http://www.linuxidc.com/Linux/2012-08/68360.htm
--------------------------------------分割線 --------------------------------------