• java实现代理domino web邮件下载


    【背景】

               近日在研究domino web邮件下载功能,下载的邮件能够导入foxmail、outlook邮件client,下载的邮件格式为eml。

    【參考代码】(须要下载jmail.jar包)

    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();     
    	  } 
    jar包下载地址:jar包下载

    
  • 相关阅读:
    第二章 创建rails应用程序
    System Call on Linux 2.6 for i386(1) INTRODUCTION
    splayer_directshow环境搭建
    90后要统治世界了
    在家写点东西
    转:SQL Server2005 中的数据类型总结
    原创:远程服务器返回错误: (500) 内部服务器错误
    转:关闭窗口Close()
    转:VS.net 2003 打开Web项目失败的解决方法
    js接收网页参数
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4556315.html
Copyright © 2020-2023  润新知