質問編集履歴

2

参考にしたサイトの追加

2018/08/02 08:58

投稿

yus3554
yus3554

スコア7

test CHANGED
File without changes
test CHANGED
@@ -22,6 +22,14 @@
22
22
 
23
23
 
24
24
 
25
+ 今回のコードを書くにあたって主に、
26
+
27
+ [JavaアプリケーションからEメールを送信するサンプルコード](https://qiita.com/rubytomato@github/items/b106ff8011bcad60bce2)
28
+
29
+ を参考にさせていただきました。
30
+
31
+
32
+
25
33
  以下はメールを送るメソッドです。メールアドレスやパスワードのところは*で埋めてます。
26
34
 
27
35
  実行にはEclipseを使用しています。

1

ソースコードとデバッグのログを追加しました。

2018/08/02 08:58

投稿

yus3554
yus3554

スコア7

test CHANGED
File without changes
test CHANGED
@@ -12,10 +12,326 @@
12
12
 
13
13
 
14
14
 
15
- メール送信のAPIはJavaMailを使用しています。
15
+ メール送信のAPIはJavaMail ver1.6.1を使用しています。
16
16
 
17
17
  現在macbook airで開発しており、今後は大学内のサーバPCを使うかもしれません。
18
18
 
19
19
 
20
20
 
21
21
  よろしくお願いします。
22
+
23
+
24
+
25
+ 以下はメールを送るメソッドです。メールアドレスやパスワードのところは*で埋めてます。
26
+
27
+ 実行にはEclipseを使用しています。
28
+
29
+
30
+
31
+ ```java
32
+
33
+ public void send(String subject, String content, String to) {
34
+
35
+
36
+
37
+ final String from = "*******@gmail.com";
38
+
39
+
40
+
41
+ // Google account mail address
42
+
43
+ final String username = "*******@gmail.com";
44
+
45
+ // Google App password
46
+
47
+ final String password = "*******";
48
+
49
+
50
+
51
+ final String charset = "UTF-8";
52
+
53
+
54
+
55
+ final String encoding = "base64";
56
+
57
+
58
+
59
+ // for gmail
60
+
61
+ String host = "smtp.gmail.com";
62
+
63
+ String port = "587";
64
+
65
+ String starttls = "true";
66
+
67
+
68
+
69
+ Properties props = new Properties();
70
+
71
+ props.put("mail.smtp.host", host);
72
+
73
+ props.put("mail.smtp.port", port);
74
+
75
+ props.put("mail.smtp.auth", "true");
76
+
77
+ props.put("mail.smtp.starttls.enable", starttls);
78
+
79
+
80
+
81
+ props.put("mail.smtp.connectiontimeout", "10000");
82
+
83
+ props.put("mail.smtp.timeout", "10000");
84
+
85
+
86
+
87
+ props.put("mail.debug", "true");
88
+
89
+
90
+
91
+ Session session = Session.getInstance(props,
92
+
93
+ new javax.mail.Authenticator() {
94
+
95
+ protected PasswordAuthentication getPasswordAuthentication() {
96
+
97
+ return new PasswordAuthentication(username, password);
98
+
99
+ }
100
+
101
+ });
102
+
103
+
104
+
105
+ try {
106
+
107
+ MimeMessage message = new MimeMessage(session);
108
+
109
+ message.setHeader("Content-Transfer-Encoding", encoding);
110
+
111
+
112
+
113
+ message.setSubject(subject, charset);
114
+
115
+
116
+
117
+ // Set From:
118
+
119
+ message.setFrom(new InternetAddress(from));
120
+
121
+ // Set ReplyTo:
122
+
123
+ message.setReplyTo(new Address[]{new InternetAddress(from)});
124
+
125
+ // Set To:
126
+
127
+ message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
128
+
129
+
130
+
131
+ message.setContent(content, "text/html; charset=" + charset);
132
+
133
+
134
+
135
+ Transport.send(message);
136
+
137
+
138
+
139
+ } catch (MessagingException e) {
140
+
141
+ throw new RuntimeException(e);
142
+
143
+ } catch (UnsupportedEncodingException e) {
144
+
145
+ throw new RuntimeException(e);
146
+
147
+ }
148
+
149
+
150
+
151
+ }
152
+
153
+ ```
154
+
155
+
156
+
157
+ 以下はJavaMailのデバッグ用のログです。
158
+
159
+
160
+
161
+ ```
162
+
163
+ DEBUG: JavaMail version 1.6.1
164
+
165
+ DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
166
+
167
+ DEBUG: Tables of loaded providers
168
+
169
+ DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle]}
170
+
171
+ DEBUG: Providers Listed By Protocol: {imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]}
172
+
173
+ DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
174
+
175
+ Thu Aug 02 01:54:33 JST 2018 WARN: Establishing SSL connection without server's identity verification is not recommended. According to MySQL 5.5.45+, 5.6.26+ and 5.7.6+ requirements SSL connection must be established by default if explicit option isn't set. For compliance with existing applications not using SSL the verifyServerCertificate property is set to 'false'. You need either to explicitly disable SSL by setting useSSL=false, or set useSSL=true and provide truststore for server certificate verification.
176
+
177
+ DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
178
+
179
+ DEBUG SMTP: need username and password for authentication
180
+
181
+ DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=******, password=<null>
182
+
183
+ DEBUG SMTP: useEhlo true, useAuth true
184
+
185
+ DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
186
+
187
+ 220 smtp.gmail.com ESMTP e21-v6sm39546145pfl.187 - gsmtp
188
+
189
+ DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
190
+
191
+ EHLO 192.168.3.4
192
+
193
+ 250-smtp.gmail.com at your service, [60.94.102.161]
194
+
195
+ 250-SIZE 35882577
196
+
197
+ 250-8BITMIME
198
+
199
+ 250-STARTTLS
200
+
201
+ 250-ENHANCEDSTATUSCODES
202
+
203
+ 250-PIPELINING
204
+
205
+ 250-CHUNKING
206
+
207
+ 250 SMTPUTF8
208
+
209
+ DEBUG SMTP: Found extension "SIZE", arg "35882577"
210
+
211
+ DEBUG SMTP: Found extension "8BITMIME", arg ""
212
+
213
+ DEBUG SMTP: Found extension "STARTTLS", arg ""
214
+
215
+ DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
216
+
217
+ DEBUG SMTP: Found extension "PIPELINING", arg ""
218
+
219
+ DEBUG SMTP: Found extension "CHUNKING", arg ""
220
+
221
+ DEBUG SMTP: Found extension "SMTPUTF8", arg ""
222
+
223
+ STARTTLS
224
+
225
+ 220 2.0.0 Ready to start TLS
226
+
227
+ EHLO 192.168.3.4
228
+
229
+ 250-smtp.gmail.com at your service, [60.94.102.161]
230
+
231
+ 250-SIZE 35882577
232
+
233
+ 250-8BITMIME
234
+
235
+ 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
236
+
237
+ 250-ENHANCEDSTATUSCODES
238
+
239
+ 250-PIPELINING
240
+
241
+ 250-CHUNKING
242
+
243
+ 250 SMTPUTF8
244
+
245
+ DEBUG SMTP: Found extension "SIZE", arg "35882577"
246
+
247
+ DEBUG SMTP: Found extension "8BITMIME", arg ""
248
+
249
+ DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
250
+
251
+ DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
252
+
253
+ DEBUG SMTP: Found extension "PIPELINING", arg ""
254
+
255
+ DEBUG SMTP: Found extension "CHUNKING", arg ""
256
+
257
+ DEBUG SMTP: Found extension "SMTPUTF8", arg ""
258
+
259
+ DEBUG SMTP: protocolConnect login, host=smtp.gmail.com, user=********@gmail.com, password=<non-null>
260
+
261
+ DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
262
+
263
+ DEBUG SMTP: Using mechanism LOGIN
264
+
265
+ DEBUG SMTP: AUTH LOGIN command trace suppressed
266
+
267
+ DEBUG SMTP: AUTH LOGIN succeeded
268
+
269
+ DEBUG SMTP: use8bit false
270
+
271
+ MAIL FROM:<********@gmail.com>
272
+
273
+ 250 2.1.0 OK e21-v6sm39546145pfl.187 - gsmtp
274
+
275
+ RCPT TO:<********@gmail.com>
276
+
277
+ 250 2.1.5 OK e21-v6sm39546145pfl.187 - gsmtp
278
+
279
+ DEBUG SMTP: Verified Addresses
280
+
281
+ DEBUG SMTP: ********@gmail.com
282
+
283
+ DATA
284
+
285
+ 354 Go ahead e21-v6sm39546145pfl.187 - gsmtp
286
+
287
+ Date: Thu, 2 Aug 2018 01:54:33 +0900 (JST)
288
+
289
+ From: =?UTF-8?B?44K544Kx44K444Ol44O844Or566h55CG44K344K544OG44Og?=
290
+
291
+ <********@gmail.com>
292
+
293
+ Reply-To: ********@gmail.com
294
+
295
+ To: ********@gmail.com
296
+
297
+ Message-ID: <423016959.1.1533142473874@[192.168.3.4]>
298
+
299
+ Subject: =?UTF-8?B?W+WGjemAgV0g5Lya6K2w44Gu6ZaL5YKs5pel56iL44Gr?=
300
+
301
+ =?UTF-8?B?44Gk44GE44GmKOimgeaxguiAhe+8muWkqueUsOaCoOS7iyk=?=
302
+
303
+ MIME-Version: 1.0
304
+
305
+ Content-Type: text/html; charset=UTF-8
306
+
307
+ Content-Transfer-Encoding: base64
308
+
309
+
310
+
311
+ PGh0bWw+PGJvZHk+4oC744GT44Gu44Oh44O844Or44Gv44CB44G+44Gg5YWl5Yqb44GV44KM44Gm
312
+
313
+ 44GE44Gq44GE5pa544Gr6YCB5L+h44GX44Gm44GE44G+44GZ44CCPGJyPjxicj4g5aSq55Sw5oKg
314
+
315
+ 5LuL44GV44KT44GL44KJ5YWl5Yqb6KaB5rGC44GM5bGK44GE44Gm44GE44G+44GZ44CCPGJyPjxi
316
+
317
+ cj48aHIgYWxpZ249ImxlZnQiIHdpZHRoPSI1NSUiPjxicj7kvJrorbDjgpLjgZfjgb7jgZnjgII8
318
+
319
+ YnI+PGhyIGFsaWduPSJsZWZ0IiB3aWR0aD0iNTUlIj48YnI+6L+U562U44Gv5Lul5LiL44GuVVJM
320
+
321
+ 44GL44KJ44CBMjAxOC0wOC0yMOOBvuOBp+OBq+OBiumhmOOBhOOBl+OBvuOBmeOAgjxicj48YSBo
322
+
323
+ cmVmPSJodHRwOi8vbG9jYWxob3N0OjgwODAvU2NoZWR1bGVNYW5hZ2VyL0Fuc3dlclBhZ2UvMHJr
324
+
325
+ NVBuRklqTCI+6L+U562U44Oa44O844K4PC9hPjxicj48YnI+PC9ib2R5PjwvaHRtbD4=
326
+
327
+ .
328
+
329
+ 250 2.0.0 OK 1533142477 e21-v6sm39546145pfl.187 - gsmtp
330
+
331
+ DEBUG SMTP: message successfully delivered to mail server
332
+
333
+ QUIT
334
+
335
+ 221 2.0.0 closing connection e21-v6sm39546145pfl.187 - gsmtp
336
+
337
+ ```