0

I'm trying to validate the attachment is password protected or not, is there a property that I can check. I don't want to process the attachment if it's password protected.

Below is the code I tried:

        FileAttachment fileAttachment = (FileAttachment) attachment;
        // if we don't call this, the Content property may be null.
        fileAttachment.load();

        // I was hoping this should not give me content of the file if it's password protected
        attachmentContent = fileAttachment.getContent(); //but I get the content without error.

        if (attachmentContent != null && attachmentContent.length > 0) {

            //check the size, I get the length as well
            int attachmentSize = attachmentContent.length;

Again I check if I can Decode the content, I get the decoded content as well.

byte[] decodedFile;
    try {
        String base64Encoded = Base64.encodeBase64String(attachmentContent);
        decodedFile = Base64.decodeBase64(base64Encoded);
    } catch (Exception e) {
        throw e);
    }
Lucky
  • 783
  • 2
  • 10
  • 28
  • What type is the file? If it's PDF or ZIP, the associated libraries should throw an exception to alert you when you try to do read/unpack. – Compass Feb 16 '17 at 20:39
  • Sorry forgot to mention, i'm testing with .xlsx file. That is what I was expecting that when I try to load the attachment it should throw exception, or when I try to encode/Decode the file. But NO, it processes the attachment. – Lucky Feb 16 '17 at 20:41
  • 1
    http://stackoverflow.com/a/28645380/2958086 this might be what you're looking for – Compass Feb 16 '17 at 20:45
  • I was hoping that there should be some EWS API property that I can check, but this is helpful too. I'll try with the Apache POI. – Lucky Feb 16 '17 at 20:52
  • I don't believe that ews itself would be able to determine it as each file type likely has its own encryption standards. An encrypted PDF, for example, would throw a Throwable and crash our program at one point before I figured out what was going on. – Compass Feb 16 '17 at 21:04

0 Answers0