how to send mail using ActionExecuterAbstractBase?

cancel
Showing results for 
Search instead for 
Did you mean: 
dharmraj
Active Member

how to send mail using ActionExecuterAbstractBase?

I did this configuration 

email.server.enabled=true
email.server.port=25
email.server.domain=alfresco.com
email.inbound.unknownUser=anonymous
email.server.allowed.senders=.*

In Class 

Properties mailServerProperties = new Properties();
mailServerProperties = System.getProperties();
mailServerProperties.put("mail.smtp.host", "localhost");
mailServerProperties.put("mail.smtp.port", "25");
Session session = Session.getDefaultInstance(mailServerProperties, null);
session.setDebug(false);
Message message = new MimeMessage(session);
String fromAddress = "admin@alfresco.com";
message.setFrom(new InternetAddress(fromAddress));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("dharmrajgurjar1990@gmail.com"));
message.setSubject(subject);

// Create the message part with body text
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(body);
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);

// Create the Attachment part
//
// Get the document content bytes

multipart.addBodyPart(messageBodyPart);

// Put parts in message
message.setContent(multipart);

 Transport.send(message);

 i am sending email from : admin@alfresco.com  to dharmrajgurjar1990@gmail.com  
But i am getting this error:
org.alfresco.error.AlfrescoRuntimeException: 06090023 Could not send email: 554 The email address 'dharmrajgurjar1990@gmail.com' does not reference a valid accessible node.
8 Replies
abhinavmishra14
Advanced

Re: how to send mail using ActionExecuterAbstractBase?

Can you clarify whether you want to send emails without attachments or with attachments ?

There is an action OOTB which allows to send emails to a user or a group but attachment feature is not available as of now. 

This action class extends ActionExecuterAbstractBase as well: org.alfresco.repo.action.executer.MailActionExecuter

https://github.com/Alfresco/alfresco-repository/blob/master/src/main/java/org/alfresco/repo/action/e...

Here is sample code snippet using OOTB MailActionExecuter which may be helpful.

private String fromEmailAddress;
private String templateSpace;
private NodeService nodeService;
private ActionService actionService;
private PersonService personService;
private NamespaceService namespaceService;
private SearchService searchService;

//Setter and Getter methods of above items


public void sendEmailToGroupOrEmailId(final String subject,
final String templateString, final Map<String, Serializable> model,
final Serializable recipients) {
//Example snippet Send to group
final ArrayList<Serializable> recipientList = new ArrayList<Serializable>();
if (recipients instanceof ArrayList) {
recipientList.addAll((Collection<Serializable>) recipients);
} else {
recipientList.add(recipients);
}
final Action action = actionService.createAction(MailActionExecuter.NAME);
action.setParameterValue(MailActionExecuter.PARAM_TO_MANY, recipientList); //A group or email id.
action.setParameterValue(MailActionExecuter.PARAM_FROM, fromEmailAddress); //From address is configured in global properties
action.setParameterValue(MailActionExecuter.PARAM_SUBJECT, subject);
action.setParameterValue(MailActionExecuter.PARAM_TEMPLATE, getMailTemplate(templateString)); //Email template path. e.g. : /Company Home/Data Dictionary/Email Templates/my_operation_email.html.ftl
action.setParameterValue(MailActionExecuter.PARAM_TEMPLATE_MODEL, (Serializable) model); //a map containing parametrers you want to pass within email template.
actionService.executeAction(action, null);
}

//If you have person's user id, this method will get the email id via its person node and intitiate email action
public void sendEmailToUserId(final String uid, final String subject,
final String templateString, final String message,
final Map<String, Serializable> model) {
final NodeRef personNodeRef = personService.getPerson(uid);
final String email = (String) nodeService.getProperty(personNodeRef, ContentModel.PROP_EMAIL);
sendEmailToGroupOrEmailId(subject, templateString, model, email);
}

private String getMailTemplate(final String templateString) throws SendMailException {
String template = StringUtils.EMPTY;
if(templateString.indexOf('/') == -1) {
template = nodeService.getChildByName(getTemplatesSpace(), ContentModel.ASSOC_CONTAINS, templateString).toString();
} else {
template = templateString;
}
return template;
}

private NodeRef getTemplatesSpace() throws SendMailException {
if (templatesSpaceRef == null) {
templatesSpaceRef = selectNodeByDisplayPath(templateSpace);
}
return templatesSpaceRef;
}


public NodeRef selectNodeByDisplayPath(final String path)
throws InvalidNodeRefException, XPathException,
IllegalArgumentException {
String nodePath = path;
NodeRef nodeRef = null;
if (path.startsWith("/")) {
nodePath = path.substring(1);
}

final List<NodeRef> selectedNodes = searchService.selectNodes(nodeService.getRootNode(StoreRef.STORE_REF_WORKSPACE_SPACESSTORE),
nodePath, null, namespaceService, false);
if (selectedNodes != null && !selectedNodes.isEmpty()) {
nodeRef = selectedNodes.get(0);
}
return nodeRef;
}


Someone has implemented a custom MailExecutorAction by taking the reference of OOTB action class which can send the attachments as well. I have not tested it but you may want to try this: https://community.alfresco.com/docs/DOC-6574-send-html-mail-with-attachments

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
dharmraj
Active Member

Re: how to send mail  using  ActionExecuterAbstractBase?

Hi Abhinav,

i  want to send a mail using inbound ACS setting to any user .So  i used ActionExecuterAbstractBase to send a mail . 

abhinavmishra14
Advanced

Re: how to send mail  using  ActionExecuterAbstractBase?

Instead of creating traditional email sender logic/methods and implementing the action. I would suggest to use OOTB way of sending emails. It is also an action which can be executed synchronously/asynchronously. You can use a custom email template with this approach where you can pass some parameters, add custom logo and format as per your liking. It will also support sending email to an Alfresco Group. e.g. GROUP_Administrators. If you pass this value, all the users who are part of this group would receive emails. You don't have to extract each user's email id..

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
dharmraj
Active Member

Re: how to send mail  using  ActionExecuterAbstractBase?

Any configuration i have to do  for from , host and port  in alfresco global properties 

abhinavmishra14
Advanced

Re: how to send mail using ActionExecuterAbstractBase?

Yes. you would  need following configurations:

mail.host=smtp.xyz.com
mail.port=25
mail.encoding=UTF-8
mail.from.default=alfresco-local@xyz.com

You can bootstratp your email templates to /Company Home/Data Dictionary/Email Templates/ folder and get the template at run time while sending email. You can refer here for bootstrapping template: https://docs.alfresco.com/4.2/concepts/dev-extensions-modules-bootstrapping-files-spaces-xml.html

You can find the ootb email templates in /Company Home/Data Dictionary/Email Templates/ folder and you can take reference from any of the templates to create your custom template if needed. 

The sample method provided above: getMailTemplate(final String templateString) needs the name of the template file and it returns the full path for the method which uses the template for sending email.. 

e.g. getMailTemplate("my_operation_email.html.ftl")

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
dharmraj
Active Member

Re: how to send mail using ActionExecuterAbstractBase?


Action mailAction = actionService.createAction(MailActionExecuter.NAME);
mailAction.setExecuteAsynchronously(false);
mailAction.setParameterValue(MailActionExecuter.PARAM_TO, to);
mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, subject);

actionService.executeAction(mailAction, null);

I am not getting any error . But mail is not going 

abhinavmishra14
Advanced

Re: how to send mail  using  ActionExecuterAbstractBase?

Can you please check all log files including catalina.out log as well. Enable these debug logs and try again to see what error you can find:

log4j.logger.org.alfresco.repo.action=debug
log4j.logger.org.alfresco.repo.action.executer.MailActionExecuter=debug

Make sure you are not catching any generic exception somewhere which may not be logged. 

~Abhinav
(ACSCE, AWS SAA, Azure Admin)
sanjaybandhniya
Intermediate

Re: how to send mail using ActionExecuterAbstractBase?

To Send Email,you need to unable outbound email configuration.

https://docs.alfresco.com/6.1/concepts/email.html

Ex. 

mail.host=smtp.gmail.com
mail.port=465
mail.encoding=UTF-8
mail.username=xxx@gmail.com
mail.password=xxxx
mail.protocol=smtps

ActionService actionService = serviceRegistry.getActionService();

Action mailAction = actionService.createAction(MailActionExecuter.NAME);

mailAction.setParameterValue(MailActionExecuter.PARAM_SUBJECT, subject);

mailAction.setParameterValue(MailActionExecuter.PARAM_TO, email);

actionService.executeAction(mailAction, null);

xxx@gmail.com This account must be less sucure app.

https://myaccount.google.com/lesssecureapps

Thanks,

Sanjay Bandhniya

Contcentric