質問編集履歴

1

自己解決

2016/10/26 02:53

投稿

takanori222
takanori222

スコア7

test CHANGED
File without changes
test CHANGED
@@ -1,4 +1,4 @@
1
- ###servletからjavax.mailを使ってgmailにメールを送りたい
1
+ ###【自己解決しました】servletからjavax.mailを使ってgmailにメールを送りたい
2
2
 
3
3
  現在、javax.mailの仕組みを利用してgmailに対してメールを送信する仕組みを構築しています。
4
4
 
@@ -34,23 +34,159 @@
34
34
 
35
35
  ```Java
36
36
 
37
- Properties props = new Properties();
38
-
39
- props.put("mail.smtp.host", "smtp.gmail.com");
40
-
41
- props.put("mail.smtp.socketFactory.port", "587");
42
-
43
- props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
44
-
45
- props.put("mail.smtp.auth", "true");
46
-
47
- props.put("mail.smtp.port", "587");
48
-
49
- props.put("mail.smtp.ssl.enable", "false");
50
-
51
- props.put("mail.smtp.starttls.enable", "true");
52
-
53
- props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
37
+ package com.xxxxx.module.mail;
38
+
39
+
40
+
41
+ import java.io.IOException;
42
+
43
+ import java.net.InetAddress;
44
+
45
+ import java.net.Socket;
46
+
47
+ import java.security.cert.X509Certificate;
48
+
49
+
50
+
51
+ import javax.net.SocketFactory;
52
+
53
+ import javax.net.ssl.SSLContext;
54
+
55
+ import javax.net.ssl.SSLSocketFactory;
56
+
57
+ import javax.net.ssl.TrustManager;
58
+
59
+ import javax.net.ssl.X509TrustManager;
60
+
61
+
62
+
63
+ /**
64
+
65
+ * DummySSLSocketFactory
66
+
67
+ */
68
+
69
+ public class DummySSLSocketFactory extends SSLSocketFactory {
70
+
71
+ private SSLSocketFactory factory;
72
+
73
+
74
+
75
+ public DummySSLSocketFactory() {
76
+
77
+ try {
78
+
79
+ SSLContext sslcontext = SSLContext.getInstance("TLS");
80
+
81
+ sslcontext.init(null, new TrustManager[] { new X509TrustManager() {
82
+
83
+ public void checkClientTrusted(X509Certificate[] cert, String authType) {
84
+
85
+ // everything is trusted
86
+
87
+ }
88
+
89
+
90
+
91
+ public void checkServerTrusted(X509Certificate[] cert, String authType) {
92
+
93
+ // everything is trusted
94
+
95
+ }
96
+
97
+
98
+
99
+ public X509Certificate[] getAcceptedIssuers() {
100
+
101
+ return new X509Certificate[0];
102
+
103
+ }
104
+
105
+ } }, null);
106
+
107
+ factory = (SSLSocketFactory) sslcontext.getSocketFactory();
108
+
109
+ } catch (Exception ex) {
110
+
111
+ // ignore
112
+
113
+ }
114
+
115
+ }
116
+
117
+
118
+
119
+ public static SocketFactory getDefault() {
120
+
121
+ return new DummySSLSocketFactory();
122
+
123
+ }
124
+
125
+
126
+
127
+ public Socket createSocket() throws IOException {
128
+
129
+ return factory.createSocket();
130
+
131
+ }
132
+
133
+
134
+
135
+ public Socket createSocket(Socket socket, String s, int i, boolean flag) throws IOException {
136
+
137
+ return factory.createSocket(socket, s, i, flag);
138
+
139
+ }
140
+
141
+
142
+
143
+ public Socket createSocket(InetAddress inaddr, int i, InetAddress inaddr1, int j) throws IOException {
144
+
145
+ return factory.createSocket(inaddr, i, inaddr1, j);
146
+
147
+ }
148
+
149
+
150
+
151
+ public Socket createSocket(InetAddress inaddr, int i) throws IOException {
152
+
153
+ return factory.createSocket(inaddr, i);
154
+
155
+ }
156
+
157
+
158
+
159
+ public Socket createSocket(String s, int i, InetAddress inaddr, int j) throws IOException {
160
+
161
+ return factory.createSocket(s, i, inaddr, j);
162
+
163
+ }
164
+
165
+
166
+
167
+ public Socket createSocket(String s, int i) throws IOException {
168
+
169
+ return factory.createSocket(s, i);
170
+
171
+ }
172
+
173
+
174
+
175
+ public String[] getDefaultCipherSuites() {
176
+
177
+ return factory.getDefaultCipherSuites();
178
+
179
+ }
180
+
181
+
182
+
183
+ public String[] getSupportedCipherSuites() {
184
+
185
+ return factory.getSupportedCipherSuites();
186
+
187
+ }
188
+
189
+ }
54
190
 
55
191
  ```
56
192
 
@@ -58,13 +194,59 @@
58
194
 
59
195
  ###試したこと
60
196
 
197
+ 問題点は、SSLが秘密鍵を求めていることでした。
198
+
61
- この部分以外ほぼ別からのソースのコピペあり、問題なと思われます。
199
+ SSLTrustManagerというクラスの秘密鍵を求めています。
200
+
62
-
201
+ そこで上記のダミーのSSLSocketFactoryを用意し、TrustManagerを求められる場所でX509TrustManagerインターフェイスをimplementした匿名クラスを作成し、そこで各メソッドをoverrideして秘密鍵を求めないように変更しました。
202
+
203
+
204
+
205
+
206
+
207
+ ``` java
208
+
63
- mainメソッドからこのクラスを呼んだ場合は問題なくメールを送信できていることから、
209
+ Properties props = new Properties();
210
+
64
-
211
+ props.put("mail.smtp.host", "smtp.gmail.com");
212
+
213
+ props.put("mail.smtp.socketFactory.port", "587");
214
+
215
+ // props.put("mail.smtp.socketFactory.class", "javax.net.SocketFactory");
216
+
217
+ props.put("mail.smtp.ssl.socketFactory.class","com.xxxxx.module.mail.DummySSLSocketFactory");
218
+
219
+
220
+
221
+ props.put("mail.smtp.auth", "true");
222
+
223
+ props.put("mail.smtp.port", "587");
224
+
225
+ props.put("mail.smtp.ssl.enable", "false");
226
+
227
+ props.put("mail.smtp.starttls.enable", "true");
228
+
229
+ props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
230
+
231
+
232
+
233
+ ```
234
+
235
+
236
+
237
+
238
+
65
- 設定に何か追加することが必要なのではいかと考えておりま
239
+ 結果、秘密鍵求められることなくgmailで送信が可能になりました
66
-
240
+
67
- 解決策をぜひ提示いただければと存じます
241
+ 協力ありがとうござました。
242
+
243
+
244
+
245
+
246
+
247
+
248
+
249
+
68
250
 
69
251
 
70
252