|
2 | 2 |
|
3 | 3 | import java.io.File;
|
4 | 4 | import java.net.URI;
|
| 5 | +import java.security.KeyManagementException; |
| 6 | +import java.security.NoSuchAlgorithmException; |
| 7 | +import java.security.cert.CertificateException; |
| 8 | +import java.security.cert.X509Certificate; |
5 | 9 | import java.util.Properties;
|
6 | 10 |
|
| 11 | +import javax.net.ssl.SSLContext; |
| 12 | +import javax.net.ssl.SSLSocketFactory; |
| 13 | +import javax.net.ssl.TrustManager; |
| 14 | +import javax.net.ssl.X509TrustManager; |
| 15 | + |
7 | 16 | import jakarta.mail.Authenticator;
|
8 | 17 | import jakarta.mail.Message;
|
9 | 18 | import jakarta.mail.Multipart;
|
@@ -40,6 +49,33 @@ public EmailService(String host, int port) {
|
40 | 49 | prop.put("mail.smtp.port", port);
|
41 | 50 | }
|
42 | 51 |
|
| 52 | + public EmailService(String host, int port, boolean bypassServerCertError) throws NoSuchAlgorithmException, KeyManagementException { |
| 53 | + prop = new Properties(); |
| 54 | + prop.put("mail.smtp.host", host); |
| 55 | + prop.put("mail.smtp.port", port); |
| 56 | + if (bypassServerCertError) { |
| 57 | + |
| 58 | + prop.put("mail.smtp.ssl.trust", "*"); |
| 59 | + prop.put("mail.smtp.ssl.checkserveridentity", false); |
| 60 | + |
| 61 | + // use this when SMTPS protocol |
| 62 | + // SSLContext sslContext = SSLContext.getInstance("TLS"); |
| 63 | + // sslContext.init(null, trustAllCerts, new java.security.SecureRandom()); |
| 64 | + // SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory(); |
| 65 | + // prop.put("mail.smtp.ssl.enable", "true"); |
| 66 | + // prop.put("mail.smtp.ssl.socketFactory", sslSocketFactory); |
| 67 | + // prop.put("mail.smtp.ssl.trust", "*"); |
| 68 | + } |
| 69 | + } |
| 70 | + |
| 71 | + TrustManager[] trustAllCerts = new TrustManager[] { |
| 72 | + new X509TrustManager() { |
| 73 | + public void checkClientTrusted(X509Certificate[] certs, String authType) throws CertificateException{} |
| 74 | + public void checkServerTrusted(X509Certificate[] certs, String authType) throws CertificateException{} |
| 75 | + public X509Certificate[] getAcceptedIssuers() { return new X509Certificate[0];} |
| 76 | + } |
| 77 | + }; |
| 78 | + |
43 | 79 | public static void main(String... args) {
|
44 | 80 | try {
|
45 | 81 | new EmailService("smtp.mailtrap.io", 25, "87ba3d9555fae8", "91cb4379af43ed").sendMail();
|
|
0 commit comments