Foros de discusión

How to send email from custom portlet

anushesh sinha, modificado hace 14 años.

How to send email from custom portlet

New Member Mensajes: 6 Fecha de incorporación: 13/10/09 Mensajes recientes
Hi Every one

I am trying to send email from my custom portlet using the code
String subject = "http://[$PORTAL_URL$]: Your New Account";


String body = "Dear [$TO_NAME$],<br /><br /> "
+ "Welcome! You recently created an account at http://[$PORTAL_URL$]. "
+ "Your password is [$USER_PASSWORD$]. Enjoy!<br /><br /> "
+ "Sincerely,<br />"
+ "[$FROM_NAME$]<br />"
+ "[$FROM_ADDRESS$]<br />"
+ "http://[$PORTAL_URL$]<br />";


subject = StringUtil.replace(
subject,
new String[] {
"[$FROM_ADDRESS$]",
"[$FROM_NAME$]",
"[$PORTAL_URL$]",
"[$TO_ADDRESS$]",
"[$TO_NAME$]",
"[$USER_ID$]",
"[$USER_PASSWORD$]",
"[$USER_SCREENNAME$]"
},
new String[] {
"liferay.test2@testdomain.com",
"anushesh",
"localhost",
"liferay.test3@testdomain.com",
"gatewayPark",
"10614",
"bruno",
"bruno"
});
System.out.println("for testing email1 "+ subject);
body = StringUtil.replace(
body,
new String[] {
"[$FROM_ADDRESS$]",
"[$FROM_NAME$]",
"[$PORTAL_URL$]",
"[$TO_ADDRESS$]",
"[$TO_NAME$]",
"[$USER_ID$]",
"[$USER_PASSWORD$]",
"[$USER_SCREENNAME$]"
},
new String[] {
"liferay.test2@testdomain.com",
"anushesh",
"localhost",
"liferay.test3@testdomain.com",
"gatewayPark",
"10614",
"bruno",
"bruno"
});
System.out.println("for testing email2 "+ body);

//Class iAddrClass = this.getClass().getClassLoader().loadClass("javax.mail.internet.InternetAddress");


InternetAddress from = new InternetAddress("liferay.test2@testdomain.com", "anushesh");
InternetAddress to = new InternetAddress("liferay.test3@testdomain.com", "gatewayPark");

MailMessage message = new MailMessage(from, to, subject, body, true);
System.out.println("for testing email3 "+ message);
MailServiceUtil.sendEmail(message);
System.out.println("for testing email4 + mail send" );

But i am getting the following error

ERROR [jsp:165] java.lang.LinkageError: loader constraint violation: when resolving method "com.liferay.portal.kernel.mail.MailMessage.<init>(Ljavax/mail/internet/InternetAddress;Ljavax/mail/internet/InternetAddress;Ljava/lang/String;Ljava/lang/String;Z)V" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/test/WebApplication3, 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
at com.test.WebApplication3.processAction(WebApplication3.java:91)
thumbnail
Jonas Yuan, modificado hace 14 años.

RE: How to send email from custom portlet

Liferay Master Mensajes: 993 Fecha de incorporación: 27/04/07 Mensajes recientes
Hi Anushesh,

You need to
1) set the mail engine
<resource name="mail/MailSession" auth="Container" type="javax.mail.Session" mail.imap.host="imap.gmail.com" mail.imap.port="993" mail.pop.host="pop.gmail.com" mail.store.protocol="imap" mail.transport.protocol="smtp" mail.smtp.host="smtp.gmail.com" mail.smtp.port="465" mail.smtp.auth="true" mail.smtp.starttls.enable="true" mail.smtp.user="user" password="password" mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory" />	

2) add code (MailServiceUtil) in custom portlet

import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;
...
try {
			User user = themeDisplay.getUser();
	
			String fromName = CommentsPropsValues.FROM_NAME;
			String fromAddress = CommentsPropsValues.FROM_ADDRESS;
	
			String subjectTemplate = ContentUtil.get(
				"com/ext/portlet/comment/dependencies/comments_subject.vm");
	
			String bodyTemplate = ContentUtil.get(
				"com/ext/portlet/comment/dependencies/comments_body.vm");
	
			String subject = VelocityUtil.evaluate(subjectTemplate, null);
	
			Map<string, object> variables = new HashMap<string, object>();
	
			variables.put("fullName", user.getFullName());
			variables.put("url", redirect);
			variables.put("text", text);
	
			String body = VelocityUtil.evaluate(bodyTemplate, variables);
	
			InternetAddress from = new InternetAddress(fromAddress, fromName);
	
			MailMessage mailMessage = new MailMessage();
			mailMessage.setFrom(from);
			mailMessage.setBody(body);
			mailMessage.setSubject(subject);
			
			String toNames = CommentsPropsValues.TO_NAMES;
			String toEmails = CommentsPropsValues.TO_EMAILS;
			if(toNames != null &amp;&amp; toEmails != null)
				mailMessage.setTo(getEmailAddresses(toNames, toEmails));
			
			String ccNames = CommentsPropsValues.CC_NAMES;
			String ccEmails = CommentsPropsValues.CC_EMAILS;
			if(ccNames != null &amp;&amp; ccEmails != null)
				mailMessage.setCC(getEmailAddresses(ccNames, ccEmails));
			
			String bccNames = CommentsPropsValues.BCC_NAMES;
			String bccEmails = CommentsPropsValues.BCC_EMAILS;
			if(bccNames != null &amp;&amp; bccEmails != null)
				mailMessage.setBCC(getEmailAddresses(bccNames, bccEmails));
			
			if(toNames != null &amp;&amp; toEmails != null)
				MailServiceUtil.sendEmail(mailMessage);
			else 
				_log.debug("Email settings of TO are incorrect.");
		}
		catch (Exception e) {
			_log.error("Email notification could not be sent", e);
		}
...
</string,></string,>


Abstracted from the book: Liferay Portal 5.2 Systems Development

Hope that it helps.

Thanks

Jonas Yuan
-----------------
The Author of Liferay Books:
Liferay Portal 5.2 Systems Development
Liferay Portal Enterprise Intranets
thumbnail
Alex Wallace, modificado hace 14 años.

RE: How to send email from custom portlet

Liferay Master Mensajes: 640 Fecha de incorporación: 5/11/07 Mensajes recientes
Also if your custom portlet is in it's own WAR make sure your web.xml has:

<resource-ref>
<res-ref-name>mail/MailSession</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
mahi kommi, modificado hace 14 años.

RE: How to send email from custom portlet

New Member Mensajes: 3 Fecha de incorporación: 11/02/10 Mensajes recientes
hi Anushesh,

did you solved ur problem with these changes? but I followed what jonas said still iam getting the same error.

Thanks in advance
mahi
thumbnail
Nidhi Singh, modificado hace 14 años.

RE: How to send email from custom portlet

Regular Member Mensajes: 155 Fecha de incorporación: 7/10/09 Mensajes recientes
mahi kommi:
hi Anushesh,

did you solved ur problem with these changes? but I followed what jonas said still iam getting the same error.

Thanks in advance
mahi


Hi,

can you tell me your problem?
did you configure mail seesion in portal-ext.properties file
mail.session.mail.pop3.host=localhost
mail.session.mail.pop3.password=
mail.session.mail.pop3.port=110
mail.session.mail.pop3.user=
mail.session.mail.smtp.auth=false
mail.session.mail.smtp.host=localhost
mail.session.mail.smtp.password=
mail.session.mail.smtp.port=25
mail.session.mail.smtp.user=
mail.session.mail.store.protocol=pop3
mail.session.mail.transport.protocol=smtp

if you provide correct smtp and pop3 value, it will be work because my custum portlet is working.

Thanks
Nidhi Singh
thumbnail
Kishore Shetty, modificado hace 14 años.

RE: How to send email from custom portlet

New Member Mensajes: 3 Fecha de incorporación: 31/03/10 Mensajes recientes
Hello,

I am developing Struts based Portlet in Plugin SDK. I did the following

portlet-ext.properties

# Email
mail.session.jndi.name=mail/MailSession

Root.xml

<Resource
name="mail/MailSession"
auth="Container"
type="javax.mail.Session"
mail.imap.host="imap.gmail.com"
mail.imap.port="993"
mail.pop.host="pop.gmail.com"
mail.store.protocol="imap"
mail.transport.protocol="smtp"
mail.smtp.host="smtp.gmail.com"
mail.smtp.port="465"
mail.smtp.auth="true"
mail.smtp.starttls.enable="true"
mail.smtp.user="user"
password="password"
mail.smtp.socketFactory.class="javax.net.ssl.SSLSocketFactory"
/>

web.xml
<resource-ref>
<res-ref-name>mail/MailSession</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>

Struts Action class:

package com.xyz.test.struts.actions;

import javax.mail.internet.InternetAddress;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

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

import com.liferay.mail.service.MailServiceUtil;
import com.liferay.portal.kernel.mail.MailMessage;


public class SendMailAction extends Action {


public ActionForward execute(
ActionMapping mapping, ActionForm form, HttpServletRequest request,
HttpServletResponse response)
throws Exception {
log.out("MailBegin");
InternetAddress from = new InternetAddress(getFromAddress());
InternetAddress to = new InternetAddress(getToAddress())
MailMessage mailMessage = new MailMessage();
mailMessage.setFrom(from);
mailMessage.setTo(to);

mailMessage.setBody("Hello");
mailMessage.setSubject("Hello");
MailServiceUtil.sendEmail(mailMessage);

log.out("Mail Sent");
return mapping.findForward("success");
}

}

Action class is inside the Portlet:

portlet.xml
<portlet>
<portlet-name>my-xyz-struts</portlet-name>
<display-name>My Xyz Struts</display-name>
<portlet-class>com.bosch.test.struts.portlet.XyzEncryptionPortlet</portlet-class>
<init-param>
<name>ServletContextProvider</name>
<value>com.liferay.util.bridges.struts.LiferayServletContextProviderWrapper</value>
</init-param>
<init-param>
<name>ViewPage</name>
<value>/portlet_action/boschstruts/view</value>
</init-param>
<expiration-cache>0</expiration-cache>

Portlet:
public class XYZEncryptionPortlet extends StrutsPortlet {

@Override
public void serveResource(ResourceRequest request, ResourceResponse response)
throws PortletException, IOException {
try {
CaptchaUtil.serveImage(request, response);
} catch (Exception e) {
System.out.println("Error");
}
super.serveResource(request, response);
}

}

Result:
No exception and system is not sending the email

Any help?. Thank you

regards,
Kishore
Joel Peterson, modificado hace 14 años.

RE: How to send email from custom portlet

New Member Mensajes: 22 Fecha de incorporación: 18/02/10 Mensajes recientes
There is also another way to send email messages assuming that your portal has been properly configured with an smtp server. Liferay exposes a MessageBusUtil class that will take messages and send them different ways and one of the types is a MailMessage. Here is some code that I pulled out of a plugin of mine that I have used a few times. I have only tested this on 5.2.3.

//the necessary imports
import com.liferay.portal.kernel.messaging.DestinationNames;
import com.liferay.portal.kernel.messaging.MessageBusUtil;
import com.liferay.portal.kernel.mail.MailMessage;

//the code to send the email
MailMessage mailMessage = new MailMessage();
mailMessage.setBody("Here is the message");
mailMessage.setFrom(new InternetAddress("someemailaddress@company.com"));
mailMessage.setSubject("Email Subject");
mailMessage.setTo(new InternetAddress("someemailaddress@company.com"));
MessageBusUtil.sendMessage(DestinationNames.MAIL, mailMessage);
thumbnail
Kishore Shetty, modificado hace 13 años.

RE: How to send email from custom portlet

New Member Mensajes: 3 Fecha de incorporación: 31/03/10 Mensajes recientes
Thank you. Mail is working. I have used EmailEngine.send(MimeMessage). Next step is to send the Encrypted mail. I feel I can take a refernce of EmailEngine source code and develop similar Utility. I welcome suggestions and opinion on the approach
thumbnail
Olaf Kock, modificado hace 14 años.

RE: How to send email from custom portlet

Liferay Legend Mensajes: 6400 Fecha de incorporación: 23/09/08 Mensajes recientes
This message sounds like classloader issues:

ERROR [jsp:165] java.lang.LinkageError: loader constraint violation: when resolving method "com.liferay.portal.kernel.mail.MailMessage.<init>(Ljavax/mail/internet/InternetAddress;Ljavax/mail/internet/InternetAddress;Ljava/lang/String;Ljava/lang/String;Z)V" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/test/WebApplication3, 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
at com.test.WebApplication3.processAction(WebApplication3.java:91)


I guess that the mail jars are deployed twice, e.g. once in the portal and once in your application/portlet/plugin. Check if your portlet *.war file contains mail.jar or one of the fragments of javamail. If so, remove it.
mahi kommi, modificado hace 14 años.

RE: How to send email from custom portlet

New Member Mensajes: 3 Fecha de incorporación: 11/02/10 Mensajes recientes
hi,


Thanks.Finally iam getting mails.The problem is with jars.But the mail is collected in spam folder and its taking too long to send the email.can u do anything for this problem?





Thanks inadvance
mahi.
thumbnail
Olaf Kock, modificado hace 14 años.

RE: How to send email from custom portlet

Liferay Legend Mensajes: 6400 Fecha de incorporación: 23/09/08 Mensajes recientes
It's not Liferay, that delivers to your spam folder: It's your mailserver. Based on some criteria it believes that the mail is spam. If your mailserver used spamassassin and tags the mail, you might want to analyse the mail headers, where the tags are stored. This usually gives a good overview of what is triggering the spam detection. It might be that your Liferay server is not "authorized" to send mail for this domain, it might be vocabulary, but there's no point in guessing here: Just look at your mail headers (Thunderbird: Ctrl-U)

The time it takes until your mail appears might be attributed to some greylisting (look up this strategy), but this also is just a wild guess. Look at your logs, Liferay's as well as your mailserver's. Again, the mail headers contain information about where the mail has been for how long.
thumbnail
Enrique Valdes Lacasa, modificado hace 8 años.

RE: How to send email from custom portlet

Junior Member Mensajes: 92 Fecha de incorporación: 29/07/14 Mensajes recientes
In case it helps, I was getting the same error that anushesh sinha mentioned.:
ERROR loader constraint violation: when resolving method "com.liferay.portal.kernel.mail.MailMessage.setTo(Ljavax/mail/internet/InternetAddress;)V" the class loader (instance of org/apache/catalina/loader/WebappClassLoader) of the current class, com/MyClassl, 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 ernet/InternetAddress;)V used in the signature

In order to fix it, and thanks to the comment of Olaf Kock, this error looked like if the library was being loaded twice somehow. I realized I was adding the Javax Mail dependency in my pom as:
		<dependency>
			<groupid>javax.mail</groupid>
			<artifactid>mail</artifactid>
			<version>1.4</version>
		</dependency>

so what I did was changing this to:
		<dependency>
			<groupid>javax.mail</groupid>
			<artifactid>mail</artifactid>
			<version>1.4</version>
			<scope>provided</scope>
		</dependency>

And that fixed it!