Expand my Community achievements bar.

Guidelines for the Responsible Use of Generative AI in the Experience Cloud Community.
SOLVED

How to send dam asset rendition as an attachment in email

Avatar

Level 3

Hi ,

I am trying to send a DAM Asset rendition as an attachment via mail.

I am sending mails using org.apache.commons.mail.HtmlEmail package.

If i use the below code

EmailAttachment attachment = new EmailAttachment();

attachment.setURL(new URL("http://localhost:4502/content/dam/geometrixx/new.jpg");
attachment.setDisposition(EmailAttachment.ATTACHMENT);
attachment.setDescription(assetName);
attachment.setName(assetName);

email.attach(attachment);

i am getting the error org.apache.sling.auth.core.impl.SlingAuthenticator getAnonymousResolver: Anonymous access not allowed by configuration - requesting credentials

 

If i use

 

File assetRendition = asset.adaptTo(File.class);
DataSource datasource = new FileDataSource(assetRendition);

 email.attach(datasource,"Image Name","Image Desc");

I am getting null pointer exception.

If anyone has already used , please let me know how to resolve this.

 

Thanks in advance

Harish

1 Accepted Solution

Avatar

Correct answer by
Employee

If you already have the Asset object, something like this should work:

Asset asset = // get asset Rendition rendition = // get rendition InputStream stream = rendition.getStream(); String mimeType = rendition.getMimeType() DataSource ds = new ByteArrayDataSource(stream, mimeType); email.attach(ds, "Image Name", "Image Desc");

View solution in original post

2 Replies

Avatar

Correct answer by
Employee

If you already have the Asset object, something like this should work:

Asset asset = // get asset Rendition rendition = // get rendition InputStream stream = rendition.getStream(); String mimeType = rendition.getMimeType() DataSource ds = new ByteArrayDataSource(stream, mimeType); email.attach(ds, "Image Name", "Image Desc");

Avatar

Level 10

I am working on Java code that uploads a file to CQ and saves the file to the CQ DAM. I am using a Sling Servlet and the JCR API to create app logic to save a file to the DAM. However - you still have to authenticate  using the JCR API from within a bundle. For example:  

 private String writeToDam(InputStream is, String fileName)

{
try
{
    session = this.repository.loginAdministrative(null);
    Node node = session.getNode("/content/dam/travel");  
    javax.jcr.ValueFactory valueFactory = session.getValueFactory();             
    javax.jcr.Binary contentValue = valueFactory.createBinary(is);            
    Node fileNode = node.addNode(fileName, "nt:file"); 
    fileNode.addMixin("mix:referenceable"); 
    Node resNode = fileNode.addNode("jcr:content", "nt:resource"); 
    resNode.setProperty("jcr:mimeType", "image/jpeg"); 
    resNode.setProperty("jcr:data", contentValue); 
    Calendar lastModified = Calendar.getInstance(); 
    lastModified.setTimeInMillis(lastModified.getTimeInMillis()); 
    resNode.setProperty("jcr:lastModified", lastModified); 
    session.save();            
     
     
    // Return the path to the document that was stored in CRX. 
    return fileNode.getPath();
}

 

So for your workflow -- instead of writing a file to the DAM, you want to read a file from the DAM. You still have to authenticate. TO see a complete example of how to authenticate from within a bundle and using the JCR API, see http://scottsdigitalcommunity.blogspot.ca/2013/02/querying-adobe-experience-manager-data.html .