10
10
11
11
import java .util .Properties ;
12
12
13
- class InsecureJavaMail {
13
+ class InsecureJavaMailTest {
14
14
public void testJavaMail () {
15
15
final Properties properties = new Properties ();
16
16
properties .put ("mail.transport.protocol" , "protocol" );
@@ -24,18 +24,48 @@ protected PasswordAuthentication getPasswordAuthentication() {
24
24
};
25
25
if (null != authenticator ) {
26
26
properties .put ("mail.smtp.auth" , "true" );
27
- // properties.put("mail.smtp.ssl.checkserveridentity", "true");
28
27
}
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
30
47
}
31
48
32
49
public void testSimpleMail () throws Exception {
33
50
Email email = new SimpleEmail ();
34
51
email .setHostName ("config.hostName" );
35
52
email .setSmtpPort (25 );
36
53
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 );
39
69
email .setFrom ("fromAddress" );
40
70
email .setSubject ("subject" );
41
71
email .setMsg ("body" );
0 commit comments