Use the EWS (Exchange Web Service) protocol to read messages, send messages

Source: Internet
Author: User

Problem:

Before the company can send and receive mail through the POP3 protocol, so in SOAPUI with JavaMail can read the mail, later configured Office 365,POP3 protocol port is no longer open, mail all read failed, reported to login timeout, You need to use the EWS (Exchange Web Service) protocol instead.

Reference: http://blog.csdn.net/yangcheng33/article/details/55049629

This jar package needs to be imported: Ews-java-api-2.0.jar

Import Java.net.URIimport Java.net.URISyntaxExceptionimport Microsoft.exchange.webservices.data.core.*import Microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersionimport Microsoft.exchange.webservices.data.core.enumeration.property.*import Microsoft.exchange.webservices.data.core.enumeration.service.ConflictResolutionModeimport Microsoft.exchange.webservices.data.core.service.item.Itemimport Microsoft.exchange.webservices.data.core.service.schema.AppointmentSchemaimport Microsoft.exchange.webservices.data.credential.*import Microsoft.exchange.webservices.data.property.complex.MessageBodyimport Microsoft.exchange.webservices.data.search.*string Contentsubject = "Regarding area" String emailaddress = "[Email  protected] "String password =" Autotest202010 "int maxemailsget = 5String queryString =" "Exchangeservice service = ge TService (emailaddress, password); finditemsresults<item> FindItems = Getemails (Maxemailsget, queryString, service);//Iterate message content for (Item Mail: FindItems) {String subject = Mail.getsubject ()//log.info "Email Subject:" +subjectmessagebody messagebody = Mail.getbod Y () Messagebody.setbodytype (bodytype.text) def hasattachment = mail.gethasattachments ()//log.info "has attatchment?:" +hasattachmentif (Subject.contains (Contentsubject) &&hasattachment.equals (true) &&mail.getIsRead ( ). Equals (False)) {Assert true, "check email pass"//email is read and synced to the mail server so that no matter how the message is viewed, the message is read state Mail.setisread (true) Mail.update (Conflictresolutionmode.alwaysoverwrite) Break}else if (maxemailsget==1) {assert false, "Check Email Fail"} maxemailsget--}//read inbox messages under public static finditemsresults<item> getemails (int maxemailsget, String queryString, Exchangeservice service) throws Exception {//default read messages under Inbox, max read 5 wellknownfoldername folderName = Wellknownfoldername.inbox;itemview Itemview = new Itemview (maxemailsget); finditemsresults<item> FindItems = Service.finditems (FolderName, queryString, Itemview);//define the underlying properties, this step must be, Otherwise it throws an exception PropertySet DEtailedpropertyset = new PropertySet (basepropertyset.firstclassproperties, appointmentschema.recurrence); Service.loadpropertiesforitems (FindItems, Detailedpropertyset); return findItems;} Connection server 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;}

  

Use the EWS (Exchange Web Service) protocol to read messages, send messages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.