Create mail session with Weblogic
The aim of the mailsession in Weblogic is avoid to parameter your session in your java code and obtain a dynamic mail server without to recode your application.


Here are the steps :
- In your Administration console, go to Service, Mail Sessions.Then click on New.
- Fill the information like the screen :
The "name" is for weblogic, but the "JNDI" Name will be the session name used in your java code to retrieve the server mail parameters.
- then you have to put the parameters of your mail server in the "JavaMail Properties" box :
| Property | Description | Default |
|---|---|---|
| mail.store.protocol | Protocol for retrieving email. Example: mail.store.protocol=imap | imap |
| mail.transport.protocol | Protocol for sending email. Example: mail.transport.protocol=smtp | smtp |
| mail.host | The name of the mail host machine. Example: mail.host=mailserver | Local machine |
| mail.user | Name of the default user for retrieving email. Example: mail.user=postmaster | Value of theuser.name Java system property. |
| mail.protocol.host | Mail host for a specific protocol. For example, you can set mail.SMTP.host andmail.IMAP.host to different machine names. Examples:mail.smtp.host=mail.mydom.com mail.imap.host=localhost | Value of themail.hostproperty. |
| mail.protocol.user | Protocol-specific default user name for logging into a mailer server. Examples:mail.smtp.user=weblogic mail.imap.user=appuser | Value of themail.userproperty. |
| mail.from | The default return address. Examples: mail.from=master@mydom.com | username@host |
| mail.debug | Set to True to enable JavaMail debug output. | False |
in my case :
you also can specify more information (report to the tab before) like user login and hostname.
- Click Next, check your Managed server and click Finish.
Now the Java code part :
- The imports :
import java.util.*;
import javax.activation.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.naming.*;- Load your Weblogic MailSession :
InitialContext ic = new InitialContext();
Session session = (Session) ic.lookup("myMail");- If you need to override the Weblogic parameters you can do with the following code and the java.util.Properties Object (OPTIONAL)
Properties props = new Properties();
props.put("mail.transport.protocol", "imap"); // new protocol instead of smtp
props.put("mail.smtp.host", "mailhost");
// use mail address from HTML form for from address
props.put("mail.from", emailAddress);
Session session2 = session.getInstance(props);- Construct a MimeMessage
Message msg = new MimeMessage(session2);
msg.setFrom();
msg.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to, false));//"to" and "subject" are strings define by the user or developer
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);For more informations on How to use javamail API with Jdeveloper : http://edocs.bea.com/wls/docs90/programming/javamail.html
Now you have a mail sent through your application with a dynamic smtp or mail server ! (you just need to change java properties in your MailSession in Weblogic)
