Reputation: 398
I'm using emailBean from Tony McGuckin for sending an HTML email.
It works fine until you save and reopen the document; at that point the bean doesn't finds attachments.
Consider this scenario:
Runing the bean on debugmode I see it prints Adding Attachments...
But, it is not getting the attachment here EmbeddedObject eo = this.getDocument().getDocument().getAttachment(persistentName);
If I try to print persistentName
I get null
This is 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");
}
}
Upvotes: 3
Views: 148
Reputation: 3365
Attachments in the AttachmentValueHolder field is either persistent or not. A new attachment that has not been saved yet will have a persistentName
. Otherwise it will have a name
. You may add the following line to determine that:
if(StringUtil.isEmpty(persistentName)) {
persistentName=((AttachmentValueHolder)attachment).getName();
}
PS. So if persistentName is null, it won't be a persistent name anymore. I didn't change variable name not to ruin your code :)
Upvotes: 1