摘要:本文将带你了解exchange服务器之Java 读取Exchange server 未读邮件数量,希望本文对大家学Exchange有所帮助。
本文将带你了解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频道!
您输入的评论内容中包含违禁敏感词
我知道了
请输入正确的手机号码
请输入正确的验证码
您今天的短信下发次数太多了,明天再试试吧!
我们会在第一时间安排职业规划师联系您!
您也可以联系我们的职业规划师咨询:
版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
沪公网安备 31011502005948号