Kohei Nozaki's blog 

How to define a JavaMail session using CLI


Posted on Sunday Feb 16, 2014 at 07:30AM in Technology


  • Defining a JavaMail session for gmail.

Environment

  • WildFly8.0.0.Final
  • Oracle JDK7u51

Defining with jboss-cli

Define a outbound-socket-binding named “MyMailSMTP”

  • SMTP server hostname
  • SMTP server port
/socket-binding-group=standard-sockets/remote-destination-outbound-socket-binding=MyMailSMTP:add( \
host=smtp.gmail.com, \
port=465)

Define a JavaMail session named “MyMail”

  • JNDI name of JavaMail session
  • Email address of the sender
  • Debug flag
/subsystem=mail/mail-session=MyMail:add(jndi-name="java:/mail/myMail", from="jane.doe@gmail.example.com", debug=true)

Add a reference from “MyMail” to “MyMailSMTP”

  • SMTP server username
  • SMTP server password
  • TLS flag
  • SSL flag
/subsystem=mail/mail-session=MyMail/server=smtp:add( \
outbound-socket-binding-ref=MyMailSMTP, \
ssl=true, \
username=jane.doe@gmail.example.com, \
password=***)

Example Servlet sends a mail

@WebServlet("/")
public class MailServlet extends HttpServlet {
    @Resource(lookup = "java:/mail/myMail")
    Session session;

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        MimeMessage message = new MimeMessage(session);
        try {

            InternetAddress[] address = { new InternetAddress("jane.doe@icloud.example.com") };
            message.setRecipients(Message.RecipientType.TO, address);
            message.setSubject("test");
            message.setSentDate(new Date());
            message.setText("testbody");
            Transport.send(message);
        } catch (MessagingException ex) {
            throw new ServletException(ex);
        }
    }
}

Mail which sent from WildFly

Debug Log

15:37:26,305 INFO  [stdout] (default task-1) DEBUG: JavaMail version 1.5.1
15:37:26,306 INFO  [stdout] (default task-1) DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
15:37:26,307 INFO  [stdout] (default task-1) DEBUG: Tables of loaded providers
15:37:26,307 INFO  [stdout] (default task-1) DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
15:37:26,307 INFO  [stdout] (default task-1) DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
15:37:26,307 INFO  [stdout] (default task-1) DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
15:37:26,309 INFO  [stdout] (default task-1) DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
15:37:26,309 INFO  [stdout] (default task-1) DEBUG SMTP: need username and password for authentication
15:37:26,309 INFO  [stdout] (default task-1) DEBUG SMTP: useEhlo true, useAuth true
15:37:26,309 INFO  [stdout] (default task-1) DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 465, isSSL true
15:37:26,889 INFO  [stdout] (default task-1) 220 mx.google.com ESMTP *** - gsmtp
15:37:26,889 INFO  [stdout] (default task-1) DEBUG SMTP: connected to host "smtp.gmail.com", port: 465
15:37:26,889 INFO  [stdout] (default task-1) 
15:37:26,893 INFO  [stdout] (default task-1) EHLO ***
15:37:27,006 INFO  [stdout] (default task-1) 250-mx.google.com at your service, [27.114.70.39]
15:37:27,006 INFO  [stdout] (default task-1) 250-SIZE 35882577
15:37:27,006 INFO  [stdout] (default task-1) 250-8BITMIME
15:37:27,006 INFO  [stdout] (default task-1) 250-AUTH LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN
15:37:27,007 INFO  [stdout] (default task-1) 250-ENHANCEDSTATUSCODES
15:37:27,007 INFO  [stdout] (default task-1) 250 CHUNKING
15:37:27,007 INFO  [stdout] (default task-1) DEBUG SMTP: Found extension "SIZE", arg "35882577"
15:37:27,007 INFO  [stdout] (default task-1) DEBUG SMTP: Found extension "8BITMIME", arg ""
15:37:27,007 INFO  [stdout] (default task-1) DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH XOAUTH2 PLAIN-CLIENTTOKEN"
15:37:27,008 INFO  [stdout] (default task-1) DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
15:37:27,008 INFO  [stdout] (default task-1) DEBUG SMTP: Found extension "CHUNKING", arg ""
15:37:27,008 INFO  [stdout] (default task-1) DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM 
15:37:27,008 INFO  [stdout] (default task-1) DEBUG SMTP: AUTH LOGIN command trace suppressed
15:37:27,959 INFO  [stdout] (default task-1) DEBUG SMTP: AUTH LOGIN succeeded
15:37:27,960 INFO  [stdout] (default task-1) DEBUG SMTP: use8bit false
15:37:27,960 INFO  [stdout] (default task-1) MAIL FROM:<jane.doe@gmail.example.com>
15:37:28,074 INFO  [stdout] (default task-1) 250 2.1.0 OK *** - gsmtp
15:37:28,074 INFO  [stdout] (default task-1) RCPT TO:<jane.doe@icloud.example.com>
15:37:28,194 INFO  [stdout] (default task-1) 250 2.1.5 OK *** - gsmtp
15:37:28,195 INFO  [stdout] (default task-1) DEBUG SMTP: Verified Addresses
15:37:28,195 INFO  [stdout] (default task-1) DEBUG SMTP:   jane.doe@icloud.example.com
15:37:28,195 INFO  [stdout] (default task-1) DATA
15:37:28,881 INFO  [stdout] (default task-1) 354  Go ahead *** - gsmtp
15:37:28,881 INFO  [stdout] (default task-1) Date: Thu, 6 Mar 2014 15:37:26 +0900 (JST)
15:37:28,881 INFO  [stdout] (default task-1) To: jane.doe@icloud.example.com
15:37:28,881 INFO  [stdout] (default task-1) Message-ID: <56980149.1.1394087846308.JavaMail.jane.doe@gmail.example.com>
15:37:28,882 INFO  [stdout] (default task-1) Subject: test
15:37:28,882 INFO  [stdout] (default task-1) MIME-Version: 1.0
15:37:28,882 INFO  [stdout] (default task-1) Content-Type: text/plain; charset=us-ascii
15:37:28,882 INFO  [stdout] (default task-1) Content-Transfer-Encoding: 7bit
15:37:28,882 INFO  [stdout] (default task-1) 
15:37:28,882 INFO  [stdout] (default task-1) testbody
15:37:28,883 INFO  [stdout] (default task-1) .
15:37:29,802 INFO  [stdout] (default task-1) 250 2.0.0 OK 1394087849 *** - gsmtp
15:37:29,803 INFO  [stdout] (default task-1) QUIT
15:37:29,916 INFO  [stdout] (default task-1) 221 2.0.0 closing connection *** - gsmtp
  • Debug logging can be turned off like this:
/subsystem=mail/mail-session=MyMail:write-attribute(name=debug, value=false)
/:reload

References

  1. How to configure a Mail Session in Wildfly? | Community
  2. JBoss AS 7.2.0 Email Session configurtion - Eng… | Community
  3. jboss7.x - Please help configure JBoss 7 “mail-session” via jboss-cli - Stack Overflow
  4. Configuring and Using mail service in JBoss AS7 « JBoss
  5. JBoss 7 JavaMail | Community



Comments:

Thanks

Posted by asd on November 28, 2016 at 04:54 AM JST #


Leave a Comment

HTML Syntax: NOT allowed