Forums

Home » Liferay Portal » English » Liferay Legacy

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
Toch d Ninja
sending mail programmatically using liferay mail api
April 27, 2007 10:16 AM
Answer

Toch d Ninja

Rank: Junior Member

Posts: 91

Join Date: October 30, 2006

Recent Posts

Hi

am developing a project on liferay 4.2.1+tomcat, everything works fine

but now, i need to send a mail programmaticaly using liferay mail api..

can anyone show me how to achieve this!

Toch!
Toch d Ninja
RE: sending mail programmatically using liferay mail api
May 7, 2007 6:17 AM
Answer

Toch d Ninja

Rank: Junior Member

Posts: 91

Join Date: October 30, 2006

Recent Posts

Hi,

av NOT been able to send mail programmatically using the new liferay 4.2 mail API..

i need help on this
Toch d Ninja
RE: sending mail programmatically using liferay mail api
May 15, 2007 8:16 AM
Answer

Toch d Ninja

Rank: Junior Member

Posts: 91

Join Date: October 30, 2006

Recent Posts

Hi,

i am still waiting for help on this issue!

i need to programmatically send mails in batches with java mail api inside the new liferay?

Toch
Brett Swaim
RE: sending mail programmatically using liferay mail api
May 15, 2007 3:56 PM
Answer

Brett Swaim

LIFERAY STAFF

Rank: Regular Member

Posts: 112

Join Date: April 13, 2006

Recent Posts

Here is an example of a newlsetter portlet I built a while back. This works in 4.x.x and will batch mail.

Basically this goes through groups, user groups, roles and makes a hash of all the email addresses, then shoots out an email to everybody.

  1package com.liferay.portlet.newsletter.action;
  2
  3import java.util.ArrayList;
  4import java.util.Arrays;
  5import java.util.HashSet;
  6import java.util.LinkedHashMap;
  7import java.util.List;
  8import java.util.Set;
  9
 10import com.liferay.portal.DuplicateGroupException;
 11import com.liferay.portal.GroupFriendlyURLException;
 12import com.liferay.portal.GroupNameException;
 13import com.liferay.portal.NoSuchGroupException;
 14import com.liferay.portal.RequiredGroupException;
 15import com.liferay.portal.model.Group;
 16import com.liferay.portal.model.Role;
 17import com.liferay.portal.model.User;
 18import com.liferay.portal.model.UserGroup;
 19import com.liferay.portal.security.auth.PrincipalException;
 20import com.liferay.portal.service.GroupLocalServiceUtil;
 21import com.liferay.portal.service.GroupServiceUtil;
 22import com.liferay.portal.service.RoleLocalServiceUtil;
 23import com.liferay.portal.service.UserGroupLocalServiceUtil;
 24import com.liferay.portal.service.UserLocalServiceUtil;
 25import com.liferay.portal.service.persistence.RoleFinder;
 26import com.liferay.portal.service.persistence.UserUtil;
 27import com.liferay.portal.struts.PortletAction;
 28import com.liferay.portal.util.Constants;
 29import com.liferay.portal.util.PortalUtil;
 30import com.liferay.portal.util.WebKeys;
 31import com.liferay.portal.util.comparator.ContactLastNameComparator;
 32import com.liferay.util.ParamUtil;
 33import com.liferay.util.Validator;
 34import com.liferay.util.dao.hibernate.QueryUtil;
 35import com.liferay.util.mail.MailEngine;
 36import com.liferay.util.servlet.SessionErrors;
 37
 38import javax.mail.internet.InternetAddress;
 39import javax.portlet.ActionRequest;
 40import javax.portlet.ActionResponse;
 41import javax.portlet.PortletConfig;
 42import javax.portlet.RenderRequest;
 43import javax.portlet.RenderResponse;
 44
 45import org.apache.struts.action.ActionForm;
 46import org.apache.struts.action.ActionForward;
 47import org.apache.struts.action.ActionMapping;
 48
 49public class SendEmail extends PortletAction {
 50
 51    public void processAction(
 52            ActionMapping mapping, ActionForm form, PortletConfig config,
 53            ActionRequest req, ActionResponse res)
 54        throws Exception {
 55       
 56        String[] userIds = req.getParameterValues("selected-users");
 57        String[] userGroupIds = req.getParameterValues("selected-usergroups");
 58        String[] groupIds = req.getParameterValues("selected-groups");
 59        String[] roleIds = req.getParameterValues("selected-roles");
 60       
 61        List addresses = new ArrayList();
 62       
 63        List tempList = new ArrayList();
 64       
 65        LinkedHashMap userParams = new LinkedHashMap();
 66       
 67        // Get all users in the selected groups
 68        if (userGroupIds != null) {
 69            for (int i = 0; i < userGroupIds.length; i++) {
 70            
 71                UserGroup UG = UserGroupLocalServiceUtil.getUserGroup(userGroupIds[i]);
 72            
 73                userParams.put("usersUserGroups", UG.getUserGroupId());
 74
 75                tempList = UserLocalServiceUtil.search(PortalUtil.getCompanyId(req), null, null, null, null, true, userParams, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
 76                   
 77                for (int j = 0; j < tempList.size(); j++) {
 78                    addresses.add(tempList.get(j));
 79                }
 80            }
 81        }
 82        userParams = new LinkedHashMap();
 83        // now communities
 84        if (groupIds != null) {
 85            for (int i = 0; i < groupIds.length; i++) {
 86                System.out.println(groupIds[i]);
 87                Group group = GroupLocalServiceUtil.getUserGroup(PortalUtil.getCompanyId(req),groupIds[i]);
 88            
 89                userParams.put("usersGroups", String.valueOf(group.getGroupId()));
 90               
 91                tempList = UserLocalServiceUtil.search(PortalUtil.getCompanyId(req), null, null, null, null, true, userParams, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
 92                               
 93                for (int j = 0; j < tempList.size(); j++) {
 94                    addresses.add(tempList.get(j));
 95                }
 96            }
 97        }
 98       
 99        if (roleIds != null) {
100            for (int i = 0; i < roleIds.length; i++) {
101               
102                Role role = RoleLocalServiceUtil.getRole(roleIds[i]);
103            
104                userParams.put("usersRoles", role.getRoleId());
105               
106                tempList = UserLocalServiceUtil.search(PortalUtil.getCompanyId(req), null, null, null, null, true, userParams, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
107               
108                for (int j = 0; j < tempList.size(); j++) {
109                    addresses.add(tempList.get(j));
110                }
111            }
112        }
113        if (userIds != null) {
114            for (int i = 0; i < userIds.length; i++) {
115                addresses.add(UserLocalServiceUtil.getUserById(userIds[i]));
116            }
117        }
118       
119        //remove dups
120       
121        Set set = new HashSet();
122        set.addAll(addresses);
123       
124        if(set.size() < addresses.size()) {
125            addresses.clear();
126            addresses.addAll(set);
127        }
128       
129        InternetAddress[] bcc = new InternetAddress[addresses.size()];
130       
131        for (int i = 0; i < addresses.size(); i++) {
132            User communityUser = (User)addresses.get(i);
133            bcc[i] = new InternetAddress(communityUser.getEmailAddress(), communityUser.getEmailAddress());
134        }
135       
136        User user = PortalUtil.getUser(req);
137
138        InternetAddress from = new InternetAddress(user.getEmailAddress(), user.getFullName());
139
140        String body = ParamUtil.getString(req, "body");
141        String subject = ParamUtil.getString(req, "subject");
142       
143        MailEngine.send(from, null, null, bcc, subject, body);
144
145    }
146
147    public ActionForward render(
148            ActionMapping mapping, ActionForm form, PortletConfig config,
149            RenderRequest req, RenderResponse res)
150        throws Exception {
151
152        return mapping.findForward(
153            getForward(req, "portlet.newsletter.send_email"));
154    }
155}
Ryan T
RE: sending mail programmatically using liferay mail api
August 6, 2009 5:41 PM
Answer

Ryan T

Rank: Junior Member

Posts: 94

Join Date: October 30, 2008

Recent Posts

Anyway to release this to the community to have work start on it? It is a great part of the user control experience.
Jakob Hohlfeld
RE: sending mail programmatically using liferay mail api
January 15, 2013 4:00 AM
Answer

Jakob Hohlfeld

Rank: New Member

Posts: 10

Join Date: October 8, 2012

Recent Posts

Hi Brett,

in your code below you are using MailEngine to send emails. How do you know whether sending of an email failed or not?

Brett Swaim:
Here is an example of a newlsetter portlet I built a while back. This works in 4.x.x and will batch mail.

Basically this goes through groups, user groups, roles and makes a hash of all the email addresses, then shoots out an email to everybody.

1
2[..]
3        MailEngine.send(from, null, null, bcc, subject, body);
4[..]