package fi.logiasoftware.messageserver.services.backend; import java.util.Date; import javax.annotation.Resource; import javax.ejb.EJBException; import javax.ejb.Local; import javax.ejb.Remote; import javax.ejb.Stateless; import javax.mail.Address; import javax.mail.Message; import javax.mail.Session; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import javax.naming.InitialContext; import javax.rmi.PortableRemoteObject; import org.jboss.annotation.ejb.LocalBinding; import org.jboss.annotation.ejb.RemoteBinding; @Stateless @RemoteBinding(jndiBinding="Mailer") @LocalBinding(jndiBinding="MailerLocal") @Remote(Mailer.class) @Local(Mailer.class) public class MailerBean implements Mailer { public void sendMail(String [] address, String subject, String message) { Session session = null; try { InitialContext ic = new InitialContext(); session = (Session) PortableRemoteObject.narrow( ic.lookup("java:Mail"), Session.class); } catch (javax.naming.NamingException e) { throw new EJBException(e); } try { MimeMessage m = new MimeMessage(session); m.setFrom(); InternetAddress[] ia = new InternetAddress[address.length]; for(int i = 0; i < address.length; i++) { ia[i] = new InternetAddress(address[i]); } Address[] to = ia; Address from = new InternetAddress("messageserver@logiasoftware.fi"); m.setFrom(from); m.setRecipients(Message.RecipientType.TO, to); m.setSubject("MessageServer alert: " + subject); m.setSentDate(new Date()); m.setContent(message, "text/plain"); Transport.send(m); } catch (javax.mail.MessagingException e) { e.printStackTrace(); } } }