切换到宽版
  • 5315阅读
  • 1回复

[求助]EXCHANGE连接邮箱,window可以,Linux下连不上 [复制链接]

上一主题 下一主题
 
只看楼主 倒序阅读 0楼  发表于: 2017-10-13
我直接附上代码,window是可以连接的;然而Linux上不知道哪里错了,就是连接不上

linux服务器是上不了外网,但是邮箱服务器是内网的,假如邮箱服务器IP地址是192.192.192.192  端口是444.   账号是33212@catlbattery.com密码是xxxxxx我该怎么设置这部分代码
ExchangeCredentials credentials = new WebCredentials(user, password);
        service.setCredentials(credentials);
        service.setUrl(new URI(mailServer));






package test;

import java.io.File;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.text.SimpleDateFormat;
import java.util.ArrayList;

import microsoft.exchange.webservices.data.Attachment;
import microsoft.exchange.webservices.data.AttachmentCollection;
import microsoft.exchange.webservices.data.BodyType;
import microsoft.exchange.webservices.data.EmailMessage;
import microsoft.exchange.webservices.data.ExchangeCredentials;
import microsoft.exchange.webservices.data.ExchangeService;
import microsoft.exchange.webservices.data.ExchangeVersion;
import microsoft.exchange.webservices.data.FileAttachment;
import microsoft.exchange.webservices.data.FindItemsResults;
import microsoft.exchange.webservices.data.Folder;
import microsoft.exchange.webservices.data.Item;
import microsoft.exchange.webservices.data.ItemSchema;
import microsoft.exchange.webservices.data.ItemView;
import microsoft.exchange.webservices.data.MessageBody;
import microsoft.exchange.webservices.data.PropertySet;
import microsoft.exchange.webservices.data.SearchFilter;
import microsoft.exchange.webservices.data.SortDirection;
import microsoft.exchange.webservices.data.WebCredentials;
import microsoft.exchange.webservices.data.WebProxy;
import microsoft.exchange.webservices.data.WellKnownFolderName;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;



public class ExchangeMailUtil {
    private String mailServer;
    private String user;
    private String password;
    private String domain;


    public  ExchangeMailUtil(String mailServer, String user, String password) {
        this.mailServer = mailServer;
        this.user = user;
        this.password = password;
    }

    public ExchangeMailUtil(String mailServer, String user, String password, String domain) {
        this.mailServer = mailServer;
        this.user = user;
        this.password = password;
        this.domain = domain;
    }

    public static void main(String[] args) throws Exception {
        // Outlook Web Access路径通常为/EWS/exchange.asmx
    
    
        ExchangeMailUtil mailUtil = new ExchangeMailUtil("https://mail.catlbattery.com/EWS/exchange.asmx",
                "3222@catlbattery.com", "xxxxxx");
        // 接收邮件
        ArrayList<EmailMessage> mails = mailUtil.receive(10);
  
        for (EmailMessage mail : mails) {
            System.out.println("邮件标题: " + mail.getSubject());
            System.out.println("ID:"+mail.getId());
            SimpleDateFormat sdf2= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String sendDate= sdf2.format(mail.getDateTimeReceived());
            System.out.println("接收时间: " + sendDate);
            System.out.println("发送人: " + mail.getFrom().getName() + ", 地址: " + mail.getFrom().getAddress());
            System.out.println("已读:" + mail.getIsRead());
            System.out.println("大小:"+mail.getSize());
            
//            System.out.println("body:"+mail.getBody().toString());
            readBodyText(mail.getBody().toString());
//            SimpleDateFormat   df   =   new   SimpleDateFormat("yyyy-MM-dd");
            
            // 更新已读
//            if (!mail.getIsRead()) {
//                mail.setIsRead(true);
//                mail.update(ConflictResolutionMode.AlwaysOverwrite);
//            }
            //System.out.println("邮件内容 :" + mail.getBody());

            // 处理附件
            AttachmentCollection attachs = mail.getAttachments();
            try {
                if (mail.getHasAttachments()) {
                    for (Attachment attach : attachs) {
                        if (attach instanceof FileAttachment) {
                            //接收邮件到临时目录
                            System.out.println(attach.getName()+attach.getSize());
                            File tempZip = new File("F:/tmp", attach.getName());
                            ((FileAttachment) attach).load(tempZip.getPath());
                        }
                    }
                    //删除邮件
                    //mail.delete(DeleteMode.HardDelete);
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            System.out.println();
        }

        // 发送邮件
        //mailUtil.send("Subject", new String[] { "xxx@domain.com" }, null, "content");

    }

    private static void readBodyText(String xml) {
        org.jsoup.nodes.Document doc = Jsoup.parse(xml);
        Elements body=doc.getElementsByTag("body");
        Element div=body.get(0);
        Elements p=div.getElementsByTag("p");
        String text="";
        System.out.println(p.size()+"-------行数");
        
        for(int x=0;x<p.size();x++){
            String str=p.get(x).text();
            if(str.indexOf("www.catlbattery.com")>=0||str.indexOf("Cell")>=0||str.indexOf("Office")>=0){
                break;
            }
            
            
//            Pattern pa= Pattern.compile("[\u4e00-\u9fa5]");
//            Matcher m = pa.matcher(p.get(x).text());
//    
//            if(!m.find()){
//                continue;
//            }
            text=text+p.get(x).text();
            
            
        }
        System.out.println("正文:"+text);        

        
    }

    

    /**
     * 收取邮件
     *
     * @param max          最大收取邮件数
     * @param searchFilter 收取邮件过滤规则
     * @return
     * @throws Exception
     */
    public ArrayList<EmailMessage> receive(int max, SearchFilter searchFilter) throws Exception {
        ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP2);
        //设置代理,若没有代理就不需设置
        //WebProxy w =new WebProxy("192.168.13.11",8888);
        //service.setWebProxy(w);
  
        ExchangeCredentials credentials = new WebCredentials(user, password);
        service.setCredentials(credentials);
        service.setUrl(new URI(mailServer));

      
        //绑定收件箱,同样可以绑定发件箱
        Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);
        
        
        
        //获取文件总数量
        int count = inbox.getTotalCount();
        if (max > 0) {
            count = count > max ? max : count;
        }
        //循环获取邮箱邮件
        ItemView view = new ItemView(count);
        //按照时间顺序收取
        view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Descending);
        FindItemsResults<Item> findResults;
        if (searchFilter == null) {
            findResults = service.findItems(inbox.getId(), view);
        } else {
            // e.g. new SearchFilter.SearchFilterCollection(
            // LogicalOperator.Or, new SearchFilter.ContainsSubstring(ItemSchema.Subject, "EWS"),
            // new SearchFilter.ContainsSubstring(ItemSchema.Subject, "API"))
            findResults = service.findItems(inbox.getId(), searchFilter, view);
        }
        service.loadPropertiesForItems(findResults, PropertySet.FirstClassProperties);
        ArrayList<EmailMessage> result = new ArrayList<EmailMessage>();
        for (Item item : findResults.getItems()) {
            EmailMessage message = (EmailMessage) item;
            result.add(message);
        }
        return result;
    }

    /**
     * 收取所有邮件
     *
     * @throws Exception
     */
    public ArrayList<EmailMessage> receive(int max) throws Exception {
        return receive(max, null);
    }

    /**
     * 收取邮件
     *
     * @throws Exception
     */
    public ArrayList<EmailMessage> receive() throws Exception {
        return receive(0, null);
    }

  

}
分享到
只看该作者 1楼  发表于: 2017-10-13
Re:EXCHANGE连接邮箱,window可以,Linux下连不 ..
还有,EWSJAVAAPI  是需要JDK多少以上的?1.5和1.6的都编译不过去
快速回复
限60 字节
 
上一个 下一个