java 未读邮件_exchange服务器之Java 读取Exchange server 未读邮件数量

本文将带你了解exchange服务器之Java 读取Exchange server 未读邮件数量,希望本文对大家学Exchange有所帮助。

<

1、准备工作

一、打开exchange shell,使用如下命令提升administrator账号为exchange超级管理,让它具有对其他用户操作的权限

New-ManagementRoleAssignment -Name:impersonationAssignmentName -Role:ApplicationImpersonation -User:administrator1

注:如果以上命令执行失败,请尝试先执行下面这条命令,在执行上面的

Remove-ManagementRoleAssignment “impersonationAssignmentName”1

二、exchange server 可以进行web访问

https://xxxx.com/EWS/Exchange.asmx1

https://xxxx.com/owa1

三、添加邮箱账户

添加多个邮箱账户,同时给发这几个用户分别发送不同数量的邮件,记得不要查看,因为要读取未读邮件

2、关键代码

package com.codesql.utils;

import java.net.URI;

import microsoft.exchange.webservices.data.ConnectingIdType;

import microsoft.exchange.webservices.data.ExchangeCredentials;

import microsoft.exchange.webservices.data.ExchangeService;

import microsoft.exchange.webservices.data.ExchangeVersion;

import microsoft.exchange.webservices.data.Folder;

import microsoft.exchange.webservices.data.ImpersonatedUserId;

import microsoft.exchange.webservices.data.WebCredentials;

import microsoft.exchange.webservices.data.WellKnownFolderName;

public class ReaderExchanageUnreadMail {

// aduser=administrator

// adpass=密码

// addomain=域的名字

// suffix=@xxx.com

// uri=https://mail.xxx.com/EWS/Exchange.asmx

// sid 被查询用户的名称

public String getCount(String aduser, String adpass, String addomain, String uri, String suffix, String sid) throws Exception {

String count = “0”;

ExchangeService service = new ExchangeService(

ExchangeVersion.Exchange2010_SP1);

ExchangeCredentials credentials = new WebCredentials(aduser, adpass,addomain);

//ExchangeCredentials credentials = new WebCredentials(“bbbb”, “Aa123456″,”fr”);

service.setCredentials(credentials);

ImpersonatedUserId other= new ImpersonatedUserId(ConnectingIdType.SmtpAddress, sid+suffix);

service.setImpersonatedUserId(other);

service.setUrl(new URI(uri));

//service.setTraceEnabled(true);

// 服务地址

try {

Folder inbox = Folder.bind(service, WellKnownFolderName.Inbox);

// 收件箱

// System.out.println(“未读邮件数:” + inbox.getUnreadCount());

count = inbox.getUnreadCount() + “”;

} catch (Exception e) {

//  e.printStackTrace();

}

return count;

}

}1234567891011121314151617181920212223242526272829303132333435363738394041424344

当然,为了方便调用,具体代码我已经打包成jar,你可以在文章末尾的链接中下载

3、Reum.jar的使用

public class PropertiesParam {

private String aduser;

private String adpass;

private String addomain;

private String uri;

private String suffix;

public PropertiesParam(String aduser, String adpass, String addomain,

String uri, String suffix) {

super();

this.aduser = aduser;

this.adpass = adpass;

this.addomain = addomain;

this.uri = uri;

this.suffix = suffix;

}

// 省略 get/set

// toSting();

}12345678910111213141516171819

给ajax调用的方法

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

String identify = “0”; // 未读邮件数量

String sid = request.getParameter(“sid”); // sid 邮箱用户名称

ReaderExchanageUnreadMail  reum = new ReaderExchanageUnreadMail();

try {

identify = reum.getCount(initparam.getAduser(), initparam.getAdpass(),initparam.getAddomain()

, initparam.getUri(), initparam.getSuffix() , sid);

System.out.println(“count  ” + identify);

} catch (Exception e) {

e.printStackTrace();

}

try {

request.setCharacterEncoding(“utf-8”);

response.setContentType(“text/html;charset=utf-8”);

PrintWriter out = response.getWriter();

out.print(identify);

out.flush();

out.close();

} catch (IOException e) {

e.printStackTrace();

}

}12345678910111213141516171819202122232425

jar下载地址:链接: https://pan.baidu.com/s/1SOioBPlEDyTl1qqCHjMDmg 密码: 3qh1

备用下载地址:https://download.csdn.net/download/immortalitywang/10401841

参考链接:https://blog.csdn.net/feihu19851111/article/details/17135825

本文由职坐标整理并发布,希望对同学们有所帮助。了解更多详情请关注职坐标系统运维之Exchange频道!


版权声明:本文为weixin_32467423原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。