Hi,
I have a requirement to send the email notification to the group. How can that be done?
Solved! Go to Solution.
Views
Replies
Total Likes
You could fetch the users in a group and iterate through them to get their email ID. Below is a sample code to fetch the user email IDs.
UserManager userManager=jrSession.getUserManager();
Group group = (Group) userManager.getAuthorizable("GroupId");
log.info("group >>> "+group);
final Iterator<Authorizable> memberslist = group.getMembers();
while (memberslist.hasNext()) {
final Authorizable user = memberslist.next();
if (user != null && !user.isGroup() && user.getProperty("./profile/email") != null) {
//EMAIL ADDRESS FOR MEMBERS
Value[] usrEmail = user.getProperty("./profile/email");
for(Value emailGroup : usrEmail) {
log.info("emailGroup line 152>>> "+emailGroup);
}
}
}
Once you have the list of email ids you can use org.apache.commons.mail.HtmlEmail.addTo() to add the list of ids.
Let me know if you still face any issues.
You could fetch the users in a group and iterate through them to get their email ID. Below is a sample code to fetch the user email IDs.
UserManager userManager=jrSession.getUserManager();
Group group = (Group) userManager.getAuthorizable("GroupId");
log.info("group >>> "+group);
final Iterator<Authorizable> memberslist = group.getMembers();
while (memberslist.hasNext()) {
final Authorizable user = memberslist.next();
if (user != null && !user.isGroup() && user.getProperty("./profile/email") != null) {
//EMAIL ADDRESS FOR MEMBERS
Value[] usrEmail = user.getProperty("./profile/email");
for(Value emailGroup : usrEmail) {
log.info("emailGroup line 152>>> "+emailGroup);
}
}
}
Once you have the list of email ids you can use org.apache.commons.mail.HtmlEmail.addTo() to add the list of ids.
Let me know if you still face any issues.
Hi @JeevanRaj ,
I have created a list of id's and trying to add it to the email.addTo() nut it's returning an exception. Is there any way create a list to add it to the email?
What is the exception you are getting?
I was able to set emails to the group with below sample code. Let me know if it helps.
try(ResourceResolver rr = request.getResourceResolver()) {
HtmlEmail email = new HtmlEmail();
email.addTo("receiver@example.com");
email.setSubject("test email subject");
email.setMsg("text email body");
email.setHtmlMsg("<html><head></head><body><p>html email body</p></body></html>");
email.setFrom("sender@example.com","TestUser");
List<String> emailIds = new ArrayList<>();
Session session = rr.adaptTo(Session.class);
UserManager userManager = AccessControlUtil.getUserManager(session);
Group group = (Group) userManager.getAuthorizable(<GROUP_NAME_HERE>);
final Iterator<Authorizable> memberslist = group.getMembers();
while (memberslist.hasNext()) {
final Authorizable user = memberslist.next();
if (user != null && !user.isGroup() && user.getProperty("./profile/email") != null) {
//EMAIL ADDRESS FOR MEMBERS
Value[] usrEmail = user.getProperty("./profile/email");
for(Value emailGroup : usrEmail) {
emailIds.add(emailGroup.getString());
}
}
}
String[] arr = emailIds.stream().toArray(String[] ::new);
email.addTo(arr);
// Declare a MessageGateway service
MessageGateway<Email> messageGateway;
messageGateway = messageGatewayService.getGateway(HtmlEmail.class);
messageGateway.send(email);
} catch(Exception e) {
e.printStackTrace();
}
It can be done in 2 ways
Hi DIPEN
can you please elaborate on the first case i.e. create DL for groups?
Thanks
Views
Replies
Total Likes
Views
Likes
Replies
Views
Likes
Replies