Not able to send mail from liferay hook

email, hook, liferay, sendmail

Solution

My best guess is that you have multiple versions of mail.jar (or the various other incarnations that contain the same code) on your classpath. You said, you have found mail.jar in tomcat - I guess it's also contained in your webapplication's WEB-INF/lib: Remove it from there, your webapp must use the one from tomcat's global classpath.

You can configure the dependency in Maven as "provided", so that Maven knows that you will depend on that code, but not deploy it with your application

Bringing in your comment: Don't just "redeploy", but undeploy (delete the webapplication) because otherwise tomcat might keep your libraries that you previously provided. Thus you wouldn't copy them again, but they'd still be there.

Problem

I am trying to send email from a hook I have using both MailEngine and MailServiceUtil, but I have not yet been able to send it. My code is as below : ``` InternetAddress from=null; InternetAddress to=null; try { from = new InternetAddress("test@test.com", "Admin admin"); to = new InternetAddress(newUser.getEmailAddress(), newUser.getFirstName()); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } MailMessage mailMessage = new MailMessage(); mailMessage.setFrom(from); mailMessage.setTo(to); mailMessage.setBody(body); mailMessage.setSubject(subject); MailServiceUtil.sendEmail(mailMessage); ``` In this I am getting the following error, ``` java.lang.LinkageError: loader constraint violation: when resolving method "com.liferay.portal.kernel.mail.MailMessage.setFrom(Ljavax/mail/internet/InternetAddress;)V" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/lftechnology/sbworkbench/hooks/createaccount/CustomCreateAccountAction, and the class loader (instance of org/apache/catalina/loader/StandardClassLoader) for resolved class, com/liferay/portal/kernel/mail/MailMessage, have different Class objects for the type javax/mail/internet/InternetAddress used in the signature ``` I found a mail.jar file in my liferay installation but I am not able to use it. I tried importing javax.mail in my pom file with artifactid com.sun but still get this error. Does anybody here know what the problem is ? Thanks

Original source