java Exchange 서비스 수신 메일

1622 단어 javaExchange
/**
serverName  
user  
pwd  
path  
max  
**/

public int receive(String serverName, String user, String pwd, String path, int max) throws Exception {
        // ExchangeVersion.Exchange2007_SP1 Exchange 
	ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2007_SP1);
	String[] userInfo = user.split("/");
	// 
    	ExchangeCredentials credentials = new WebCredentials(userInfo[1], pwd,userInfo[0]);
    	service.setCredentials(credentials);
    	// Exchange 
    	service.setUrl(new URI(serverName));
    	// 
    	Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
    	// 
    	int count = inbox.getTotalCount();
    	if(max > 0) count = count > max ? max : count; 
        // 
    	ItemView view = new ItemView(count);
        FindItemsResults<Item> findResults = service.findItems(inbox.getId(), view);
        for (Item item : findResults.getItems()) {
            EmailMessage message = EmailMessage.bind(service, item.getId());
            List<Attachment> attachs = message.getAttachments().getItems();
            try{
	            if(message.getHasAttachments()){
		            for(Attachment f : attachs){
		            	if(f instanceof FileAttachment){
		            	        // 
		            		File tempZip = new File(path,f.getName());
		            		((FileAttachment)f).load(tempZip.getPath());
		            	}
		            }
		            // 
		            message.delete(DeleteMode.HardDelete);
	            }
            }catch(Exception err){
            	log.equals(err);
            }
        }
		return count;
	}

좋은 웹페이지 즐겨찾기