Configuring JavaMail for WebLogic Server Sending Messages with JavaMail

12-2 Developing Applications for Oracle WebLogic Server

12.2 Understanding JavaMail Configuration Files

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. Unless you want to extend JavaMail to support additional transports, protocols, and message types, you do not have to modify any JavaMail configuration files. If you do want to extend JavaMail, download JavaMail from Sun and follow Suns instructions for adding your extensions. Then add your extended JavaMail package in the WebLogic Server classpath in front of weblogic.jar.

12.3 Configuring JavaMail for WebLogic Server

To configure JavaMail for use in WebLogic Server, you create a mail session in the WebLogic Server Administration Console. This allows server-side modules and applications to access JavaMail services with JNDI, using Session properties you preconfigure for them. For example, by creating a mail session, you can designate the mail hosts, transport and store protocols, and the default mail user in the Administration Console so that modules that use JavaMail do not have to set these properties. 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. For information on using the Administration Console to create a mail session, see Configure access to JavaMail in the Oracle WebLogic Server Administration Console Help. 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. See Section 12.4, Sending Messages with JavaMail . 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.

12.4 Sending Messages with JavaMail

Here are the steps to send a message with JavaMail from within a WebLogic Server module: 1. Import the JNDI naming, JavaBean Activation, and JavaMail packages. You will also need to import java.util.Properties: import java.util.; import javax.activation.; import javax.mail.; import javax.mail.internet.; import javax.naming.; 2. Look up the Mail Session in JNDI: InitialContext ic = new InitialContext; Session session = Session ic.lookupmyMailSession; 3. If you need to override the properties you set for the Session in the Administration Console, create a java.util.Properties object and add the properties you want to override. Then call getInstance to get a new Session object with the new properties. Programming JavaMail with WebLogic Server 12-3 Properties props = new Properties; props.putmail.transport.protocol, smtp; props.putmail.smtp.host, mailhost; use mail address from HTML form for from address props.putmail.from, emailAddress; Session session2 = session.getInstanceprops; 4. Construct a MimeMessage. In the following example, to, subject, and messageTxt are String variables containing input from the user. Message msg = new MimeMessagesession2; msg.setFrom; msg.setRecipientsMessage.RecipientType.TO, InternetAddress.parseto, false; msg.setSubjectsubject; msg.setSentDatenew Date; Content is stored in a MIME multi-part message with one body part MimeBodyPart mbp = new MimeBodyPart; mbp.setTextmessageTxt; Multipart mp = new MimeMultipart; mp.addBodyPartmbp; msg.setContentmp; 5. Send the message. Transport.sendmsg; The JNDI lookup can throw a NamingException on failure. JavaMail can throw a MessagingException if there are problems locating transport classes or if communications with the mail host fails. Be sure to put your code in a try block and catch these exceptions.

12.5 Reading Messages with JavaMail