teratail header banner
teratail header banner
質問するログイン新規登録

質問編集履歴

2

参考にしたサイトの追加

2018/08/02 08:58

投稿

yus3554
yus3554

スコア7

title CHANGED
File without changes
body CHANGED
@@ -10,6 +10,10 @@
10
10
 
11
11
  よろしくお願いします。
12
12
 
13
+ 今回のコードを書くにあたって主に、
14
+ [JavaアプリケーションからEメールを送信するサンプルコード](https://qiita.com/rubytomato@github/items/b106ff8011bcad60bce2)
15
+ を参考にさせていただきました。
16
+
13
17
  以下はメールを送るメソッドです。メールアドレスやパスワードのところは*で埋めてます。
14
18
  実行にはEclipseを使用しています。
15
19
 

1

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

2018/08/02 08:58

投稿

yus3554
yus3554

スコア7

title CHANGED
File without changes
body CHANGED
@@ -5,7 +5,165 @@
5
5
  システム上、本文がそれぞれ違うものを数十通送る可能性もあるのですが、数十件も送っていると数分かかってしまうので、
6
6
  できるだけ早く送信したいのですが何かいい方法はないでしょうか。
7
7
 
8
- メール送信のAPIはJavaMailを使用しています。
8
+ メール送信のAPIはJavaMail ver1.6.1を使用しています。
9
9
  現在macbook airで開発しており、今後は大学内のサーバPCを使うかもしれません。
10
10
 
11
- よろしくお願いします。
11
+ よろしくお願いします。
12
+
13
+ 以下はメールを送るメソッドです。メールアドレスやパスワードのところは*で埋めてます。
14
+ 実行にはEclipseを使用しています。
15
+
16
+ ```java
17
+ public void send(String subject, String content, String to) {
18
+
19
+ final String from = "*******@gmail.com";
20
+
21
+ // Google account mail address
22
+ final String username = "*******@gmail.com";
23
+ // Google App password
24
+ final String password = "*******";
25
+
26
+ final String charset = "UTF-8";
27
+
28
+ final String encoding = "base64";
29
+
30
+ // for gmail
31
+ String host = "smtp.gmail.com";
32
+ String port = "587";
33
+ String starttls = "true";
34
+
35
+ Properties props = new Properties();
36
+ props.put("mail.smtp.host", host);
37
+ props.put("mail.smtp.port", port);
38
+ props.put("mail.smtp.auth", "true");
39
+ props.put("mail.smtp.starttls.enable", starttls);
40
+
41
+ props.put("mail.smtp.connectiontimeout", "10000");
42
+ props.put("mail.smtp.timeout", "10000");
43
+
44
+ props.put("mail.debug", "true");
45
+
46
+ Session session = Session.getInstance(props,
47
+ new javax.mail.Authenticator() {
48
+ protected PasswordAuthentication getPasswordAuthentication() {
49
+ return new PasswordAuthentication(username, password);
50
+ }
51
+ });
52
+
53
+ try {
54
+ MimeMessage message = new MimeMessage(session);
55
+ message.setHeader("Content-Transfer-Encoding", encoding);
56
+
57
+ message.setSubject(subject, charset);
58
+
59
+ // Set From:
60
+ message.setFrom(new InternetAddress(from));
61
+ // Set ReplyTo:
62
+ message.setReplyTo(new Address[]{new InternetAddress(from)});
63
+ // Set To:
64
+ message.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
65
+
66
+ message.setContent(content, "text/html; charset=" + charset);
67
+
68
+ Transport.send(message);
69
+
70
+ } catch (MessagingException e) {
71
+ throw new RuntimeException(e);
72
+ } catch (UnsupportedEncodingException e) {
73
+ throw new RuntimeException(e);
74
+ }
75
+
76
+ }
77
+ ```
78
+
79
+ 以下はJavaMailのデバッグ用のログです。
80
+
81
+ ```
82
+ DEBUG: JavaMail version 1.6.1
83
+ DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
84
+ DEBUG: Tables of loaded providers
85
+ 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]}
86
+ 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]}
87
+ DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
88
+ 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.
89
+ DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
90
+ DEBUG SMTP: need username and password for authentication
91
+ DEBUG SMTP: protocolConnect returning false, host=smtp.gmail.com, user=******, password=<null>
92
+ DEBUG SMTP: useEhlo true, useAuth true
93
+ DEBUG SMTP: trying to connect to host "smtp.gmail.com", port 587, isSSL false
94
+ 220 smtp.gmail.com ESMTP e21-v6sm39546145pfl.187 - gsmtp
95
+ DEBUG SMTP: connected to host "smtp.gmail.com", port: 587
96
+ EHLO 192.168.3.4
97
+ 250-smtp.gmail.com at your service, [60.94.102.161]
98
+ 250-SIZE 35882577
99
+ 250-8BITMIME
100
+ 250-STARTTLS
101
+ 250-ENHANCEDSTATUSCODES
102
+ 250-PIPELINING
103
+ 250-CHUNKING
104
+ 250 SMTPUTF8
105
+ DEBUG SMTP: Found extension "SIZE", arg "35882577"
106
+ DEBUG SMTP: Found extension "8BITMIME", arg ""
107
+ DEBUG SMTP: Found extension "STARTTLS", arg ""
108
+ DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
109
+ DEBUG SMTP: Found extension "PIPELINING", arg ""
110
+ DEBUG SMTP: Found extension "CHUNKING", arg ""
111
+ DEBUG SMTP: Found extension "SMTPUTF8", arg ""
112
+ STARTTLS
113
+ 220 2.0.0 Ready to start TLS
114
+ EHLO 192.168.3.4
115
+ 250-smtp.gmail.com at your service, [60.94.102.161]
116
+ 250-SIZE 35882577
117
+ 250-8BITMIME
118
+ 250-AUTH LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH
119
+ 250-ENHANCEDSTATUSCODES
120
+ 250-PIPELINING
121
+ 250-CHUNKING
122
+ 250 SMTPUTF8
123
+ DEBUG SMTP: Found extension "SIZE", arg "35882577"
124
+ DEBUG SMTP: Found extension "8BITMIME", arg ""
125
+ DEBUG SMTP: Found extension "AUTH", arg "LOGIN PLAIN XOAUTH2 PLAIN-CLIENTTOKEN OAUTHBEARER XOAUTH"
126
+ DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
127
+ DEBUG SMTP: Found extension "PIPELINING", arg ""
128
+ DEBUG SMTP: Found extension "CHUNKING", arg ""
129
+ DEBUG SMTP: Found extension "SMTPUTF8", arg ""
130
+ DEBUG SMTP: protocolConnect login, host=smtp.gmail.com, user=********@gmail.com, password=<non-null>
131
+ DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2
132
+ DEBUG SMTP: Using mechanism LOGIN
133
+ DEBUG SMTP: AUTH LOGIN command trace suppressed
134
+ DEBUG SMTP: AUTH LOGIN succeeded
135
+ DEBUG SMTP: use8bit false
136
+ MAIL FROM:<********@gmail.com>
137
+ 250 2.1.0 OK e21-v6sm39546145pfl.187 - gsmtp
138
+ RCPT TO:<********@gmail.com>
139
+ 250 2.1.5 OK e21-v6sm39546145pfl.187 - gsmtp
140
+ DEBUG SMTP: Verified Addresses
141
+ DEBUG SMTP: ********@gmail.com
142
+ DATA
143
+ 354 Go ahead e21-v6sm39546145pfl.187 - gsmtp
144
+ Date: Thu, 2 Aug 2018 01:54:33 +0900 (JST)
145
+ From: =?UTF-8?B?44K544Kx44K444Ol44O844Or566h55CG44K344K544OG44Og?=
146
+ <********@gmail.com>
147
+ Reply-To: ********@gmail.com
148
+ To: ********@gmail.com
149
+ Message-ID: <423016959.1.1533142473874@[192.168.3.4]>
150
+ Subject: =?UTF-8?B?W+WGjemAgV0g5Lya6K2w44Gu6ZaL5YKs5pel56iL44Gr?=
151
+ =?UTF-8?B?44Gk44GE44GmKOimgeaxguiAhe+8muWkqueUsOaCoOS7iyk=?=
152
+ MIME-Version: 1.0
153
+ Content-Type: text/html; charset=UTF-8
154
+ Content-Transfer-Encoding: base64
155
+
156
+ PGh0bWw+PGJvZHk+4oC744GT44Gu44Oh44O844Or44Gv44CB44G+44Gg5YWl5Yqb44GV44KM44Gm
157
+ 44GE44Gq44GE5pa544Gr6YCB5L+h44GX44Gm44GE44G+44GZ44CCPGJyPjxicj4g5aSq55Sw5oKg
158
+ 5LuL44GV44KT44GL44KJ5YWl5Yqb6KaB5rGC44GM5bGK44GE44Gm44GE44G+44GZ44CCPGJyPjxi
159
+ cj48aHIgYWxpZ249ImxlZnQiIHdpZHRoPSI1NSUiPjxicj7kvJrorbDjgpLjgZfjgb7jgZnjgII8
160
+ YnI+PGhyIGFsaWduPSJsZWZ0IiB3aWR0aD0iNTUlIj48YnI+6L+U562U44Gv5Lul5LiL44GuVVJM
161
+ 44GL44KJ44CBMjAxOC0wOC0yMOOBvuOBp+OBq+OBiumhmOOBhOOBl+OBvuOBmeOAgjxicj48YSBo
162
+ cmVmPSJodHRwOi8vbG9jYWxob3N0OjgwODAvU2NoZWR1bGVNYW5hZ2VyL0Fuc3dlclBhZ2UvMHJr
163
+ NVBuRklqTCI+6L+U562U44Oa44O844K4PC9hPjxicj48YnI+PC9ib2R5PjwvaHRtbD4=
164
+ .
165
+ 250 2.0.0 OK 1533142477 e21-v6sm39546145pfl.187 - gsmtp
166
+ DEBUG SMTP: message successfully delivered to mail server
167
+ QUIT
168
+ 221 2.0.0 closing connection e21-v6sm39546145pfl.187 - gsmtp
169
+ ```