Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/364.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
EWS Java API获取附件_Java_Email_Email Attachments_Exchangewebservices_Ewsjavaapi - Fatal编程技术网

EWS Java API获取附件

EWS Java API获取附件,java,email,email-attachments,exchangewebservices,ewsjavaapi,Java,Email,Email Attachments,Exchangewebservices,Ewsjavaapi,我在使用ews java API 1.3快照获取附件时遇到一些问题,我想在电子邮件中获取附件,我的代码如下: try { ExchangeService service; service.setUrl(new URI("https://" + myserver + "/ews/Exchange.asmx")); ExchangeCredentials credentials = new WebCredentials(username, password); ser

我在使用ews java API 1.3快照获取附件时遇到一些问题,我想在电子邮件中获取附件,我的代码如下:

try {
    ExchangeService service;
    service.setUrl(new URI("https://" + myserver + "/ews/Exchange.asmx"));
    ExchangeCredentials credentials = new WebCredentials(username, password);
    service.setCredentials(credentials);
    ItemView view = new ItemView(Integer.MAX_VALUE);
    view.getOrderBy().add(ItemSchema.DateTimeReceived, SortDirection.Ascending);
    Folder folder = Folder.bind(service, WellKnownFolderName.Inbox);
    FindItemsResults<Item> results = service.findItems(folder.getId(),view);
    service.loadPropertiesForItems(results, new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));

        for (Item item : results) {
        Item itm = Item.bind(service, item.getId(), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));
        EmailMessage emailMessage = EmailMessage.bind(service, itm.getId(), new PropertySet(BasePropertySet.FirstClassProperties, EmailMessageSchema.Attachments));
        if (emailMessage.getHasAttachments()) {
                for (Attachment attachment : emailMessage.getAttachments()) {
                    String FileExtension = getFileExtension(attachment.getName());
                    File TempFile = File.createTempFile(attachment.getName(), FileExtension);
                    attachment.load(TempFile.getAbsolutePath());
                }
            }
        }
} catch (Exception e) {
    logger.error("Error ", e.getMessage());
}
试试看{
交换服务;
setUrl(新URI(“https://“+myserver+”/ews/Exchange.asmx”);
ExchangeCredentials凭据=新的WebCredentials(用户名、密码);
服务。设置凭据(凭据);
ItemView视图=新的ItemView(整数.MAX_值);
view.getOrderBy().add(ItemSchema.DateTimeReceived,SortDirection.升序);
Folder Folder=Folder.bind(服务,WellKnownFolderName.Inbox);
FindItemsResults=service.findItems(folder.getId(),视图);
loadPropertiesForItems(结果、新属性集(BasePropertySet.FirstClassProperties、EmailMessageSchema.Attachments));
用于(项目:结果){
Item itm=Item.bind(服务,Item.getId(),新属性集(BasePropertySet.FirstClassProperties,EmailMessageSchema.Attachments));
EmailMessage EmailMessage=EmailMessage.bind(服务,itm.getId(),新属性集(BasePropertySet.FirstClassProperties,EmailMessageSchema.Attachments));
如果(emailMessage.getHasAttachments()){
对于(附件:emailMessage.getAttachments()){
字符串FileExtension=getFileExtension(attachment.getName());
File TempFile=File.createTempFile(附件.getName(),文件扩展名);
load(TempFile.getAbsolutePath());
}
}
}
}捕获(例外e){
logger.error(“error”,例如getMessage());
}
我的问题是它可以收到另一封没有附件的电子邮件,并且总是跳过有附件的电子邮件,示例如下, 在我的收件箱中,我有这个电子邮件列表

  • 发件人:a@gmail.com(附页)
  • 发件人:b@mycompany.com(无附件)
  • 发件人:c@hiscompany.com(附页)
  • 发件人:d@mycompany.com(无附件)
当我运行代码时,它总是收到没有附件的电子邮件,如下所示:

  • 发件人:b@mycompany.com(无附件)
  • 发件人:d@mycompany.com(无附件)
跳过另一封有附件的邮件,我不知道怎么会这样。有人能帮我吗?

HashMap attachments=newhashmap();
HashMap<String, HashMap<String, String>> attachments = new HashMap<String, HashMap<String, String>>();    

if (emailMessage.getHasAttachments() || emailMessage.getAttachments().getItems().size() > 0) {

        //get all the attachments
        AttachmentCollection attachmentsCol = emailMessage.getAttachments();

        log.info("File Count: " +attachmentsCol.getCount());

        //loop over the attachments
        for (int i = 0; i < attachmentsCol.getCount(); i++) {
            Attachment attachment = attachmentsCol.getPropertyAtIndex(i);
            //log.debug("Starting to process attachment "+ attachment.getName());

               //FileAttachment - Represents a file that is attached to an email item
                if (attachment instanceof FileAttachment || attachment.getIsInline()) {

                    attachments.putAll(extractFileAttachments(attachment, properties));

                } else if (attachment instanceof ItemAttachment) { //ItemAttachment - Represents an Exchange item that is attached to another Exchange item.

                    attachments.putAll(extractItemAttachments(service, attachment, properties, appendedBody));
                }
            }
        }
    } else {
        log.debug("Email message does not have any attachments.");
    }


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

    //extract the attachment content, it's not base64 encoded.
    attachmentContent = fileAttachment.getContent();

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

        //check the size
        int attachmentSize = attachmentContent.length;

        //check if the attachment is valid
        ValidateEmail.validateAttachment(fileAttachment, properties,
                emailIdentifier, attachmentSize);

        fileAttachments.put(UtilConstants.ATTACHMENT_SIZE, String.valueOf(attachmentSize));

        //get attachment name
        String fileName = fileAttachment.getName();
        fileAttachments.put(UtilConstants.ATTACHMENT_NAME, fileName);

        String mimeType = fileAttachment.getContentType();
        fileAttachments.put(UtilConstants.ATTACHMENT_MIME_TYPE, mimeType);

        log.info("File Name: " + fileName + "  File Size: " + attachmentSize);


        if (attachmentContent != null && attachmentContent.length > 0) {
            //convert the content to base64 encoded string and add to the collection.
            String base64Encoded = UtilFunctions.encodeToBase64(attachmentContent);
            fileAttachments.put(UtilConstants.ATTACHMENT_CONTENT, base64Encoded);
        }



//Extract Item Attachment
try {
    ItemAttachment itemAttachment = (ItemAttachment) attachment;

    PropertySet propertySet = new PropertySet(
            BasePropertySet.FirstClassProperties, ItemSchema.Attachments, 
            ItemSchema.Body, ItemSchema.Id, ItemSchema.DateTimeReceived,
            EmailMessageSchema.DateTimeReceived, EmailMessageSchema.Body);

    itemAttachment.load();
    propertySet.setRequestedBodyType(BodyType.Text);

    Item item = itemAttachment.getItem();

    eBody = appendItemBody(item, appendedBody.get(UtilConstants.BODY_CONTENT));

    appendedBody.put(UtilConstants.BODY_CONTENT, eBody);

    /*
     * We need to check if Item attachment has further more
     * attachments like .msg attachment, which is an outlook email
     * as attachment. Yes, we can attach an email chain as
     * attachment and that email chain can have multiple
     * attachments.
     */
    AttachmentCollection childAttachments = item.getAttachments();
    //check if not empty collection. move on
    if (childAttachments != null && !childAttachments.getItems().isEmpty() && childAttachments.getCount() > 0) {

        for (Attachment childAttachment : childAttachments) {

            if (childAttachment instanceof FileAttachment) {

                itemAttachments.putAll(extractFileAttachments(childAttachment, properties, emailIdentifier));

            } else if (childAttachment instanceof ItemAttachment) {

                itemAttachments = extractItemAttachments(service, childAttachment, properties, appendedBody, emailIdentifier);
            }
        }
    }
} catch (Exception e) {
    throw new Exception("Exception while extracting Item Attachments: " + e.getMessage());
}
如果(emailMessage.getHasAttachments()| | emailMessage.getAttachments().getItems().size()>0){ //获取所有附件 AttachmentCollection attachmentsCol=emailMessage.getAttachments(); log.info(“文件计数:+attachmentsCol.getCount()); //在附件上循环 对于(int i=0;i0){ //检查尺寸 int attachmentSize=attachmentContent.length; //检查附件是否有效 ValidateEmail.validateAttachment(文件附件、属性、, emailIdentifier,attachmentSize); fileAttachments.put(UtilConstants.ATTACHMENT_SIZE,String.valueOf(attachmentSize)); //获取附件名称 String fileName=fileAttachment.getName(); fileAttachments.put(UtilConstants.ATTACHMENT\u名称、文件名); 字符串mimeType=fileAttachment.getContentType(); fileAttachments.put(UtilConstants.ATTACHMENT\u MIME\u TYPE,mimeType); log.info(“文件名:“+fileName+”文件大小:“+attachmentSize”); if(attachmentContent!=null&&attachmentContent.length>0){ //将内容转换为base64编码字符串并添加到集合中。 String base64Encoded=UtilFunctions.encodeToBase64(attachmentContent); fileAttachments.put(UtilConstants.ATTACHMENT\u内容,base64Encoded); } //提取项目附件 试一试{ ItemAttachment ItemAttachment=(ItemAttachment)附件; PropertySet PropertySet=新的PropertySet( BasePropertySet.FirstClassProperties、ItemSchema.Attachments、, ItemSchema.Body、ItemSchema.Id、ItemSchema.DateTimeReceived、, EmailMessageSchema.DateTimeReceived,EmailMessageSchema.Body); itemAttachment.load(); propertySet.setRequestedBodyType(BodyType.Text); Item=itemAttachment.getItem(); eBody=appendItemBody(item,appendedBody.get(UtilConstants.BODY_CONTENT)); 附件BODY.put(UtilConstants.BODY\u内容,电子书); /* *我们需要检查项目附件是否有更多信息 *附件,如.msg附件,它是outlook电子邮件 *作为附件。是的,我们可以作为附件附加电子邮件链 *附件和电子邮件链可以有多个 *附件。 */ AttachmentCollection childAttachments=item.getAttachments(); //检查集合是否为空。继续 if(childAttachments!=null&&