简体   繁体   中英

how to get email body in text from exchange server using EWS in java?

I have an application that reads the email from exchange using EWS. My problem is that to get the TEXT version of the email, not HTML version. I've tried to use this codes below. The "emailBody" what I've got from Exchange Server is not the Text version. I want to get only text, not html tags. So please recommend any method to covert it.

    public Map readEmailItem(ItemId itemId){
       Map messageData = new HashMap();
       try{
       Item itm = Item.bind(service, itemId, pertySet.FirstClassProperties);
       EmailMessage emailMessage = EmailMessage.bind(service, itm.getId());
       messageData.put("emailItemId", emailMessage.getId().toString());
       messageData.put("subject", emailMessage.getSubject().toString());
       messageData.put("fromAddress",
                         emailMessage.getFrom().getAddress().toString());
       messageData.put("senderName",
                         emailMessage.getSender().getName().toString());
       Date dateTimeCreated = emailMessage.getDateTimeCreated();
       messageData.put("SendDate",dateTimeCreated.toString());
       Date dateTimeRecieved = emailMessage.getDateTimeReceived();
       messageData.put("RecievedDate",dateTimeRecieved.toString());
       messageData.put("Size",emailMessage.getSize()+"");
       messageData.put("emailBody",emailMessage.getBody().toString());
       }catch (Exception e) {
       e.printStackTrace();
       }
      return messageData;
    }

When you Bind to the Item you need to specify you want the Text Body using the propertyset class eg modify your code like

PropertySet BindPropSet = new PropertySet(BasePropertySet.FirstClassProperties);
BindPropSet.RequestedBodyType = BodyType.Text;
Item itm = Item.bind(service, itemId, BindPropSet);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM