掲示板

Liferay 7 - Email API

thumbnail
6年前 に Mohammed Rawoof Shaik によって更新されました。

Liferay 7 - Email API

Junior Member 投稿: 37 参加年月日: 16/09/25 最新の投稿
Hello LR Community,

Background:
I'm developing a portlet to send out emails to users in the portal. I followed the below guides,

Below is my code,
// List of imports
import java.util.List;

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

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
import org.springframework.web.portlet.bind.annotation.ActionMapping;
import org.springframework.web.portlet.bind.annotation.RenderMapping;

import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.exception.SystemException;
import com.liferay.portal.kernel.log.Log;
import com.liferay.portal.kernel.log.LogFactoryUtil;
import com.liferay.portal.kernel.mail.MailMessage;
import com.liferay.portal.model.User;
import com.liferay.portal.service.UserLocalServiceUtil;

// Code inside the function
MailMessage mailMessage = new MailMessage();
mailMessage.setHTMLFormat(true);

List<user> lUser = UserLocalServiceUtil.getUsers(0, UserLocalServiceUtil.getUsersCount());
_logger.debug("-- lUser.size - " + lUser.size());
InternetAddress[] toIA = new InternetAddress[lUser.size()];
int i = 0;
for (User user : lUser) {
	toIA[i] = new InternetAddress(user.getEmailAddress());
	i++;
}
mailMessage.setTo(toIA); // Line 65: Error occurs here
_logger.debug("-- to - " + mailMessage.getTo());

mailMessage.setFrom(new InternetAddress("no-reply@utoronto.ca"));
_logger.debug("-- from - " + mailMessage.getFrom().getAddress());

String subject = actionRequest.getParameter("subject");
mailMessage.setSubject(subject);
_logger.debug("-- subject - " + mailMessage.getSubject());

String body = actionRequest.getParameter("body");
mailMessage.setBody(body);
_logger.debug("-- body - " + mailMessage.getBody());

MailServiceUtil.sendEmail(mailMessage);</user>

The logs have the below error,
16:51:29,252 ERROR [http-nio-8080-exec-8][render_portlet_jsp:131] null
java.lang.LinkageError: loader constraint violation: when resolving method "com.liferay.mail.kernel.model.MailMessage.setTo([Ljavax/mail/internet/InternetAddress;)V" the class loader (instance of org/eclipse/osgi/internal/loader/EquinoxClassLoader) of the current class, ca/utoronto/outage/EmailOutageController, and the class loader (instance of java/net/URLClassLoader) for the method's defining class, com/liferay/mail/kernel/model/MailMessage, have different Class objects for the type [Ljavax/mail/internet/InternetAddress; used in the signature
	at com.package.outage.EmailOutageController.sendEmailOutage(EmailOutageController.java:65)
	at com.liferay.portlet.FilterChainImpl.doFilter(FilterChainImpl.java:71)
	at com.liferay.portal.kernel.portlet.PortletFilterUtil.doFilter(PortletFilterUtil.java:48)
	at com.liferay.portal.kernel.servlet.PortletServlet.service(PortletServlet.java:107)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
	at com.liferay.portal.osgi.web.wab.extender.internal.adapter.ServletExceptionAdapter.service(ServletExceptionAdapter.java:68)
	at org.eclipse.equinox.http.servlet.internal.registration.EndpointRegistration.service(EndpointRegistration.java:153)
	at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:50)
	at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilterChain.doFilter(InvokerFilterChain.java:119)
	at com.liferay.portal.kernel.servlet.filters.invoker.InvokerFilter.doFilter(InvokerFilter.java:99)
	at com.liferay.portal.osgi.web.wab.extender.internal.adapter.FilterExceptionAdapter.doFilter(FilterExceptionAdapter.java:46)
	at org.eclipse.equinox.http.servlet.internal.registration.FilterRegistration.doFilter(FilterRegistration.java:121)
	at org.eclipse.equinox.http.servlet.internal.servlet.FilterChainImpl.doFilter(FilterChainImpl.java:45)
	at org.eclipse.equinox.http.servlet.internal.servlet.ResponseStateHandler.processRequest(ResponseStateHandler.java:70)
	at org.eclipse.equinox.http.servlet.internal.context.DispatchTargets.doDispatch(DispatchTargets.java:117)
	at org.eclipse.equinox.http.servlet.internal.servlet.RequestDispatcherAdaptor.include(RequestDispatcherAdaptor.java:48)
	at com.liferay.portlet.InvokerPortletImpl.invoke(InvokerPortletImpl.java:530)
	at com.liferay.portlet.InvokerPortletImpl.invokeAction(InvokerPortletImpl.java:576)
	at com.liferay.portlet.InvokerPortletImpl.processAction(InvokerPortletImpl.java:334)
	at com.liferay.portal.monitoring.internal.portlet.MonitoringInvokerPortlet.processAction(MonitoringInvokerPortlet.java:189)

The dependency I have in the pom.xml is below,
<dependency>
	<groupid>com.sun.mail</groupid>
	<artifactid>javax.mail</artifactid>
	<version>1.6.0</version>
</dependency>


How do I go about resolving this?
6年前 に Victor Soares によって更新されました。

RE: Liferay 7 - Email API

New Member 投稿: 8 参加年月日: 15/08/06 最新の投稿
Your issue seems to be located on the wrong version number used here:

1<dependency>
2 <groupId>com.sun.mail</groupId>
3 <artifactId>javax.mail</artifactId>
4 <version>1.6.0</version>
5</dependency>

You just need to match with Liferay's one. 1.2 if I recall correctly
thumbnail
6年前 に Mohammed Rawoof Shaik によって更新されました。

RE: Liferay 7 - Email API (回答)

Junior Member 投稿: 37 参加年月日: 16/09/25 最新の投稿
Hi Victor,

Thanks for the reply. I changed my dependency as below and it worked,
<dependency>
	<groupid>javax.mail</groupid>
	<artifactid>javax.mail-api</artifactid>
	<version>1.6.0</version>
	<scope>provided</scope>
</dependency>