質問編集履歴
1
自己解決
title
CHANGED
File without changes
|
body
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
###servletからjavax.mailを使ってgmailにメールを送りたい
|
1
|
+
###【自己解決しました】servletからjavax.mailを使ってgmailにメールを送りたい
|
2
2
|
現在、javax.mailの仕組みを利用してgmailに対してメールを送信する仕組みを構築しています。
|
3
3
|
mainメソッドからはメールが送信できることを確認しましたが、この仕組みをWebSphereServerに持って行ったところ、
|
4
4
|
下記のエラーが発生しました。
|
@@ -16,23 +16,114 @@
|
|
16
16
|
|
17
17
|
###該当のソースコード
|
18
18
|
```Java
|
19
|
+
package com.xxxxx.module.mail;
|
20
|
+
|
21
|
+
import java.io.IOException;
|
19
|
-
|
22
|
+
import java.net.InetAddress;
|
23
|
+
import java.net.Socket;
|
24
|
+
import java.security.cert.X509Certificate;
|
25
|
+
|
26
|
+
import javax.net.SocketFactory;
|
27
|
+
import javax.net.ssl.SSLContext;
|
28
|
+
import javax.net.ssl.SSLSocketFactory;
|
29
|
+
import javax.net.ssl.TrustManager;
|
30
|
+
import javax.net.ssl.X509TrustManager;
|
31
|
+
|
32
|
+
/**
|
33
|
+
* DummySSLSocketFactory
|
34
|
+
*/
|
20
|
-
|
35
|
+
public class DummySSLSocketFactory extends SSLSocketFactory {
|
36
|
+
private SSLSocketFactory factory;
|
37
|
+
|
38
|
+
public DummySSLSocketFactory() {
|
39
|
+
try {
|
21
|
-
|
40
|
+
SSLContext sslcontext = SSLContext.getInstance("TLS");
|
41
|
+
sslcontext.init(null, new TrustManager[] { new X509TrustManager() {
|
42
|
+
public void checkClientTrusted(X509Certificate[] cert, String authType) {
|
43
|
+
// everything is trusted
|
44
|
+
}
|
45
|
+
|
46
|
+
public void checkServerTrusted(X509Certificate[] cert, String authType) {
|
47
|
+
// everything is trusted
|
48
|
+
}
|
49
|
+
|
50
|
+
public X509Certificate[] getAcceptedIssuers() {
|
51
|
+
return new X509Certificate[0];
|
52
|
+
}
|
53
|
+
} }, null);
|
22
|
-
|
54
|
+
factory = (SSLSocketFactory) sslcontext.getSocketFactory();
|
55
|
+
} catch (Exception ex) {
|
56
|
+
// ignore
|
57
|
+
}
|
58
|
+
}
|
59
|
+
|
23
|
-
|
60
|
+
public static SocketFactory getDefault() {
|
61
|
+
return new DummySSLSocketFactory();
|
62
|
+
}
|
63
|
+
|
64
|
+
public Socket createSocket() throws IOException {
|
65
|
+
return factory.createSocket();
|
66
|
+
}
|
67
|
+
|
68
|
+
public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
|
69
|
+
return factory.createSocket(socket, s, i, flag);
|
70
|
+
}
|
71
|
+
|
72
|
+
public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {
|
73
|
+
return factory.createSocket(inaddr, i, inaddr1, j);
|
74
|
+
}
|
75
|
+
|
76
|
+
public Socket createSocket(InetAddress inaddr, int i) throws IOException {
|
77
|
+
return factory.createSocket(inaddr, i);
|
78
|
+
}
|
79
|
+
|
80
|
+
public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
|
81
|
+
return factory.createSocket(s, i, inaddr, j);
|
82
|
+
}
|
83
|
+
|
84
|
+
public Socket createSocket(String s, int i) throws IOException {
|
85
|
+
return factory.createSocket(s, i);
|
86
|
+
}
|
87
|
+
|
88
|
+
public String[] getDefaultCipherSuites() {
|
89
|
+
return factory.getDefaultCipherSuites();
|
90
|
+
}
|
91
|
+
|
92
|
+
public String[] getSupportedCipherSuites() {
|
24
|
-
|
93
|
+
return factory.getSupportedCipherSuites();
|
25
|
-
|
94
|
+
}
|
26
|
-
|
95
|
+
}
|
27
|
-
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
|
28
96
|
```
|
29
97
|
|
30
98
|
###試したこと
|
31
|
-
|
99
|
+
問題点は、SSLが秘密鍵を求めていることでした。
|
32
|
-
mainメソッドからこのクラスを呼んだ場合は問題なくメールを送信できていることから、
|
33
|
-
|
100
|
+
SSLはTrustManagerというクラスの中で秘密鍵を求めています。
|
34
|
-
|
101
|
+
そこで上記のダミーのSSLSocketFactoryを用意し、TrustManagerを求められる場所でX509TrustManagerインターフェイスをimplementした匿名クラスを作成し、そこで各メソッドをoverrideして秘密鍵を求めないように変更しました。
|
35
102
|
|
103
|
+
|
104
|
+
``` java
|
105
|
+
Properties props = new Properties();
|
106
|
+
props.put("mail.smtp.host", "smtp.gmail.com");
|
107
|
+
props.put("mail.smtp.socketFactory.port", "587");
|
108
|
+
// props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
|
109
|
+
props.put("mail.smtp.ssl.socketFactory.class","com.xxxxx.module.mail.DummySSLSocketFactory");
|
110
|
+
|
111
|
+
props.put("mail.smtp.auth", "true");
|
112
|
+
props.put("mail.smtp.port", "587");
|
113
|
+
props.put("mail.smtp.ssl.enable", "false");
|
114
|
+
props.put("mail.smtp.starttls.enable", "true");
|
115
|
+
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
|
116
|
+
|
117
|
+
```
|
118
|
+
|
119
|
+
|
120
|
+
結果、秘密鍵を求められることなくgmailでの送信が可能になりました。
|
121
|
+
ご協力ありがとうございました。
|
122
|
+
|
123
|
+
|
124
|
+
|
125
|
+
|
126
|
+
|
36
127
|
###補足情報(言語/FW/ツール等のバージョンなど)
|
37
128
|
・MacOS
|
38
129
|
・Java8
|