Using JavaMail with Weblogic Server

Markus Eisele
4
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)
ic.lookup("sampleMailSession");

// 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);
msg.setFrom();
msg.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to, false));
msg.setSubject(subject);
msg.setSentDate(new Date());
// Content is stored in a MIME
// multi-part message
// with one body part
MimeBodyPart mbp = new MimeBodyPart();
mbp.setText(messageTxt);

Multipart mp = new MimeMultipart();
mp.addBodyPart(mbp);
msg.setContent(mp);

//Send the message
Transport.send(msg);

Post a Comment

4Comments

  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 ;)

    Cheers,
    Andrés

    ReplyDelete
  2. Hi Andrés,

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

    Thanks,
    Markus

    ReplyDelete
  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...

    https://github.com/NeQuissimus/Library/blob/master/src/com/nequissimus/library/email/EmailSenderSSL.java

    ReplyDelete
  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 ?

    ReplyDelete
Post a Comment