Oracle Weblogic mail sessions

Sometimes it’s handy to send mail reports from your newly created application… If we choose to do so and we have the expensive obligation of using $$ Oracle Weblogic in our project, we sure can use it 🙂

You need two things:

  1. A J2EE java application with JavaMail
  2. A Weblogic domain which is the container for the application

1. The java snippet


public class RepportMailArchiveerder implements IRapportArchiveerder{
    private static final Logger logger = Logger.getLogger(RepportMailArchiveerder.class);

    public RepportMailArchiveerder() {
        super();
    }

    /**
     * Send email.
     */
    public void mail(Rapport rapport){

        String content = rapport.toString();

        try {
            // Lookup the WLS context
            InitialContext ic = new InitialContext();
            Session session = (Session) ic.lookup("myTestMailSetup");

            //Construct the email
            MimeMessage msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(session.getProperty("mail.from")));
          msg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(session.getProperty("mail.to"), false));
            msg.setSubject(session.getProperty("mail.subject"));
            msg.setText(content);
            Transport.send(msg);
        } catch (MessagingException mex) {
            logger.error("Send mail error: " + mex);
            } catch (NamingException ne){
                ne.getMessage();
            }
    }
}

2. The WLS configuration : SERVICES >> MAIL SESSIONS


Advertisements
Leave a comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: