Forums de discussion

How can I send mails with MailEngine ?

thumbnail
N. Belo, modifié il y a 14 années.

How can I send mails with MailEngine ?

Junior Member Publications: 33 Date d'inscription: 17/03/09 Publications récentes
Hi all,

I have my portal-ext.properties configured correctly to use an smtp server.
I've tested this connection and it works fine.

Now I want to use MailEngine.send(fromAddress, toAddress, subject, body)
to simply send an email. And the problem is that MailEngineException is not a Throwable exception, but a NestedException

try {
MailEngine.send(fromAddress, toAddress, subject, body);
} catch (MailEngineException ex) {
Logger.getLogger(MyClass.class.getName()).log(Level.SEVERE, null, ex);
}


I've found some workarounds for this, but they use InternetAdress class which doesn't exist in my javax.mail.*

I am using Liferay bundle with Tomcat 6 and the development is all made with liferay SDK.

Can someone help me in this simple task of sending an email?

Thank you.
N.
thumbnail
Tomas Polesovsky, modifié il y a 14 années.

RE: How can I send mails with MailEngine ?

Liferay Master Publications: 676 Date d'inscription: 13/02/09 Publications récentes
Hi,

MailEngineException is NestedException, which is Exception and Throwable (see NestedException javadoc).

InternetAddress is not in javax.mail package, but in javax.mail.internet package.

For full understanding of the MailEngine you can also look at the Liferay 5.2 MailEngine source.

-- tom
thumbnail
N. Belo, modifié il y a 14 années.

RE: How can I send mails with MailEngine ?

Junior Member Publications: 33 Date d'inscription: 17/03/09 Publications récentes
Hi Tom,

Thank you for your post. I've already had look into those documents, but I am still confused how to handle with MailEngine exceptions.

The InternetAddress class has to be imported from /tomcat6.0.18/lib/ext/mail.jar and when I use it I have this error in logs...

java.lang.LinkageError: Class javax/mail/internet/InternetAddress violates loader constraints

There is a post about this problem, but it was written 3 years ago and the solution is not applicable anymore.

java.lang.LinkageError Post

Can you put here a small sample of code about how to send emails in a portlet?

Thank you for your help.
BR,
N.
thumbnail
Corné A, modifié il y a 14 années.

RE: How can I send mails with MailEngine ?

Liferay Legend Publications: 1313 Date d'inscription: 03/10/06 Publications récentes
This works for me;


import javax.mail.internet.InternetAddress;
import javax.portlet.RenderRequest;

import org.apache.log4j.Logger;

import com.liferay.portal.kernel.mail.SMTPAccount;
import com.liferay.util.mail.MailEngine;
import com.liferay.util.mail.MailEngineException;

public class Emailer {
	private static final Logger logger = Logger.getLogger(Emailer.class);
	private static void send(
			InternetAddress from, InternetAddress[] to, InternetAddress[] cc,
			InternetAddress[] bcc, InternetAddress[] bulkAddresses,
			String subject, String body, boolean htmlFormat,
			InternetAddress[] replyTo, String messageId, String inReplyTo,
			File[] attachments)
		throws MailEngineException {
		
		SMTPAccount smtpAccount = null;
		
		MailEngine.send(
				from,to, cc,
				 bcc, bulkAddresses,
				subject, body, htmlFormat,
				replyTo, messageId, inReplyTo,
				attachments,smtpAccount);

	}	

	public static void sendEmail(final String from, final String to,
			final String bcc, final String subject, final String body,
			final File[] attachments) throws Exception {

		InternetAddress[] toS = new InternetAddress[] { new InternetAddress(to) };
		InternetAddress fromS = new InternetAddress(from);
		InternetAddress[] bccS = null;
		if (bcc != null)
			bccS = new InternetAddress[] { new InternetAddress(bcc) };

		InternetAddress[] cc = null;
		InternetAddress[] bulkAddresses = null;
		boolean htmlFormat = true;
		InternetAddress[] replyTo = null;
		String messageId = null;
		String inReplyTo = null;

		Emailer.send(fromS, toS, cc, bccS, bulkAddresses, subject, body,
				htmlFormat, replyTo, messageId, inReplyTo, attachments);
	}
}
thumbnail
N. Belo, modifié il y a 14 années.

RE: How can I send mails with MailEngine ?

Junior Member Publications: 33 Date d'inscription: 17/03/09 Publications récentes
Hi,

Thank you for reply. That looks good.
I believe that my problem is that I need to know which mail.jar contains InternetAddress class should I use to avoid the link problem.

Do you know where is the mail.jar that as defined the InternetAddress class and MailEngine is refering to? How about this Exception? (please, see attached image)


I am developing with Liferay SDK and using Tomcat 6. I've added the library mail.jar from /Tomcat-6.0.18/lib/ext/ or from liferay-sdk/lib/ to my portlet so I could compile my code using the InternetAddress class.

Then I've deployed the portlet, and when I try to use the InternetAddress class I get the link error that I've mentioned.

Thanks.
BR,
N.
thumbnail
N. Belo, modifié il y a 14 années.

RE: How can I send mails with MailEngine ?

Junior Member Publications: 33 Date d'inscription: 17/03/09 Publications récentes
Ok,

the Exception problem is fixed. All I had to do was to import the commons-lang.jar.
That's because the MailEngineException implementation depends of that class.

But now I have one compilation problem.

Compiling 1 source file to C:\Liferay-sdk\portlets\send-mail-portlet\build\web\WEB-INF\classes
C:\Liferay-sdk\portlets\send-mail-portlet\docroot\WEB-INF\src\com\sample\jsp\portlet\JSPPortlet.java:134: cannot access javax.mail.internet.InternetAddress
class file for javax.mail.internet.InternetAddress not found
MailEngine.send(from, to, subject, body);
1 error
C:\Liferay-sdk\portlets\send-mail-portlet\nbproject\build-impl.xml:417: The following error occurred while executing this line:
C:\Liferay-sdk\portlets\send-mail-portlet\nbproject\build-impl.xml:244: Compile failed; see the compiler error output for details.
BUILD FAILED (total time: 0 seconds)


I believe the usage of this library should be included in the compilation process and not imported. Does anyone have a clue what is the problem??

This problem was verified with a portlet builded using Netbeans(With Portal Pack Pluggin) and when I use Liferay-sdk.

Thanks,
N.
Abzal Amantaev, modifié il y a 12 années.

RE: How can I send mails with MailEngine ?

New Member Publications: 22 Date d'inscription: 16/05/11 Publications récentes
Hi
N. Belo!

I have the same error, if you solved this problem, please show me..
thumbnail
Tomas Polesovsky, modifié il y a 14 années.

RE: How can I send mails with MailEngine ?

Liferay Master Publications: 676 Date d'inscription: 13/02/09 Publications récentes
Hi
N. Belo:
The InternetAddress class has to be imported from /tomcat6.0.18/lib/ext/mail.jar and when I use it I have this error in logs...


And why do you need to do this? The /tomcat6.0.18/lib/ext/mail.jar already contains the javax/mail/internet/InternetAddress.class

Are you building your sources against the same version of the mail.jar? The problem with InternetAddress was with the tomcat 5.x.

-- tom