Version 5

    chinese version:

    http://www.javaarm.com/faces/display.xhtml?tid=2383

    or

    https://community.jboss.org/wiki/JBossAS720EmailSession-ChineseVersion

     

    This article introduces how to configure email session in JBoss AS 7.2.0. Here, we use gmail smtp. I summarized many articles and corrected some errors.

     

    1. configure mail session and smtp in standalone.xml

     

            <subsystem xmlns="urn:jboss:domain:mail:1.1">
                <mail-session jndi-name="java:/Mail" from="javaarm@gmail.com">
                    <smtp-server ssl="true" outbound-socket-binding-ref="mail-smtp-gmail">
                        <login name="javaarm@gmail.com" password="your_password"/>
                    </smtp-server>
                </mail-session>
                <mail-session jndi-name="java:/OtherMailSession" from="javaarm@gmail.com">
                    <smtp-server ssl="true" outbound-socket-binding-ref="mail-smtp-gmail">
                        <login name="javaarm@gmail.com" password="your_password"/>
                    </smtp-server>
                </mail-session>        
    
    </subsystem>
    
            <outbound-socket-binding name="mail-smtp-gmail">
                <remote-destination host="smtp.gmail.com" port="465"/>
            </outbound-socket-binding>
    

     

    2. Java code of getting Mail Session and sending email

     

    2.1 method.1: Resource injection

     

     

    @Local(IEmailService.class)
    @Singleton
    @Startup
    public class EmailService implements IEmailService{
        public static final Logger log = Logger.getLogger(EmailService.class.getName());
        public static final String EMAIL_SESSION_JNDI_PATH = "java:/Mail";
        
        @PersistenceContext
        private EntityManager em;
        
        @Resource(name = "java:/Mail")
        private Session mailSession;
    
    
     @Resource(name = "java:/OtherMailSession")
        private Session otherSession;
        
        
        @PostConstruct
        public void start() throws Exception {
        }
    
        @PreDestroy
        public void destroy(){
        }
        
        //*****************[interface methods]***************//
        @Override
        @PermitAll()
        public boolean sendMail(Email email){
            try{
                MimeMessage m = new MimeMessage(getEmailSession());
                InternetAddress[] to = new InternetAddress[] {
                        new InternetAddress(email.getToUser().getEmail()),
                        new InternetAddress("javaarm@gmail.com")//备份一份!
                };
                m.setRecipients(Message.RecipientType.TO, to);
                m.setSubject(email.getTitle());
                m.setSentDate(new java.util.Date());
                m.setText(email.getContent(), "utf-8", "html");
                //
                Transport.send(m);
                //
                return true;
            }catch(Exception e){
                e.printStackTrace();
                return false;
            }
        }
        
        private Session getEmailSession() throws Exception{
            return mailSession;
        }
    }
    

     

     

    2.2 method.2: JNDI look up

     

    change above getEmailSession() method to:

     

        private Session getEmailSession() throws Exception{
            InitialContext context = new InitialContext();
            return (Session) context.lookup(EMAIL_SESSION_JNDI_PATH);
        }
    

     

     

    Questions:   If My application in jboss is critical and I have 3 backing email server, how to use them?

    Answer: You can configure multiple mail sessions in mail subsystem and inject/lookup ones you need, or you can use JCA.

     

     

    Refrences:

    https://community.jboss.org/thread/176988

    https://community.jboss.org/message/783617?tstart=0&_sscc=t

    http://middlewaremagic.com/jboss/?p=940

    http://www.mastertheboss.com/jboss-configuration/jboss-mail-service-configuration

    https://community.jboss.org/thread/231210

    jboss-as-7.2.0.Final\docs\schema\jboss-as-mail_1_1.xsd