1010
1111import java .util .Properties ;
1212
13- class InsecureJavaMail {
13+ class InsecureJavaMailTest {
1414 public void testJavaMail () {
1515 final Properties properties = new Properties ();
1616 properties .put ("mail.transport.protocol" , "protocol" );
@@ -24,18 +24,48 @@ protected PasswordAuthentication getPasswordAuthentication() {
2424 };
2525 if (null != authenticator ) {
2626 properties .put ("mail.smtp.auth" , "true" );
27- // properties.put("mail.smtp.ssl.checkserveridentity", "true");
2827 }
29- final Session session = Session .getInstance (properties , authenticator );
28+ final Session session = Session .getInstance (properties , authenticator ); // $hasInsecureJavaMail
29+ }
30+
31+ public void testSecureJavaMail () {
32+ final Properties properties = new Properties ();
33+ properties .put ("mail.transport.protocol" , "protocol" );
34+ properties .put ("mail.smtp.host" , "hostname" );
35+ properties .put ("mail.smtp.socketFactory.class" , "classname" );
36+
37+ final javax .mail .Authenticator authenticator = new javax .mail .Authenticator () {
38+ protected PasswordAuthentication getPasswordAuthentication () {
39+ return new PasswordAuthentication ("username" , "password" );
40+ }
41+ };
42+ if (null != authenticator ) {
43+ properties .put ("mail.smtp.auth" , "true" );
44+ properties .put ("mail.smtp.ssl.checkserveridentity" , "true" );
45+ }
46+ final Session session = Session .getInstance (properties , authenticator ); // Safe
3047 }
3148
3249 public void testSimpleMail () throws Exception {
3350 Email email = new SimpleEmail ();
3451 email .setHostName ("config.hostName" );
3552 email .setSmtpPort (25 );
3653 email .setAuthenticator (new DefaultAuthenticator ("config.username" , "config.password" ));
37- email .setSSLOnConnect (true );
38- // email.setSSLCheckServerIdentity(true);
54+ email .setSSLOnConnect (true ); // $hasInsecureJavaMail
55+ email .setFrom ("fromAddress" );
56+ email .setSubject ("subject" );
57+ email .setMsg ("body" );
58+ email .addTo ("toAddress" );
59+ email .send ();
60+ }
61+
62+ public void testSecureSimpleMail () throws Exception {
63+ Email email = new SimpleEmail ();
64+ email .setHostName ("config.hostName" );
65+ email .setSmtpPort (25 );
66+ email .setAuthenticator (new DefaultAuthenticator ("config.username" , "config.password" ));
67+ email .setSSLOnConnect (true ); // Safe
68+ email .setSSLCheckServerIdentity (true );
3969 email .setFrom ("fromAddress" );
4070 email .setSubject ("subject" );
4171 email .setMsg ("body" );
0 commit comments