【问题标题】:EWS Java: Create new email with existing item as attachmentEWS Java:使用现有项目作为附件创建新电子邮件
【发布时间】:2018-10-10 19:30:04
【问题描述】:

我正在使用ews-java-api 创建一个新的电子邮件消息,其中包含现有项目作为附件。我按照 Glen 对this 问题的回答在 java 中编写了类似的代码。但是,我收到错误消息 - “不支持将 EmailMessage 类型的项目作为附件”

这是我的代码 sn-p 供参考:

EmailMessage approvalMessage = new EmailMessage(exchangeService);
ItemAttachment itemAttachment = approvalMessage.getAttachments().addItemAttachment(EmailMessage.class);

MimeContent mimeContent = ewsItem.getMimeContent();
Item attachedItem = itemAttachment.getItem();
attachedItem.setMimeContent(mimeContent);
ExtendedPropertyDefinition prFlags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);
attachedItem.setExtendedProperty(prFlags, "1");
itemAttachment.setName(subject);

approvalMessage.setSubject("Reminder Email with original email as attachment");
approvalMessage.getToRecipients().add("my-email-address");
approvalMessage.send();
  • approvalMessage 是我正在创建的新电子邮件
  • ewsItem 是我想作为附件添加到approvalMessage 的现有项目

问题:这是 ews-java-api 不接受 EmailMessage 作为附件类型的问题吗?

我尝试了什么:我用ItemAttachment itemAttachment = approvalMessage.getAttachments().addItemAttachment(Item.class);替换了ItemAttachment itemAttachment = approvalMessage.getAttachments().addItemAttachment(EmailMessage.class);

发布此更改,我不再观察到上述错误。但是,代码现在会在以下行引发 stackoverflow 错误 - attachedItem.setMimeContent(mimeContent);

错误堆栈跟踪-

Oct 09, 2018 9:02:45 PM org.apache.catalina.core.StandardWrapperValve invoke
SEVERE: Servlet.service() for servlet [dispatcher] in context with path [] 
threw exception [Handler dispatch failed; nested exception is 
java.lang.StackOverflowError] with root cause
java.lang.StackOverflowError
at microsoft.exchange.webservices.data.core.PropertyBag.changed
(PropertyBag.java:364)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
itemChanged(ItemAttachment.java:96)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
serviceObjectChanged(ItemAttachment.java:252)
at microsoft.exchange.webservices.data.core.service.ServiceObject.
changed(ServiceObject.java:85)
at microsoft.exchange.webservices.data.core.PropertyBag.changed
(PropertyBag.java:364)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
itemChanged(ItemAttachment.java:96)
at microsoft.exchange.webservices.data.property.complex.ItemAttachment.
serviceObjectChanged(ItemAttachment.java:252)
at microsoft.exchange.webservices.data.core.service.ServiceObject.
changed(ServiceObject.java:85)

问题:为什么将Item指定为附件类会导致stackoverflow错误?将现有项目添加为附件的正确方法是什么?

【问题讨论】:

    标签: exchange-server exchangewebservices


    【解决方案1】:

    StackOverflowError扩展了VirtualMachineError类,表示JVM坏了,或者资源耗尽无法操作。

    更多信息,您可以参考:

    java.lang.StackOverflowError – How to solve StackOverflowError

    您可以添加现有项目作为附件使用下面的代码:

    FolderId  folderid= new FolderId(WellKnownFolderName.Inbox,"MailboxName");    
    Folder Inbox = Folder.Bind(service,folderid);  
    ItemView ivItemView =  new ItemView(1) ;     
    FindItemsResults<Item> fiItems = service.FindItems(Inbox.Id,ivItemView);
    if(fiItems.Items.Count == 1){  
    EmailMessage mail = new EmailMessage(service);   
    EmailMessage OriginalEmail = (EmailMessage)fiItems.Items[0];
    PropertySet  psPropset= new PropertySet(BasePropertySet.IdOnly);    
    psPropset.Add(ItemSchema.MimeContent);
    psPropset.Add(ItemSchema.Subject);
    OriginalEmail.Load(psPropset);  
    ItemAttachment Attachment = mail.Attachments.AddItemAttachment<EmailMessage>();
    Attachment.Item.MimeContent = OriginalEmail.MimeContent;  
    ExtendedPropertyDefinition PR_Flags = new ExtendedPropertyDefinition(3591, MapiPropertyType.Integer);    
    Attachment.Item.SetExtendedProperty(PR_Flags,"1");    
    Attachment.Name = OriginalEmail.Subject;  
    mail.Subject = "See the Attached Email";  
    mail.ToRecipients.Add("glen.scales@domain.com");
    mail.SendAndSaveCopy();     
    

    您不能直接发送另一条消息,您需要使用原始消息的 MimeContent,然后在此基础上创建一个 ItemAttachment。

    参考:

    Create an email using EWS Exchange and attach another email

    【讨论】:

    • 我已经参考了 Glen 对这个问题的回答(正如我在我的问题中提到的那样)并基于它编写了我的 Java 代码。
    • 你可以参考这个链接:stackoverflow.com/questions/28450984/…
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-02-18
    • 2019-12-05
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 2018-01-25
    • 2020-11-07
    相关资源
    最近更新 更多