mail-resource Mail session

classic Classic list List threaded Threaded
3 messages Options
Reply | Threaded
Open this post in threaded view
|

mail-resource Mail session

Andreas Ernst
Hi,

i try to access a Payara Mailsession (mail-resource), but it is always null:

@Stateless
public class MailSessionBean {

   private static final Logger logger =
Logger.getLogger(MailSessionBean.class.getName());
   @Resource(name = "mail/motdSession")
   private Session mailSession;

   public void sendEmail(String to, String subject, String body) {
     MimeMessage message = new MimeMessage(mailSession);
     logger.log(Level.INFO, "### mailSession.sendEmail: {0}", mailSession);

mailSession is null. I added the class to AppModule:

public static void bind(ServiceBinder binder) {
   binder.bind(MailSessionBean.class);

but nothing helped, anybody how has this running. Google doesn't really
help.

Thanks
Andy
--
ae | Andreas Ernst | IT Spektrum
Postfach 5, 65612 Beselich
Schupbacher Str. 32, 65614 Beselich, Germany
Tel: +49-6484-91002 Fax: +49-6484-91003
[hidden email] | www.ae-online.de
www.tachyon-online.de

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]

Reply | Threaded
Open this post in threaded view
|

Re: mail-resource Mail session

Thiago H de Paula Figueiredo
On Sun, Sep 9, 2018 at 12:22 PM Andreas Ernst <[hidden email]> wrote:

> Hi,
>

Hi!


> i try to access a Payara Mailsession (mail-resource), but it is always
> null:
>
> @Stateless
> public class MailSessionBean {
>
>    private static final Logger logger =
> Logger.getLogger(MailSessionBean.class.getName());
>    @Resource(name = "mail/motdSession")
>    private Session mailSession;
>

Shouldn't you use @Inject instead of @Resource? What's the fully qualified
name of the @Stateless and @Resource annotations you're using?


> public static void bind(ServiceBinder binder) {
>    binder.bind(MailSessionBean.class);
>

This part is correct.

--
Thiago
Reply | Threaded
Open this post in threaded view
|

Re: mail-resource Mail session

Andreas Ernst
Hi Thiago,

maybe i give to less informations.

Am 11.09.18 um 14:13 schrieb Thiago H. de Paula Figueiredo:

> On Sun, Sep 9, 2018 at 12:22 PM Andreas Ernst <[hidden email]> wrote:
>
>> Hi,
>>
>
> Hi!
>
>
>> i try to access a Payara Mailsession (mail-resource), but it is always
>> null:
>>
>> @Stateless
>> public class MailSessionBean {
>>
>>     private static final Logger logger =
>> Logger.getLogger(MailSessionBean.class.getName());
>>     @Resource(name = "mail/motdSession")
>>     private Session mailSession;
>>
>
> Shouldn't you use @Inject instead of @Resource? What's the fully qualified
> name of the @Stateless and @Resource annotations you're using?

This is the bean, where mail is created:

@Stateless
public class MailSessionBean {

   private static final Logger logger =
Logger.getLogger(MailSessionBean.class.getName());
   @Resource(lookup = "mail/motdSession")
   private Session mailSession;

   public void sendEmail(String to, String subject, String body) {
     MimeMessage message = new MimeMessage(mailSession);
     try {
       message.setFrom(new
InternetAddress(mailSession.getProperty("mail.from")));
       InternetAddress[] address = {new InternetAddress(to)};
       message.setRecipients(Message.RecipientType.TO, address);
       message.setSubject(subject);
       message.setSentDate(new Date());
       message.setText(body);
       Transport.send(message);
     } catch (MessagingException ex) {
     }
   }
}

Here is the Tapestry page:

import de.aeits.motd.beans.MailSessionBean;
[...]

public class AccountReset {

   [...]
   @Inject
   private MailSessionBean mailBean;

   [...]

   Object onSuccess() {
        mailBean.sendEmail("[hidden email]", "TestMail - Java Mail Session",
"TestMail - Java Mail Session - Body\n\n(c) 2018 AE Online");
   }

   [...]
}


mail:~ # asadmin list-javamail-resources
Enter admin user name>
Enter admin password>
mail/Session
mailSession
mail/motdSession
Command list-javamail-resources executed successfully.

--
ae | Andreas Ernst | IT Spektrum
Postfach 5, 65612 Beselich
Schupbacher Str. 32, 65614 Beselich, Germany
Tel: +49-6484-91002 Fax: +49-6484-91003
[hidden email] | www.ae-online.de
www.tachyon-online.de

---------------------------------------------------------------------
To unsubscribe, e-mail: [hidden email]
For additional commands, e-mail: [hidden email]