Hello all,
JBoss has now JavaMail 1.2 support !
Security guys, see bullet 1a, please.
Legal stuff: AFAIK, mail.jar from Sun is freely redistributable, so there
should be no restriction to use it in JBoss or in your client applications.
HowTo:
1) edit jboss.jcml and find Mail Service MBean;
a) replace User and Password attributes values with the user name and
password used to connect to your mail server (Security guys: I did not find
a clean solution for the password being stored in clear here. Any hint ?)
b) specify a file with the mail settings, default is "mail.properties"
c) specify a JNDI name for your mail session, that will be used in
<res-jndi-name> tag in jboss.xml, bullet 4 (default is "Mail")
2) go in the configuration directory of JBoss and set the mail properties in
the file given in bullet 1b (see JavaMail specification for details).
3) in your EJB, specify a <resource-ref> like this:
<ejb-jar>
<enterprise-beans>
...
<session>
<ejb-name>Mailer</ejb-name>
<home>some.package.MailerHome</home>
<remote>some.package.Mailer</remote>
<ejb-class>some.package.MailerEJB</ejb-class>
<session-type>Stateless</session-type>
<transaction-type>Container</transaction-type>
<resource-ref>
<res-ref-name>mail/MyMail</res-ref-name>
<res-type>javax.mail.Session</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</session>
...
</enterprise-beans>
</ejb-jar>
4) in jboss.xml, specify a <resource-manager> like this:
<jboss>
...
<resource-managers>
<resource-manager>
<res-name>mail/MyMail</res-name>
<res-jndi-name>Mail</res-jndi-name>
</resource-manager>
</resource-managers>
</jboss>
5) from your EJB, lookup the mail session like this:
InitialContext ctx = new InitialContext();
Object ref = ctx.lookup("java:comp/env/mail/MyMail");
Session session = (Session)PortableRemoteObject.narrow(ref, Session.class);
then create a MimeMessage, set its properties and send it !
For example:
MimeMessage m = new MimeMessage(session);
m.setFrom();
Address[] to = new InternetAddress[] {new
InternetAddress("[EMAIL PROTECTED]")};
m.setRecipients(Message.RecipientType.TO, to);
m.setSubject("JavaMail Test");
m.setSentDate(new Date());
m.setContent("Test from inside EJB Using JBoss", "text/plain");
Transport.send(m);
I tried it using Yahoo, where I have an account, and it worked just fine.
(
Yahoo requires to be POP autenticated before using SMTP, but this is easily
done using:
Store s = session.getStore();
s.connect(); // POP authentication
Transport.send(m);
)
That's it !
Simon