Forums

Home » Liferay Portal » English » 3. Development

Combination View Flat View Tree View
Threads [ Previous | Next ]
toggle
François LE QUEMENER
MailEngine.send() not useable?
October 2, 2009 6:18 AM
Answer

François LE QUEMENER

Rank: Junior Member

Posts: 48

Join Date: September 18, 2009

Recent Posts

Hi,
I'm tryiong to send emails in Liferay. I use MailEngine.send(from, to, subject, body); but then Eclipse tells me there is an exception that is unhandled:
Unhandled exception type MailEngineException

Smart as I am, I add the try/catch block :
1        try {
2            MailEngine.send(from, to, subject, body);
3        } catch (MailEngineException e) {
4            // TODO Auto-generated catch block
5            e.printStackTrace();
6        }


Now, Eclipse tells me:
No exception of type MailEngineException can be thrown; an exception type must be a subclass of Throwable

I am developing this hook in an external eclipse project. I tried to copy the source file in the Liferay plugin SDK and it goes well. So I thought about a wrong version of util-java.jar, but I'm using the 5.2.2.

Do you see what's going on?

Thanks
Anonymous
RE: MailEngine.send() not useable?
October 2, 2009 6:34 AM
Answer

Anonymous

Hi francois,

Use the following method to send mail.

public static void send(String fromName, String fromAddress, String toName, String toAddress, String subject, String mailBody) {
InternetAddress from = null;
InternetAddress to = null;
try {
from = new InternetAddress(fromAddress, fromName);
to = new InternetAddress(toAddress, toName);
} catch (UnsupportedEncodingException usee) {
// ignore
}
if (from != null && to != null) {
MailMessage message = new MailMessage(from, to, subject, mailBody, true);
MailService mailService = (MailService) PortalBeanLocatorUtil.locate(MailService.class.getName() + ".impl");
mailService.sendEmail(message);
}
}
Using eclipse import necessary classes.

Note:
I don't no whether you have did mail configuration in portal-ext.properties file.
if not configure that then only mail will send.



Thanks in Advance,

V.C.Vijayan
Software Developer, TransIT mPower Labs (P) Ltd.
info@mpowerglobal.com
mPower Global Inc.
A Liferay expert company.
Pravin Pawar
RE: MailEngine.send() not useable?
April 5, 2010 7:49 AM
Answer

Pravin Pawar

Rank: Junior Member

Posts: 62

Join Date: November 17, 2009

Recent Posts

MailMessage message = new MailMessage(from, to, subject, mailBody, true);


How can we use MailService to send email for Multiple Recipient?

So I have used MailEngine class for that.
MailEngine.send(fromInternetAddress, toInternetAddresses, subject, body.toString()) method of MailEngine class.
Where toInternetAddresses is declared as an array of InternetAddress.

This method is working well with one of our custom portlet.
N. Belo
RE: MailEngine.send() not useable?
April 21, 2010 10:35 AM
Answer

N. Belo

Rank: Junior Member

Posts: 27

Join Date: March 16, 2009

Recent Posts

Hi,

where can I find InternetAddress class???

Thx,
NB
Pravin Pawar
RE: MailEngine.send() not useable?
April 25, 2010 10:35 PM
Answer

Pravin Pawar

Rank: Junior Member

Posts: 62

Join Date: November 17, 2009

Recent Posts

InternetAddress class comes with JavaMail API. You can import this class as javax.mail.internet.InternetAddress.

You can declare the InternetAddress array like this way to send the mail to multiple receipents...


1InternetAddress[] toInternetAddresses =
2    new InternetAddress[] {new InternetAddress(toEmailAddress)};
N. Belo
RE: MailEngine.send() not useable?
April 26, 2010 4:30 AM
Answer

N. Belo

Rank: Junior Member

Posts: 27

Join Date: March 16, 2009

Recent Posts

Hi Pravin,

Thank you for your reply but I've already imported the JavaMail API from /tomcat-6.0.18/lib/ext/mai.jar

and got this error:

11:00:41,820 ERROR [jsp:165] java.lang.LinkageError: loader constraints violated when linking javax/mail/internet/InternetAddress class

The error is caused by this line of code:

MailMessage message = new MailMessage(from, to, subject, mailBody, true);


If I use the other method, I don't know how to handle the MailEngineException because it's not a Throwable exception

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

}

Sorry, about this, but i've done a lot of code for this project and this issue about emails doesn't make sense.

Thank you.
BR,
N.
Pravin Pawar
RE: MailEngine.send() not useable?
April 26, 2010 4:53 AM
Answer

Pravin Pawar

Rank: Junior Member

Posts: 62

Join Date: November 17, 2009

Recent Posts

Please see the below code which I have used to send the mail, which can help you!
 1
 2protected void addTimesheet(ActionRequest actionRequest) throws Exception {            
 3   try{
 4        long userId = ParamUtil.getLong(actionRequest, "userId");
 5    long managerId = ParamUtil.getLong(actionRequest, "managerId");
 6        User user = UserLocalServiceUtil.getUserById(userId);   
 7        User managet = UserLocalServiceUtil.getUserById(managerId);   
 8
 9    InternetAddress fromInternetAddress = new InternetAddress(user.getEmailAddress(), user.getFullName());
10    InternetAddress[] toInternetAddresses = new InternetAddress[]{new InternetAddress(manager.getEmailAddress())};
11            
12    String subject = user.getFullName()+" Timesheet";
13    StringBuffer body = new StringBuffer();
14   
15    body.append("Dear Sir,\n");
16    body.append("I have completed all the task specified in the timesheet and requesting ");
17    body.append("for approve the timesheet. \n");
18    body.append("Let me know if you have any question. \n");
19    body.append("\n");
20    body.append("Thanks,\n");
21    body.append(user.getFirstName());
22    if (_log.isDebugEnabled()) {
23        _log.debug("Sending email to " + toInternetAddresses);
24    }
25            
26    MailEngine.send(fromInternetAddress, toInternetAddresses, subject, body.toString());
27            
28   }catch(Exception e){
29       if (_log.isErrorEnabled()) {
30           _log.error(e.getMessage(), e);
31   }
32}
N. Belo
RE: MailEngine.send() not useable?
April 26, 2010 6:42 AM
Answer

N. Belo

Rank: Junior Member

Posts: 27

Join Date: March 16, 2009

Recent Posts

Ok, and how can I handle this Exception problem... (please, see attached image)

Thanks,
N.
Attachment

Attachments: MailEngineException.JPG (35.7k)
kitie vbr
RE: MailEngine.send() not useable?
September 27, 2010 7:30 AM
Answer

kitie vbr

Rank: Junior Member

Posts: 39

Join Date: August 5, 2010

Recent Posts

Hello, I tried whith your code, but don´t recognize me _log.isDebugEnabled() and

_log.isDebugEnabled()

Where do you declare _log?????

Thanks a lot




Pravin Pawar:
Please see the below code which I have used to send the mail, which can help you!
 1
 2protected void addTimesheet(ActionRequest actionRequest) throws Exception {            
 3   try{
 4        long userId = ParamUtil.getLong(actionRequest, "userId");
 5    long managerId = ParamUtil.getLong(actionRequest, "managerId");
 6        User user = UserLocalServiceUtil.getUserById(userId);   
 7        User managet = UserLocalServiceUtil.getUserById(managerId);   
 8
 9    InternetAddress fromInternetAddress = new InternetAddress(user.getEmailAddress(), user.getFullName());
10    InternetAddress[] toInternetAddresses = new InternetAddress[]{new InternetAddress(manager.getEmailAddress())};
11            
12    String subject = user.getFullName()+" Timesheet";
13    StringBuffer body = new StringBuffer();
14   
15    body.append("Dear Sir,\n");
16    body.append("I have completed all the task specified in the timesheet and requesting ");
17    body.append("for approve the timesheet. \n");
18    body.append("Let me know if you have any question. \n");
19    body.append("\n");
20    body.append("Thanks,\n");
21    body.append(user.getFirstName());
22    if (_log.isDebugEnabled()) {
23        _log.debug("Sending email to " + toInternetAddresses);
24    }
25            
26    MailEngine.send(fromInternetAddress, toInternetAddresses, subject, body.toString());
27            
28   }catch(Exception e){
29       if (_log.isErrorEnabled()) {
30           _log.error(e.getMessage(), e);
31   }
32}
Jelmer Kuperus
RE: MailEngine.send() not useable?
September 27, 2010 9:20 AM
Answer

Jelmer Kuperus

Rank: Liferay Legend

Posts: 1192

Join Date: March 10, 2010

Recent Posts

MailEngineException extends from org.apache.commons.lang.exception.NestableException, do you have commons-lang on your classpath ?
Rubén García Tamayo
RE: MailEngine.send() not useable?
January 26, 2012 9:16 AM
Answer

Rubén García Tamayo

Rank: New Member

Posts: 7

Join Date: October 10, 2011

Recent Posts

jelmer kuperus:
MailEngineException extends from org.apache.commons.lang.exception.NestableException, do you have commons-lang on your classpath ?


Thank you very much jelmer!! this works pretty good!!!
Achmed Tyrannus Albab
RE: MailEngine.send() not useable?
May 14, 2013 4:44 AM
Answer

Achmed Tyrannus Albab

Rank: Junior Member

Posts: 92

Join Date: March 5, 2010

Recent Posts

Rubén García Tamayo:
jelmer kuperus:
MailEngineException extends from org.apache.commons.lang.exception.NestableException, do you have commons-lang on your classpath ?


Thank you very much jelmer!! this works pretty good!!!


Can you tell me how did you fix it?
I am having same problem and im not sure ho wto fix using eclipse Juno.