Using JavaMail with Weblogic Server

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 = "";
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

Post a Comment


  1. Hi there.

    I'm facing a problem trying to send a mail through an smtp server and I read your post and found it was interestingly well explained, although there is just one thing that I didn't get very clear.

    You say that if you want to customize a session, you might invoke the getInstance after you get a session with the look up... but... Why would you do that look up then?

    In fact, the problem I'm facing is about the relay of the smtp. I guess the thing is that I need an authentication, and the getSession can receive an authenticator that can return the user/password (which can not be specified as properties)...

    Voila my question ;)


  2. Hi Andrés,

    thanks for the question. I am not sure. I passed this on. Hope to get a reply ..


  3. Take a look at the bottom class here and then lines 124-134.
    It defines the authenticator and uses it just before sending out the email.
    I hope that helps...

  4. I send email using BPEL 11g and I set the MIME to multipart/mixed but when the message go out, the header shows it is multipart/related. Is there any way to go in the framework and make sure it doesn't change the email when it goes out ? The BPEL sends it out the way it supposes to be, but when I get it in gmail, I see that it has been changes ! :( Any suggestion ?

Post a Comment