我正在使用emailBean from Tony McGuckin发送HTML电子邮件。
在您保存并重新打开文档之前,它可以正常工作;那时豆子找不到附件。
考虑这种情况:
在debugmode上运行bean我看到它打印出Add Attachments ...
但是,这里没有获得附件EmbeddedObject eo = this.getDocument().getDocument().getAttachment(persistentName);
如果我尝试打印persistentName
,我会得到空
这是emailBean:
// add attachments....
final List<FileRowData> attachments = this.getDocument().getAttachmentList(this.getFieldName());
if(null != attachments && !attachments.isEmpty()){
if(this.isDebugMode()){
System.out.println("Adding Attachments...");
}
for(FileRowData attachment : attachments) {
emailRootChild = emailRoot.createChildEntity();
if(null != emailRootChild && attachment instanceof AttachmentValueHolder){
InputStream is = null;
try {
String persistentName = ((AttachmentValueHolder)attachment).getPersistentName();
String cid = ((AttachmentValueHolder)attachment).getCID();
//Here is printing null for persistentName
System.out.println("Attachment: " + persistentName);
EmbeddedObject eo = this.getDocument().getDocument().getAttachment(persistentName);
if(null != eo){
emailHeader = emailRootChild.createHeader("Content-Disposition");
emailHeader.setHeaderVal("attachment; filename=\"" + persistentName + "\"");
emailHeader = emailRootChild.createHeader("Content-ID");
emailHeader.setHeaderVal("<" + cid + ">");
is = eo.getInputStream();
Stream stream = session.createStream();
stream.setContents(is);
emailRootChild.setContentFromBytes(stream, attachment.getType(), MIMEEntity.ENC_IDENTITY_BINARY);
if(this.isDebugMode()){
System.out.println("Added Attachment : " + persistentName);
}
}
} catch (Exception e) {
if(this.isDebugMode()){
System.out.println("Adding Attachment failed : " + e.getMessage());
}
throw e;
} finally {
if(null != is){
is.close();
is = null;
}
}
}
}
if(this.isDebugMode()){
System.out.println("Completed Adding Attachments");
}
}
答案 0 :(得分:1)
AttachmentValueHolder字段中的附件是持久性的还是非持久性的。尚未保存的新附件将包含persistentName
。否则它将有一个name
。您可以添加以下行来确定:
if(StringUtil.isEmpty(persistentName)) {
persistentName=((AttachmentValueHolder)attachment).getName();
}
PS。因此,如果persistentName为null,则它将不再是持久名称。我没有更改变量名称而不是破坏你的代码:)