Today is Monday, 4th July 2022

Archive for May, 2012

Unit Testing Java Email Code with Fake SMTP Server

What is your testing strategy for sending emails from your Java application? Do you hard-code your developer email in the application? I’ve been there and done that (not too proud though). Ideally, you would like to use the actual email addresses in your testing process. However, you do not want to send the email to the actual recipient. You would also like to view the message and validate/assert the message is correct.

One possible solution is to install a local email server and run your tests against the server. However, this requires that you create accounts for all of your test data or customer data.

I found a really great open-source solution to this problem, Wiser. Wiser is a fake SMTP server written in Java. You can use it for unit testing your application that sends email. It accepts all email messages and stores them in memory. Then you can call an API method to retrieve the messages. All of this can run in the same VM as your unit test…really slick!

It is really easy to get started using Wiser:

Step 1:  Start Wiser in your Java code

Wiser wiser = new Wiser();
wiser.setPort(2500); // Default is 25

Step 2:  Call your Java code to send email to the Wiser server

// your JavaMail code

Step 3: Retrieve messages from Wiser

for (WiserMessage message : wiser.getMessages()) {
    String envelopeSender = message.getEnvelopeSender();
    String envelopeReceiver = message.getEnvelopeReceiver();

    MimeMessage mess = message.getMimeMessage();

   // now do assert the message headers and contents

That’s it.  Take a look at Wiser’s website to get the JAR files. Enjoy!

Configure SSL on GlassFish with GoDaddy Certificate: GlassFish 2 and GlassFish 3

This page describes the steps for setting up SSL on a GlassFish server. The SSL Certificate used is a GoDaddy certificate. If you are using a different CA then the steps should be similar.

These steps apply to GlassFish 2 and GlassFish 3.

Preliminary Steps:

  • Backup your directory: <glassfish_install>\domains\domain1\config
  • Stop the GlassFish server

The following fields will change based on your installation. For this example, we are using:

  • CSR file name: mywebsite.csr
  • dname: information on about your server install, where cn=<fully qualified domain name>
  • certificate file:
  • keystore file: mycompanystore.jks

Note: These variables will be displayed in red bold in the following steps. Be sure to change the values based on your server environment.


Open MS-DOS window (Run As Administrator)

cd <glassfish_install>\domains\domain1\config

Step 1:  Generate key

keytool -genkey -dname ", ou=My Company Name, o=My Company Name, L=Philadelphia, ST=PA, C=US" -keyalg RSA -keysize 2048 -alias mykey -keypass changeit -keystore mycompanystore.jks -storepass changeit -validity 365

Step 2:  Generate CSR file

keytool -certreq -alias mykey -keypass changeit -keystore mycompanystore.jks -storepass changeit -file mywebsite.csr

Email the file “mywebsite.csr” to the Certificate Authority (CA). The CA will eventually send you an email to download the certificates. You’ll use those in step 3.

Step 3:  Download the GoDaddy certificate zip file

The zip file includes:

  • gd_bundle.crt

Unzip the zip file. Copy these files to <glassfish_install>\domains\domain1\config

Import Go Daddy root certificate

keytool -import -trustcacerts -alias root -file gd_bundle.crt -keystore mycompanystore.jks -keypass changeit -storepass changeit

Step 4: Import your domain certificate

keytool -import -trustcacerts -alias mykey -file -keystore mycompanystore.jks -keypass changeit -storepass changeit

Step 5:  Change the server config to point to the new mycompanystore.jks file.

Edit the file cd <glassfish_install>\domains\domain1\config\domain.xml


Step 6:  Change the server config to use new SSL nickname.

Perform a global replace of “s1as” with “mykey”. “mykey” is the alias you used in the previous steps.
<ssl cert-nickname="s1as" client-auth-enabled="false" ssl2-enabled="false" ssl3-enabled="true" tls-enabled="true" tls-rollback-enabled="true"/>

Step 7: Restart the GlassFish server

Step 8: Visit the SSL URL in your browser


By default, GlassFish SSL port is on 8181. If you’d like, you can change it to port 443 which is the browser default for the secure https:// protocol.