Foros de discusión

sending mail programmatically using liferay mail api

Toch d Ninja, modificado hace 16 años.

sending mail programmatically using liferay mail api

Regular Member Mensajes: 105 Fecha de incorporación: 30/10/06 Mensajes recientes
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, modificado hace 16 años.

RE: sending mail programmatically using liferay mail api

Regular Member Mensajes: 105 Fecha de incorporación: 30/10/06 Mensajes recientes
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, modificado hace 16 años.

RE: sending mail programmatically using liferay mail api

Regular Member Mensajes: 105 Fecha de incorporación: 30/10/06 Mensajes recientes
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
thumbnail
Brett Swaim, modificado hace 16 años.

RE: sending mail programmatically using liferay mail api

Regular Member Mensajes: 112 Fecha de incorporación: 14/04/06 Mensajes recientes
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.

package com.liferay.portlet.newsletter.action;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Set;

import com.liferay.portal.DuplicateGroupException;
import com.liferay.portal.GroupFriendlyURLException;
import com.liferay.portal.GroupNameException;
import com.liferay.portal.NoSuchGroupException;
import com.liferay.portal.RequiredGroupException;
import com.liferay.portal.model.Group;
import com.liferay.portal.model.Role;
import com.liferay.portal.model.User;
import com.liferay.portal.model.UserGroup;
import com.liferay.portal.security.auth.PrincipalException;
import com.liferay.portal.service.GroupLocalServiceUtil;
import com.liferay.portal.service.GroupServiceUtil;
import com.liferay.portal.service.RoleLocalServiceUtil;
import com.liferay.portal.service.UserGroupLocalServiceUtil;
import com.liferay.portal.service.UserLocalServiceUtil;
import com.liferay.portal.service.persistence.RoleFinder;
import com.liferay.portal.service.persistence.UserUtil;
import com.liferay.portal.struts.PortletAction;
import com.liferay.portal.util.Constants;
import com.liferay.portal.util.PortalUtil;
import com.liferay.portal.util.WebKeys;
import com.liferay.portal.util.comparator.ContactLastNameComparator;
import com.liferay.util.ParamUtil;
import com.liferay.util.Validator;
import com.liferay.util.dao.hibernate.QueryUtil;
import com.liferay.util.mail.MailEngine;
import com.liferay.util.servlet.SessionErrors;

import javax.mail.internet.InternetAddress;
import javax.portlet.ActionRequest;
import javax.portlet.ActionResponse;
import javax.portlet.PortletConfig;
import javax.portlet.RenderRequest;
import javax.portlet.RenderResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

public class SendEmail extends PortletAction {

	public void processAction(
			ActionMapping mapping, ActionForm form, PortletConfig config,
			ActionRequest req, ActionResponse res)
		throws Exception {
		
		String[] userIds = req.getParameterValues("selected-users");
		String[] userGroupIds = req.getParameterValues("selected-usergroups");
		String[] groupIds = req.getParameterValues("selected-groups");
		String[] roleIds = req.getParameterValues("selected-roles");
		
		List addresses = new ArrayList();
		
		List tempList = new ArrayList();
		
		LinkedHashMap userParams = new LinkedHashMap();
		
		// Get all users in the selected groups
		if (userGroupIds != null) {
			for (int i = 0; i < userGroupIds.length; i++) {
			
				UserGroup UG = UserGroupLocalServiceUtil.getUserGroup(userGroupIds[i]);
			
				userParams.put("usersUserGroups", UG.getUserGroupId());

				tempList = UserLocalServiceUtil.search(PortalUtil.getCompanyId(req), null, null, null, null, true, userParams, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
					
				for (int j = 0; j < tempList.size(); j++) {
					addresses.add(tempList.get(j));
				}
			}
		}
		userParams = new LinkedHashMap();
		// now communities
		if (groupIds != null) {
			for (int i = 0; i < groupIds.length; i++) {
				System.out.println(groupIds[i]);
				Group group = GroupLocalServiceUtil.getUserGroup(PortalUtil.getCompanyId(req),groupIds[i]);
			
				userParams.put("usersGroups", String.valueOf(group.getGroupId()));
				
				tempList = UserLocalServiceUtil.search(PortalUtil.getCompanyId(req), null, null, null, null, true, userParams, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
								
				for (int j = 0; j < tempList.size(); j++) {
					addresses.add(tempList.get(j));
				}
			}
		}
		
		if (roleIds != null) {
			for (int i = 0; i < roleIds.length; i++) {
				
				Role role = RoleLocalServiceUtil.getRole(roleIds[i]);
			
				userParams.put("usersRoles", role.getRoleId());
				
				tempList = UserLocalServiceUtil.search(PortalUtil.getCompanyId(req), null, null, null, null, true, userParams, true, QueryUtil.ALL_POS, QueryUtil.ALL_POS, null);
				
				for (int j = 0; j < tempList.size(); j++) {
					addresses.add(tempList.get(j));
				}
			}
		}
		if (userIds != null) {
			for (int i = 0; i < userIds.length; i++) {
				addresses.add(UserLocalServiceUtil.getUserById(userIds[i]));
			}
		}
		
		//remove dups
		
		Set set = new HashSet();
		set.addAll(addresses);
		
		if(set.size() < addresses.size()) {
			addresses.clear();
			addresses.addAll(set);
		}
		
		InternetAddress[] bcc = new InternetAddress[addresses.size()];
		
		for (int i = 0; i < addresses.size(); i++) {
			User communityUser = (User)addresses.get(i);
			bcc[i] = new InternetAddress(communityUser.getEmailAddress(), communityUser.getEmailAddress());
		}
		
		User user = PortalUtil.getUser(req);

		InternetAddress from = new InternetAddress(user.getEmailAddress(), user.getFullName());

		String body = ParamUtil.getString(req, "body");
		String subject = ParamUtil.getString(req, "subject");
		
		MailEngine.send(from, null, null, bcc, subject, body);

	}

	public ActionForward render(
			ActionMapping mapping, ActionForm form, PortletConfig config,
			RenderRequest req, RenderResponse res)
		throws Exception {

		return mapping.findForward(
			getForward(req, "portlet.newsletter.send_email"));
	}
}
thumbnail
Ryan Terwedo, modificado hace 14 años.

RE: sending mail programmatically using liferay mail api

Junior Member Mensajes: 94 Fecha de incorporación: 31/10/08 Mensajes recientes
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, modificado hace 11 años.

RE: sending mail programmatically using liferay mail api

New Member Mensajes: 10 Fecha de incorporación: 8/10/12 Mensajes recientes
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.


[..]
		MailEngine.send(from, null, null, bcc, subject, body);
[..]