|
| 1 | +import org.apache.commons.mail.DefaultAuthenticator; |
| 2 | +import org.apache.commons.mail.Email; |
| 3 | +import org.apache.commons.mail.SimpleEmail; |
| 4 | + |
| 5 | +public class InsecureSimpleEmailTest { |
| 6 | + public void test() throws Exception { |
| 7 | + // with setSSLOnConnect |
| 8 | + { |
| 9 | + Email email = new SimpleEmail(); |
| 10 | + email.setHostName("config.hostName"); |
| 11 | + email.setSmtpPort(25); |
| 12 | + email.setAuthenticator(new DefaultAuthenticator("config.username", "config.password")); |
| 13 | + email.setSSLOnConnect(true); // $hasInsecureJavaMail |
| 14 | + email.setFrom("fromAddress"); |
| 15 | + email.setSubject("subject"); |
| 16 | + email.setMsg("body"); |
| 17 | + email.addTo("toAddress"); |
| 18 | + email.send(); |
| 19 | + } |
| 20 | + // with setStartTLSRequired |
| 21 | + { |
| 22 | + Email email = new SimpleEmail(); |
| 23 | + email.setHostName("config.hostName"); |
| 24 | + email.setSmtpPort(25); |
| 25 | + email.setAuthenticator(new DefaultAuthenticator("config.username", "config.password")); |
| 26 | + email.setStartTLSRequired(true); // $hasInsecureJavaMail |
| 27 | + email.setFrom("fromAddress"); |
| 28 | + email.setSubject("subject"); |
| 29 | + email.setMsg("body"); |
| 30 | + email.addTo("toAddress"); |
| 31 | + email.send(); |
| 32 | + } |
| 33 | + // safe with setSSLOnConnect |
| 34 | + { |
| 35 | + Email email = new SimpleEmail(); |
| 36 | + email.setHostName("config.hostName"); |
| 37 | + email.setSmtpPort(25); |
| 38 | + email.setAuthenticator(new DefaultAuthenticator("config.username", "config.password")); |
| 39 | + email.setSSLOnConnect(true); // Safe |
| 40 | + email.setSSLCheckServerIdentity(true); |
| 41 | + email.setFrom("fromAddress"); |
| 42 | + email.setSubject("subject"); |
| 43 | + email.setMsg("body"); |
| 44 | + email.addTo("toAddress"); |
| 45 | + email.send(); |
| 46 | + } |
| 47 | + // safe with setStartTLSRequired |
| 48 | + { |
| 49 | + Email email = new SimpleEmail(); |
| 50 | + email.setHostName("config.hostName"); |
| 51 | + email.setSmtpPort(25); |
| 52 | + email.setAuthenticator(new DefaultAuthenticator("config.username", "config.password")); |
| 53 | + email.setStartTLSRequired(true); // Safe |
| 54 | + email.setSSLCheckServerIdentity(true); |
| 55 | + email.setFrom("fromAddress"); |
| 56 | + email.setSubject("subject"); |
| 57 | + email.setMsg("body"); |
| 58 | + email.addTo("toAddress"); |
| 59 | + email.send(); |
| 60 | + } |
| 61 | + } |
| 62 | +} |
0 commit comments