Enterprise grade Java.
You'll read about Conferences, Java User Groups, Java, Integration, Reactive, Microservices and other technologies.

Wednesday, August 19, 2009

Using JavaMail with Weblogic Server

08:06 Wednesday, August 19, 2009 Posted by Markus Eisele
, , , ,
WebLogic Server includes the JavaMail API version 1.3. Using the JavaMail API, you can add email capabilities to your enterprise java applications. It provides access to Internet Message Access Protocol (IMAP)- and Simple Mail Transfer Protocol (SMTP).

JavaMail depends on configuration files that define the mail transport capabilities of the system. The weblogic.jar file contains the standard configuration files from Sun, which enable IMAP and SMTP mail servers for JavaMail and define the default message types JavaMail can process. If you do want to extend JavaMail, download JavaMail from Sun and follow Sun’s instructions for adding your extensions. Then add your extended JavaMail package in the WebLogic Server classpath in front of weblogic.jar.

First thing to do is to configure a JavaMail Session in WebLogic Server. This allows server-side modules and applications to access JavaMail services with JNDI, using Session properties you preconfigure for them (e.g. mail hosts, transport and store protocols, and the default mail user). Applications that are heavy email users benefit because the mail session creates a single javax.mail.Session object and makes it available via JNDI to any module that needs it.

You can override any properties set in the mail session in your code by creating a java.util.Properties object containing the properties you want to override. Then, after you look up the mail session object in JNDI, call the Session.getInstance() method with your Properties object to get a customized Session.

To create a mail session in weblogic admin console:

  1. In the Administration Console, expand Services and select Mail Sessions.

  2. Click the New button.

  3. Enter a name for your mail session and click OK.

  4. On the Mail Sessions page, click the new mail session name.

  5. On the Mail Sessions: Configuration page, in JNDI Name, enter a unique JNDI name.

  6. In Properties, specify information for connecting to an existing mail server.

  7. save your changes

A simple example of sending a message with java mail:

// Look up the mail session
InitialContext ic = new InitialContext();
Session session = (Session)

// Construct a MimeMessage.

String to = "some@adress.net";
String subject = "some example subject";
String messageTxt =
"Some example message body text";

Message msg = new MimeMessage(session);
InternetAddress.parse(to, false));
msg.setSentDate(new Date());
// Content is stored in a MIME
// multi-part message
// with one body part
MimeBodyPart mbp = new MimeBodyPart();

Multipart mp = new MimeMultipart();

//Send the message