使用 EWS(Exchange Web Service)协议读取邮件、发送邮件
问题:
公司之前可以通过POP3协议收发邮件,因而在SoapUI中用JavaMail可以读取邮件,后来配置了Office 365,POP3协议端口不再开放,邮件全部读取失败,报login timeout,需要改用EWS(Exchange Web Service)协议。
参考 : http://blog.csdn.net/yangcheng33/article/details/55049629
需要导入此JAR包 : ews-java-api-2.0.jar
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 | import java.net.URI import java.net.URISyntaxException import microsoft.exchange.webservices.data.core.* import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion import microsoft.exchange.webservices.data.core.enumeration.property.* import microsoft.exchange.webservices.data.core.enumeration.service.ConflictResolutionMode import microsoft.exchange.webservices.data.core.service.item.Item import microsoft.exchange.webservices.data.core.service.schema.AppointmentSchema import microsoft.exchange.webservices.data.credential.* import microsoft.exchange.webservices.data.property.complex.MessageBody import microsoft.exchange.webservices.data.search.* String contentSubject = "Regarding area" String emailAddress = "auto.tester@zhang.com" String password = "Autotest202010" int maxEmailsGet = 5 String queryString = "" ExchangeService service = getService(emailAddress, password); FindItemsResults<Item> findItems = getEmails(maxEmailsGet, queryString, service); // 迭代 邮件内容 for (Item mail : findItems) { String subject = mail.getSubject() //log.info "Email subject : "+subject MessageBody messageBody = mail.getBody() messageBody.setBodyType(BodyType.Text) def hasAttachment = mail.getHasAttachments() //log.info "Has attatchment ? :"+hasAttachment if (subject.contains(contentSubject)&&hasAttachment.equals( true )&&mail.getIsRead().equals( false )){ assert true , "Check email pass" //置email为已读,并且同步到邮件服务器,这样无论以什么方式查看邮件,邮件都是已读状态 mail.setIsRead( true ) mail.update(ConflictResolutionMode.AlwaysOverwrite) break } else if (maxEmailsGet== 1 ){ assert false , "Check email fail" } maxEmailsGet-- } // 读取 Inbox下的邮件 public static FindItemsResults<Item> getEmails( int maxEmailsGet, String queryString, ExchangeService service) throws Exception { // 默认读取 Inbox下的邮件,最大读取5封 WellKnownFolderName folderName = WellKnownFolderName.Inbox; ItemView itemView = new ItemView(maxEmailsGet); FindItemsResults<Item> findItems = service.findItems(folderName, queryString, itemView); // 定义基础属性,此步骤必须要,否则会抛异常 PropertySet detailedPropertySet = new PropertySet(BasePropertySet.FirstClassProperties, AppointmentSchema.Recurrence); service.loadPropertiesForItems(findItems, detailedPropertySet); return findItems; } // 连接服务器 public static ExchangeService getService(String emailAddress, String password) throws URISyntaxException { ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010_SP1); ExchangeCredentials credentials = new WebCredentials(emailAddress, password); service.setUrl( new URI( "https://outlook.office365.com/EWS/Exchange.asmx" )); service.setCredentials(credentials); return service; } |
分类:
SoapUI
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 25岁的心里话
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
2013-09-27 JMeter Ant Task 生成的*.jtl打开之后request和response data是空的,怎样让其不是空的呢?
2013-09-27 Jmeter Ant Task如何让beanshell断言失败的详细信息展示在report里面
2013-09-27 [BAT]cmd命令之 cd /d %~dp0