質問編集履歴

4

質問内容変更

2020/04/04 08:01

投稿

chocolate_pie
chocolate_pie

スコア26

test CHANGED
@@ -1 +1 @@
1
- Spring Boot thymeleafを使ってメールをHTML化したい
1
+ Spring メールを複数のアドレスに送信したい MimeMessageHelper
test CHANGED
@@ -1,56 +1,40 @@
1
1
  ### 前提・実現したいこと
2
2
 
3
- Spring Bootキスで送っいるメールをHTML化したいで
3
+ Springからthymeilafをンプレーに利用してHTMLメール送信る機能を実装
4
-
4
+
5
- 調べたところ、thymeleafを使っ実装でるようです
5
+ していたところ、toが1つのアドレスにしか対応しいないことに気づました
6
-
7
-
8
-
9
- 送信したいメールの条件は以下の通りです
6
+
10
-
11
- 0. 送りたいメールが何パターンもある
7
+
12
-
8
+
13
- 0. メールの本文の変を固定ではなく他クラスの変数引っ張って利用
9
+ これを、複送信するためにどこにどん処理追加すればよろしいでしょうか…?
14
-
15
-
16
-
17
- 以下のサイトを参考にしたのですが、
10
+
18
-
19
- 1パターンのメール送信、変数がベタ打ちされて固定されてしまっていて、上記の条件が実現できるか分かりません。
11
+
20
-
21
-
22
-
23
- 【参考サイト】
12
+
24
-
25
- [ThymeleafをメールテンプレートにしてHTMLメールを送信する](https://tech-lab.sios.jp/archives/11176)
26
-
27
- [Thymeleafをメールテンプレートとして使う](https://www.agilegroup.co.jp/technote/spring-thymeleaf-mail-template.html)
28
-
29
-
30
-
31
- そして、メールの本文に入れる変の値メインメソッドに記述されているのですが
13
+ メールアドレスを配列で取ってこないと複送信できないことは分かっているので、
14
+
32
-
15
+ 配列をどうやってtoに持ってくるかが知りたいです。
16
+
17
+
18
+
19
+ よろしくお願いします。
20
+
21
+
22
+
33
- そこでないと機能のでしょうか…?
23
+ ### 発生る問題・エラーメッセージ
34
-
35
-
36
-
24
+
37
- 送信したいメールの条件実現できる方法を教えてただきたいです、よろしくお願いします。
25
+ メールが複数人に送れな
38
26
 
39
27
 
40
28
 
41
29
  ### 該当のソースコード
42
30
 
43
- とりあえず、1パターンのメール送信、変数がベタ打ちで動いたHTMLメールのソースコードはこのようになっています。
31
+
44
-
45
- コメントアウトに要望を書きました。
32
+
46
-
47
-
48
-
49
- SendMailService.java
33
+ MailSendService.java
50
34
 
51
35
  ```java
52
36
 
53
- package jp.co.sss.food.service;
37
+ package com.paidmanage.service.primary;
54
38
 
55
39
 
56
40
 
@@ -92,7 +76,7 @@
92
76
 
93
77
 
94
78
 
95
- public void sendMail(Context context) {
79
+ public void sendMail(Context context,String email,String title,String htmlLocation) {
96
80
 
97
81
 
98
82
 
@@ -108,13 +92,13 @@
108
92
 
109
93
  StandardCharsets.UTF_8.name());
110
94
 
111
- helper.setFrom("");
95
+ helper.setFrom("hogehoge@sample");
112
-
96
+
113
- helper.setTo("");//他のコントローラーのメソッドで処理してる変数を入れたい
97
+ helper.setTo(email);
114
-
98
+
115
- helper.setSubject("商品一覧");//他のコントローラーのメソッドで処理してる変数を入れたい
99
+ helper.setSubject(title);
116
-
100
+
117
- helper.setText(getMailBody("samplemail", context), true);
101
+ helper.setText(getMailBody(htmlLocation, context), true);
118
102
 
119
103
  }
120
104
 
@@ -122,6 +106,8 @@
122
106
 
123
107
 
124
108
 
109
+
110
+
125
111
  }
126
112
 
127
113
 
@@ -146,7 +132,7 @@
146
132
 
147
133
  templateResolver.setTemplateMode(TemplateMode.HTML);
148
134
 
149
- templateResolver.setPrefix("mailtemplate/");//メールの種類によって参照するHTMLを変えたい
135
+ templateResolver.setPrefix("mailtemplate/");
150
136
 
151
137
  templateResolver.setSuffix(".html");
152
138
 
@@ -160,77 +146,45 @@
160
146
 
161
147
 
162
148
 
163
-
164
-
165
149
  }
166
150
 
167
151
 
168
152
 
169
153
  ```
170
154
 
171
- mainクラス
155
+ Controller.java
172
156
 
173
157
  ```java
174
158
 
175
- package jp.co.sss.food;
176
-
177
-
178
-
179
-
180
-
181
-
182
-
183
- import org.springframework.beans.factory.annotation.Autowired;
184
-
185
- import org.springframework.boot.CommandLineRunner;
186
-
187
- import org.springframework.boot.SpringApplication;
188
-
189
- import org.springframework.boot.autoconfigure.SpringBootApplication;
190
-
191
- import org.thymeleaf.context.Context;
192
-
193
-
194
-
195
- import jp.co.sss.food.service.SendMailService;
196
-
197
-
198
-
199
- @SpringBootApplication
159
+ @Controller
200
-
160
+
201
- public class FoodApplication implements CommandLineRunner {
161
+ public class ItemController {
202
162
 
203
163
  @Autowired
204
164
 
205
165
  SendMailService sendMailService;
206
166
 
207
-
167
+ //メール送信
168
+
208
-
169
+ @RequestMapping("/sendmail")
170
+
209
- public static void main(String[] args) {
171
+ public String sendmail() {
210
-
211
- SpringApplication.run(FoodApplication.class, args);
172
+
212
-
213
- }
214
-
215
-
216
-
217
- @Override
218
-
219
- public void run(String... args) throws Exception {
220
-
221
-
222
-
223
- //出力内容設定
173
+ //出力内容設定($で渡す値)
224
174
 
225
175
  Context context = new Context();
226
176
 
227
- context.setVariable("name", "ほげ");//他のコントローラーのメソッドで処理してる変数を入れたい
177
+ context.setVariable("name", "ほげ");
178
+
228
-
179
+ context.setVariable("name2", "ほげほげ");
180
+
181
+ String email="×××@co.jp";
182
+
229
- //メー送信←これだとアプリ起動毎のみにメーが送信されるので、コントローラーかサービスに移動させたい
183
+ //内容、宛先、タイト、HTMLファイ指定
230
-
184
+
231
- sendMailService.sendMail(context);
185
+ sendMailService.sendMail(context,email,"コントローラーから","samplemail");
186
+
232
-
187
+ return "redirect:/";
233
-
234
188
 
235
189
  }
236
190
 
@@ -240,9 +194,9 @@
240
194
 
241
195
  ```
242
196
 
243
- /resources/mailtemplate/samplemail.html
197
+ samplemail.html
244
-
198
+
245
- ```html
199
+ ```java
246
200
 
247
201
  <!DOCTYPE html>
248
202
 
@@ -250,9 +204,11 @@
250
204
 
251
205
  <body>
252
206
 
207
+ <h2 style="margin-bottom: 20px;" th:inline="text" th:text="|${name}様|"></h2>
208
+
253
- <h2 style="margin-bottom: 20px;" th:inline="text">[[${name}]]様</h2>
209
+ <h2 style="margin-bottom: 20px;" th:inline="text">[[${name2}]]様</h2>
254
-
210
+
255
- <p>商品の一覧です。ご確認ください。</p>
211
+ <p>テストメール</p>
256
212
 
257
213
  </body>
258
214
 
@@ -260,191 +216,25 @@
260
216
 
261
217
  ```
262
218
 
263
- pom.xml
264
-
265
- ```xml
266
-
267
- <?xml version="1.0" encoding="UTF-8"?>
268
-
269
- <project xmlns="http://maven.apache.org/POM/4.0.0"
270
-
271
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
272
-
273
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
274
-
275
- <modelVersion>4.0.0</modelVersion>
276
-
277
- <parent>
278
-
279
- <groupId>org.springframework.boot</groupId>
280
-
281
- <artifactId>spring-boot-starter-parent</artifactId>
282
-
283
- <version>2.2.1.RELEASE</version>
284
-
285
- <relativePath /> <!-- lookup parent from repository -->
286
-
287
- </parent>
288
-
289
- <groupId>jp.co.sss.food</groupId>
290
-
291
- <artifactId>food</artifactId>
292
-
293
- <version>0.0.1-SNAPSHOT</version>
294
-
295
- <name>food</name>
296
-
297
- <description>Demo project for Spring Boot</description>
298
-
299
-
300
-
301
- <properties>
302
-
303
- <java.version>1.8</java.version>
304
-
305
- </properties>
306
-
307
-
308
-
309
- <dependencies>
310
-
311
- <dependency>
312
-
313
- <groupId>org.springframework.boot</groupId>
314
-
315
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
316
-
317
- </dependency>
318
-
319
- <dependency>
320
-
321
- <groupId>org.springframework.boot</groupId>
322
-
323
- <artifactId>spring-boot-starter-web</artifactId>
324
-
325
- </dependency>
326
-
327
-
328
-
329
- <dependency>
330
-
331
- <groupId>org.springframework.boot</groupId>
332
-
333
- <artifactId>spring-boot-devtools</artifactId>
334
-
335
- <scope>runtime</scope>
336
-
337
- <optional>true</optional>
338
-
339
- </dependency>
340
-
341
- <dependency>
342
-
343
- <groupId>org.springframework.boot</groupId>
344
-
345
- <artifactId>spring-boot-starter-test</artifactId>
346
-
347
- <scope>test</scope>
348
-
349
- <exclusions>
350
-
351
- <exclusion>
352
-
353
- <groupId>org.junit.vintage</groupId>
354
-
355
- <artifactId>junit-vintage-engine</artifactId>
356
-
357
- </exclusion>
358
-
359
- </exclusions>
360
-
361
- </dependency>
362
-
363
-
364
-
365
- <!--JPAを使用するための設定(ここが原因でエラーが出る) -->
366
-
367
- <dependency>
368
-
369
- <groupId>org.springframework.boot</groupId>
370
-
371
- <artifactId>spring-boot-starter-data-jpa</artifactId>
372
-
373
- </dependency>
374
-
375
-
376
-
377
- <!--MySQLを使用するための設定 -->
378
-
379
- <dependency>
380
-
381
- <groupId>mysql</groupId>
382
-
383
- <artifactId>mysql-connector-java</artifactId>
384
-
385
- <!-- <version>8.0.18</version> -->
386
-
387
- </dependency>
388
-
389
-
390
-
391
- <dependency>
392
-
393
- <groupId>org.springframework.boot</groupId>
394
-
395
- <artifactId>spring-boot-starter-mail</artifactId>
396
-
397
- </dependency>
398
-
399
-
400
-
401
- <dependency>
402
-
403
- <groupId>org.springframework.boot</groupId>
404
-
405
- <artifactId>spring-boot-starter-data-jpa</artifactId>
406
-
407
- </dependency>
408
-
409
- <dependency>
410
-
411
- <groupId>org.springframework.security</groupId>
412
-
413
- <artifactId>spring-security-core</artifactId>
414
-
415
- </dependency>
416
-
417
- </dependencies>
418
-
419
-
420
-
421
- <build>
422
-
423
- <plugins>
424
-
425
- <plugin>
426
-
427
- <groupId>org.springframework.boot</groupId>
428
-
429
- <artifactId>spring-boot-maven-plugin</artifactId>
430
-
431
- </plugin>
432
-
433
- </plugins>
434
-
435
- </build>
436
-
437
-
438
-
439
- </project>
440
-
441
-
442
-
443
-
444
-
445
- ```
446
-
447
- テキストメール本文にある変数を引っ張って、別クラスの別メソッドでthymeleafの変数に入れることができるのでしょうか…?
219
+
220
+
221
+
222
+
223
+ ### 試したこと
224
+
225
+ 以下のサイトからMimeMessageを使った複数送信の例を発見したのですが、私の使っているものに
226
+
227
+ MimeMessage msg = new MimeMessage(session);が存在しないので使い方が分かりませんでした。
228
+
229
+ [【備忘録】String型の配列に格納されたメールアドレスをInternetAddress[]型に変換して複数の宛先にメールを送信【java】](http://puu-0328.hatenablog.com/entry/2017/04/17/170913)
230
+
231
+
232
+
233
+ [JavaMailでのメール送信まとめその1](https://ttshrk.hatenadiary.org/entry/20110405/1301998708)
234
+
235
+
236
+
237
+ そもそも、[ThymeleafをメールテンプレートにしてHTMLメールを送信する](https://tech-lab.sios.jp/archives/11176)を参考にメール機能を実装したのですがMailSendServiceで行っていることがほとんど理解できていません…
448
238
 
449
239
 
450
240
 
@@ -452,6 +242,6 @@
452
242
 
453
243
 
454
244
 
455
- pomにバージョン情報等載っています。
245
+
456
-
246
+
457
- 初心者で至らないてんがあると思いますがよろしくお願いします。
247
+ 初心者で至らないがあると思いますがよろしくお願いします。

3

pom修正

2020/04/04 08:01

投稿

chocolate_pie
chocolate_pie

スコア26

test CHANGED
File without changes
test CHANGED
@@ -266,7 +266,9 @@
266
266
 
267
267
  <?xml version="1.0" encoding="UTF-8"?>
268
268
 
269
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
269
+ <project xmlns="http://maven.apache.org/POM/4.0.0"
270
+
271
+ xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
270
272
 
271
273
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
272
274
 
@@ -278,19 +280,19 @@
278
280
 
279
281
  <artifactId>spring-boot-starter-parent</artifactId>
280
282
 
281
- <version>2.2.2.RELEASE</version>
283
+ <version>2.2.1.RELEASE</version>
282
-
284
+
283
- <relativePath/> <!-- lookup parent from repository -->
285
+ <relativePath /> <!-- lookup parent from repository -->
284
286
 
285
287
  </parent>
286
288
 
287
- <groupId>jp.co.itc.mbo</groupId>
289
+ <groupId>jp.co.sss.food</groupId>
288
-
290
+
289
- <artifactId>mboapp</artifactId>
291
+ <artifactId>food</artifactId>
290
292
 
291
293
  <version>0.0.1-SNAPSHOT</version>
292
294
 
293
- <name>mboapp</name>
295
+ <name>food</name>
294
296
 
295
297
  <description>Demo project for Spring Boot</description>
296
298
 
@@ -300,10 +302,6 @@
300
302
 
301
303
  <java.version>1.8</java.version>
302
304
 
303
- <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
304
-
305
- <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
306
-
307
305
  </properties>
308
306
 
309
307
 
@@ -314,63 +312,69 @@
314
312
 
315
313
  <groupId>org.springframework.boot</groupId>
316
314
 
315
+ <artifactId>spring-boot-starter-thymeleaf</artifactId>
316
+
317
+ </dependency>
318
+
319
+ <dependency>
320
+
321
+ <groupId>org.springframework.boot</groupId>
322
+
323
+ <artifactId>spring-boot-starter-web</artifactId>
324
+
325
+ </dependency>
326
+
327
+
328
+
329
+ <dependency>
330
+
331
+ <groupId>org.springframework.boot</groupId>
332
+
333
+ <artifactId>spring-boot-devtools</artifactId>
334
+
335
+ <scope>runtime</scope>
336
+
337
+ <optional>true</optional>
338
+
339
+ </dependency>
340
+
341
+ <dependency>
342
+
343
+ <groupId>org.springframework.boot</groupId>
344
+
345
+ <artifactId>spring-boot-starter-test</artifactId>
346
+
347
+ <scope>test</scope>
348
+
349
+ <exclusions>
350
+
351
+ <exclusion>
352
+
353
+ <groupId>org.junit.vintage</groupId>
354
+
355
+ <artifactId>junit-vintage-engine</artifactId>
356
+
357
+ </exclusion>
358
+
359
+ </exclusions>
360
+
361
+ </dependency>
362
+
363
+
364
+
365
+ <!--JPAを使用するための設定(ここが原因でエラーが出る) -->
366
+
367
+ <dependency>
368
+
369
+ <groupId>org.springframework.boot</groupId>
370
+
317
371
  <artifactId>spring-boot-starter-data-jpa</artifactId>
318
372
 
319
373
  </dependency>
320
374
 
321
- <dependency>
375
+
322
-
323
- <groupId>org.springframework.boot</groupId>
376
+
324
-
325
- <artifactId>spring-boot-starter-jdbc</artifactId>
326
-
327
- </dependency>
377
+ <!--MySQLを使用するための設定 -->
328
-
329
- <dependency>
330
-
331
- <groupId>org.springframework.boot</groupId>
332
-
333
- <artifactId>spring-boot-starter-security</artifactId>
334
-
335
- </dependency>
336
-
337
- <dependency>
338
-
339
- <groupId>org.thymeleaf.extras</groupId>
340
-
341
- <artifactId>thymeleaf-extras-springsecurity5</artifactId>
342
-
343
- </dependency>
344
-
345
- <dependency>
346
-
347
- <groupId>org.springframework.boot</groupId>
348
-
349
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
350
-
351
- </dependency>
352
-
353
- <dependency>
354
-
355
- <groupId>org.springframework.boot</groupId>
356
-
357
- <artifactId>spring-boot-starter-web</artifactId>
358
-
359
- </dependency>
360
-
361
-
362
-
363
- <dependency>
364
-
365
- <groupId>org.springframework.boot</groupId>
366
-
367
- <artifactId>spring-boot-devtools</artifactId>
368
-
369
- <scope>runtime</scope>
370
-
371
- <optional>true</optional>
372
-
373
- </dependency>
374
378
 
375
379
  <dependency>
376
380
 
@@ -378,79 +382,37 @@
378
382
 
379
383
  <artifactId>mysql-connector-java</artifactId>
380
384
 
381
- <scope>runtime</scope>
385
+ <!-- <version>8.0.18</version> -->
382
-
386
+
383
- </dependency>
387
+ </dependency>
384
-
388
+
389
+
390
+
385
- <dependency>
391
+ <dependency>
386
-
392
+
387
- <groupId>org.springframework.boot</groupId>
393
+ <groupId>org.springframework.boot</groupId>
388
-
394
+
389
- <artifactId>spring-boot-starter-test</artifactId>
395
+ <artifactId>spring-boot-starter-mail</artifactId>
390
-
391
- <scope>test</scope>
396
+
392
-
393
- <exclusions>
394
-
395
- <exclusion>
396
-
397
- <groupId>org.junit.vintage</groupId>
398
-
399
- <artifactId>junit-vintage-engine</artifactId>
400
-
401
- </exclusion>
402
-
403
- </exclusions>
404
-
405
- </dependency>
397
+ </dependency>
406
-
398
+
399
+
400
+
407
- <dependency>
401
+ <dependency>
402
+
403
+ <groupId>org.springframework.boot</groupId>
404
+
405
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
406
+
407
+ </dependency>
408
+
409
+ <dependency>
408
410
 
409
411
  <groupId>org.springframework.security</groupId>
410
412
 
411
- <artifactId>spring-security-test</artifactId>
413
+ <artifactId>spring-security-core</artifactId>
412
-
413
- <scope>test</scope>
414
+
414
-
415
- </dependency>
415
+ </dependency>
416
-
417
- <dependency>
418
-
419
- <groupId>org.webjars</groupId>
420
-
421
- <artifactId>bootstrap</artifactId>
422
-
423
- <version>3.3.5</version>
424
-
425
- </dependency>
426
-
427
- <dependency>
428
-
429
- <groupId>org.springframework.boot</groupId>
430
-
431
- <artifactId>spring-boot-starter-mail</artifactId>
432
-
433
- </dependency>
434
-
435
- <dependency>
436
-
437
- <groupId>org.webjars</groupId>
438
-
439
- <artifactId>jquery</artifactId>
440
-
441
- <version>3.4.1</version>
442
-
443
- </dependency>
444
-
445
- <dependency>
446
-
447
- <groupId>org.hibernate</groupId>
448
-
449
- <artifactId>hibernate-jpamodelgen</artifactId>
450
-
451
- </dependency>
452
-
453
-
454
416
 
455
417
  </dependencies>
456
418
 
@@ -478,6 +440,8 @@
478
440
 
479
441
 
480
442
 
443
+
444
+
481
445
  ```
482
446
 
483
447
  テキストメール本文にある変数を引っ張って、別クラスの別メソッドでthymeleafの変数に入れることができるのでしょうか…?

2

コード変更

2020/04/02 11:27

投稿

chocolate_pie
chocolate_pie

スコア26

test CHANGED
File without changes
test CHANGED
@@ -40,130 +40,226 @@
40
40
 
41
41
  ### 該当のソースコード
42
42
 
43
- メール送信してるクラス中のメドはこのような感じになっています。
43
+ とりあえず、1パターンのメール送信、変数がベタ打ちで動たHTMLメールのソースコードはこのようになっています。
44
+
45
+ コメントアウトに要望を書きました。
46
+
47
+
48
+
49
+ SendMailService.java
44
50
 
45
51
  ```java
46
52
 
53
+ package jp.co.sss.food.service;
54
+
55
+
56
+
57
+ import java.nio.charset.StandardCharsets;
58
+
59
+
60
+
61
+ import javax.mail.internet.MimeMessage;
62
+
63
+
64
+
65
+ import org.springframework.beans.factory.annotation.Autowired;
66
+
67
+ import org.springframework.mail.javamail.JavaMailSender;
68
+
69
+ import org.springframework.mail.javamail.MimeMessageHelper;
70
+
71
+ import org.springframework.mail.javamail.MimeMessagePreparator;
72
+
73
+ import org.springframework.stereotype.Service;
74
+
75
+ import org.thymeleaf.context.Context;
76
+
77
+ import org.thymeleaf.spring5.SpringTemplateEngine;
78
+
79
+ import org.thymeleaf.templatemode.TemplateMode;
80
+
81
+ import org.thymeleaf.templateresolver.ClassLoaderTemplateResolver;
82
+
83
+
84
+
47
- @Controller
85
+ @Service
48
-
49
- @RequestMapping("/mailer")
86
+
50
-
51
- public class MailController {
87
+ public class SendMailService {
52
-
53
-
54
-
55
- final String charset = "UTF-8";
56
-
57
- private final JavaMailSender javaMailSender;
58
-
59
-
60
88
 
61
89
  @Autowired
62
90
 
63
- MailController(JavaMailSender javaMailSender) {
64
-
65
- this.javaMailSender = javaMailSender;
91
+ private JavaMailSender javaMailSender;
92
+
93
+
94
+
95
+ public void sendMail(Context context) {
96
+
97
+
98
+
99
+ javaMailSender.send(new MimeMessagePreparator() {
100
+
101
+
102
+
103
+ @Override
104
+
105
+ public void prepare(MimeMessage mimeMessage) throws Exception {
106
+
107
+ MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,
108
+
109
+ StandardCharsets.UTF_8.name());
110
+
111
+ helper.setFrom("");
112
+
113
+ helper.setTo("");//他のコントローラーのメソッドで処理してる変数を入れたい
114
+
115
+ helper.setSubject("商品一覧");//他のコントローラーのメソッドで処理してる変数を入れたい
116
+
117
+ helper.setText(getMailBody("samplemail", context), true);
118
+
119
+ }
120
+
121
+ });
122
+
123
+
66
124
 
67
125
  }
68
126
 
69
127
 
70
128
 
71
- @RequestMapping(value = "/resubmit/{id}", method = RequestMethod.GET)
72
-
73
- public String resubmitMail(@PathVariable Integer id) {
129
+ private String getMailBody(String templateName, Context context) {
74
-
75
- UserMaster user = usermasterservice.findOne(id);
130
+
76
-
77
- String username = user.getFullname();
78
-
79
- String mailAddress = user.getMailaddress();
80
-
81
-
82
-
83
- SimpleMailMessage msg = new SimpleMailMessage();
131
+ SpringTemplateEngine templateEngine = new SpringTemplateEngine();
84
-
85
- msg.setFrom("hogehoge.com");
132
+
86
-
87
- msg.setTo(mailAddress);
88
-
89
- msg.setSubject("タイトル");//タイトルの設定
90
-
91
- String message = "【再提出通知】\n"
92
-
93
- + username + "さん"
94
-
95
- + "\n" + username + "さんの内容が再提出となりました。"
133
+ templateEngine.setTemplateResolver(mailTemplateResolver());
96
-
97
- + "\n 下記URLよりログインして内容を確認し、編集してください。"
134
+
98
-
99
- + "\n 【管理システム】"
100
-
101
- + "\n http://〇〇〇"
102
-
103
- ;
104
-
105
- msg.setText(message); //本文の設定
106
-
107
- javaMailSender.send(msg);
108
-
109
- return "redirect:/memberdetail/{id}";
135
+ return templateEngine.process(templateName, context);
136
+
137
+
110
138
 
111
139
  }
112
140
 
113
- @RequestMapping("/id_confirm/{id}")
141
+
114
-
142
+
115
- public String idConfirmMail(@PathVariable Integer id) {
143
+ private ClassLoaderTemplateResolver mailTemplateResolver() {
116
-
117
- //送信情報取得
144
+
118
-
119
- UserMaster user = usermasterservice.findOne(id);
145
+ ClassLoaderTemplateResolver templateResolver = new ClassLoaderTemplateResolver();
120
-
121
- String mailAddress=user.getMailaddress();
146
+
122
-
123
- String userId=user.getUsername();
124
-
125
-
126
-
127
- SimpleMailMessage msg = new SimpleMailMessage();
147
+ templateResolver.setTemplateMode(TemplateMode.HTML);
148
+
128
-
149
+ templateResolver.setPrefix("mailtemplate/");//メールの種類によって参照するHTMLを変えたい
150
+
129
- msg.setFrom("hogehoge.com");
151
+ templateResolver.setSuffix(".html");
130
-
131
- msg.setTo(mailAddress);
152
+
132
-
133
- msg.setSubject("ユーザーID通知(管理システム)");//タイトルの設定
134
-
135
- String message = "【ユーザーID通知】\n"
136
-
137
- + "ユーザーIDを忘れた方へ"
138
-
139
- + "\nあなたの登録されたユーザーIDは以下になります。"
140
-
141
- + "\n\n" +userId
142
-
143
- + "\n\nお忘れにならないようにご注意ください。"
144
-
145
- + "\n\n※ このメールに覚えの無い方は恐れ入りますが、本メールを破棄してください。"
146
-
147
- + "\n\n■管理トップページへ"
148
-
149
- + "\n http://〇〇〇"
150
-
151
- ;
152
-
153
- msg.setText(message); //本文の設定
154
-
155
- javaMailSender.send(msg);
156
-
157
- //完了画面を出す
158
-
159
- return "redirect:/id_confirm/received";
153
+ templateResolver.setCharacterEncoding("UTF-8");
154
+
155
+ templateResolver.setCacheable(true);
156
+
157
+ return templateResolver;
160
158
 
161
159
  }
162
160
 
161
+
162
+
163
+
164
+
163
-    }
165
+ }
166
+
167
+
164
168
 
165
169
  ```
166
170
 
171
+ mainクラス
172
+
173
+ ```java
174
+
175
+ package jp.co.sss.food;
176
+
177
+
178
+
179
+
180
+
181
+
182
+
183
+ import org.springframework.beans.factory.annotation.Autowired;
184
+
185
+ import org.springframework.boot.CommandLineRunner;
186
+
187
+ import org.springframework.boot.SpringApplication;
188
+
189
+ import org.springframework.boot.autoconfigure.SpringBootApplication;
190
+
191
+ import org.thymeleaf.context.Context;
192
+
193
+
194
+
195
+ import jp.co.sss.food.service.SendMailService;
196
+
197
+
198
+
199
+ @SpringBootApplication
200
+
201
+ public class FoodApplication implements CommandLineRunner {
202
+
203
+ @Autowired
204
+
205
+ SendMailService sendMailService;
206
+
207
+
208
+
209
+ public static void main(String[] args) {
210
+
211
+ SpringApplication.run(FoodApplication.class, args);
212
+
213
+ }
214
+
215
+
216
+
217
+ @Override
218
+
219
+ public void run(String... args) throws Exception {
220
+
221
+
222
+
223
+ //出力内容設定
224
+
225
+ Context context = new Context();
226
+
227
+ context.setVariable("name", "ほげ");//他のコントローラーのメソッドで処理してる変数を入れたい
228
+
229
+ //メール送信←これだとアプリ起動毎のみにメールが送信されるので、コントローラーかサービスに移動させたい
230
+
231
+ sendMailService.sendMail(context);
232
+
233
+
234
+
235
+ }
236
+
237
+ }
238
+
239
+
240
+
241
+ ```
242
+
243
+ /resources/mailtemplate/samplemail.html
244
+
245
+ ```html
246
+
247
+ <!DOCTYPE html>
248
+
249
+ <html xmlns:th="http://www.thymeleaf.org">
250
+
251
+ <body>
252
+
253
+ <h2 style="margin-bottom: 20px;" th:inline="text">[[${name}]]様</h2>
254
+
255
+ <p>商品の一覧です。ご確認ください。</p>
256
+
257
+ </body>
258
+
259
+ </html>
260
+
261
+ ```
262
+
167
263
  pom.xml
168
264
 
169
265
  ```xml

1

文章修正

2020/04/02 11:14

投稿

chocolate_pie
chocolate_pie

スコア26

test CHANGED
File without changes
test CHANGED
@@ -36,364 +36,354 @@
36
36
 
37
37
  送信したいメールの条件が実現できる方法を教えていただきたいです、よろしくお願いします。
38
38
 
39
+
40
+
39
- ### 発生している問題・エラメッセ
41
+ ### 該当のソスコ
42
+
40
-
43
+ メールを送信しているクラスの中のメソッドはこのような感じになっています。
44
+
41
-
45
+ ```java
46
+
47
+ @Controller
48
+
49
+ @RequestMapping("/mailer")
50
+
51
+ public class MailController {
52
+
53
+
54
+
55
+ final String charset = "UTF-8";
56
+
57
+ private final JavaMailSender javaMailSender;
58
+
59
+
60
+
61
+ @Autowired
62
+
63
+ MailController(JavaMailSender javaMailSender) {
64
+
65
+ this.javaMailSender = javaMailSender;
66
+
67
+ }
68
+
69
+
70
+
71
+ @RequestMapping(value = "/resubmit/{id}", method = RequestMethod.GET)
72
+
73
+ public String resubmitMail(@PathVariable Integer id) {
74
+
75
+ UserMaster user = usermasterservice.findOne(id);
76
+
77
+ String username = user.getFullname();
78
+
79
+ String mailAddress = user.getMailaddress();
80
+
81
+
82
+
83
+ SimpleMailMessage msg = new SimpleMailMessage();
84
+
85
+ msg.setFrom("hogehoge.com");
86
+
87
+ msg.setTo(mailAddress);
88
+
89
+ msg.setSubject("タイトル");//タイトルの設定
90
+
91
+ String message = "【再提出通知】\n"
92
+
93
+ + username + "さん"
94
+
95
+ + "\n" + username + "さんの内容が再提出となりました。"
96
+
97
+ + "\n 下記URLよりログインして内容を確認し、編集してください。"
98
+
99
+ + "\n 【管理システム】"
100
+
101
+ + "\n http://〇〇〇"
102
+
103
+ ;
104
+
105
+ msg.setText(message); //本文の設定
106
+
107
+ javaMailSender.send(msg);
108
+
109
+ return "redirect:/memberdetail/{id}";
110
+
111
+ }
112
+
113
+ @RequestMapping("/id_confirm/{id}")
114
+
115
+ public String idConfirmMail(@PathVariable Integer id) {
116
+
117
+ //送信情報取得
118
+
119
+ UserMaster user = usermasterservice.findOne(id);
120
+
121
+ String mailAddress=user.getMailaddress();
122
+
123
+ String userId=user.getUsername();
124
+
125
+
126
+
127
+ SimpleMailMessage msg = new SimpleMailMessage();
128
+
129
+ msg.setFrom("hogehoge.com");
130
+
131
+ msg.setTo(mailAddress);
132
+
133
+ msg.setSubject("ユーザーID通知(管理システム)");//タイトルの設定
134
+
135
+ String message = "【ユーザーID通知】\n"
136
+
137
+ + "ユーザーIDを忘れた方へ"
138
+
139
+ + "\nあなたの登録されたユーザーIDは以下になります。"
140
+
141
+ + "\n\n" +userId
142
+
143
+ + "\n\nお忘れにならないようにご注意ください。"
144
+
145
+ + "\n\n※ このメールに覚えの無い方は恐れ入りますが、本メールを破棄してください。"
146
+
147
+ + "\n\n■管理トップページへ"
148
+
149
+ + "\n http://〇〇〇"
150
+
151
+ ;
152
+
153
+ msg.setText(message); //本文の設定
154
+
155
+ javaMailSender.send(msg);
156
+
157
+ //完了画面を出す
158
+
159
+ return "redirect:/id_confirm/received";
160
+
161
+ }
162
+
163
+    }
42
164
 
43
165
  ```
44
166
 
45
- エラーメッセージ
167
+ pom.xml
168
+
169
+ ```xml
170
+
171
+ <?xml version="1.0" encoding="UTF-8"?>
172
+
173
+ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
174
+
175
+ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
176
+
177
+ <modelVersion>4.0.0</modelVersion>
178
+
179
+ <parent>
180
+
181
+ <groupId>org.springframework.boot</groupId>
182
+
183
+ <artifactId>spring-boot-starter-parent</artifactId>
184
+
185
+ <version>2.2.2.RELEASE</version>
186
+
187
+ <relativePath/> <!-- lookup parent from repository -->
188
+
189
+ </parent>
190
+
191
+ <groupId>jp.co.itc.mbo</groupId>
192
+
193
+ <artifactId>mboapp</artifactId>
194
+
195
+ <version>0.0.1-SNAPSHOT</version>
196
+
197
+ <name>mboapp</name>
198
+
199
+ <description>Demo project for Spring Boot</description>
200
+
201
+
202
+
203
+ <properties>
204
+
205
+ <java.version>1.8</java.version>
206
+
207
+ <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
208
+
209
+ <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
210
+
211
+ </properties>
212
+
213
+
214
+
215
+ <dependencies>
216
+
217
+ <dependency>
218
+
219
+ <groupId>org.springframework.boot</groupId>
220
+
221
+ <artifactId>spring-boot-starter-data-jpa</artifactId>
222
+
223
+ </dependency>
224
+
225
+ <dependency>
226
+
227
+ <groupId>org.springframework.boot</groupId>
228
+
229
+ <artifactId>spring-boot-starter-jdbc</artifactId>
230
+
231
+ </dependency>
232
+
233
+ <dependency>
234
+
235
+ <groupId>org.springframework.boot</groupId>
236
+
237
+ <artifactId>spring-boot-starter-security</artifactId>
238
+
239
+ </dependency>
240
+
241
+ <dependency>
242
+
243
+ <groupId>org.thymeleaf.extras</groupId>
244
+
245
+ <artifactId>thymeleaf-extras-springsecurity5</artifactId>
246
+
247
+ </dependency>
248
+
249
+ <dependency>
250
+
251
+ <groupId>org.springframework.boot</groupId>
252
+
253
+ <artifactId>spring-boot-starter-thymeleaf</artifactId>
254
+
255
+ </dependency>
256
+
257
+ <dependency>
258
+
259
+ <groupId>org.springframework.boot</groupId>
260
+
261
+ <artifactId>spring-boot-starter-web</artifactId>
262
+
263
+ </dependency>
264
+
265
+
266
+
267
+ <dependency>
268
+
269
+ <groupId>org.springframework.boot</groupId>
270
+
271
+ <artifactId>spring-boot-devtools</artifactId>
272
+
273
+ <scope>runtime</scope>
274
+
275
+ <optional>true</optional>
276
+
277
+ </dependency>
278
+
279
+ <dependency>
280
+
281
+ <groupId>mysql</groupId>
282
+
283
+ <artifactId>mysql-connector-java</artifactId>
284
+
285
+ <scope>runtime</scope>
286
+
287
+ </dependency>
288
+
289
+ <dependency>
290
+
291
+ <groupId>org.springframework.boot</groupId>
292
+
293
+ <artifactId>spring-boot-starter-test</artifactId>
294
+
295
+ <scope>test</scope>
296
+
297
+ <exclusions>
298
+
299
+ <exclusion>
300
+
301
+ <groupId>org.junit.vintage</groupId>
302
+
303
+ <artifactId>junit-vintage-engine</artifactId>
304
+
305
+ </exclusion>
306
+
307
+ </exclusions>
308
+
309
+ </dependency>
310
+
311
+ <dependency>
312
+
313
+ <groupId>org.springframework.security</groupId>
314
+
315
+ <artifactId>spring-security-test</artifactId>
316
+
317
+ <scope>test</scope>
318
+
319
+ </dependency>
320
+
321
+ <dependency>
322
+
323
+ <groupId>org.webjars</groupId>
324
+
325
+ <artifactId>bootstrap</artifactId>
326
+
327
+ <version>3.3.5</version>
328
+
329
+ </dependency>
330
+
331
+ <dependency>
332
+
333
+ <groupId>org.springframework.boot</groupId>
334
+
335
+ <artifactId>spring-boot-starter-mail</artifactId>
336
+
337
+ </dependency>
338
+
339
+ <dependency>
340
+
341
+ <groupId>org.webjars</groupId>
342
+
343
+ <artifactId>jquery</artifactId>
344
+
345
+ <version>3.4.1</version>
346
+
347
+ </dependency>
348
+
349
+ <dependency>
350
+
351
+ <groupId>org.hibernate</groupId>
352
+
353
+ <artifactId>hibernate-jpamodelgen</artifactId>
354
+
355
+ </dependency>
356
+
357
+
358
+
359
+ </dependencies>
360
+
361
+
362
+
363
+ <build>
364
+
365
+ <plugins>
366
+
367
+ <plugin>
368
+
369
+ <groupId>org.springframework.boot</groupId>
370
+
371
+ <artifactId>spring-boot-maven-plugin</artifactId>
372
+
373
+ </plugin>
374
+
375
+ </plugins>
376
+
377
+ </build>
378
+
379
+
380
+
381
+ </project>
382
+
383
+
46
384
 
47
385
  ```
48
386
 
49
-
50
-
51
- ### 該当のソースコード
52
-
53
- メールを送信しているクラスの中のメソッドはこのような感じになっています。
54
-
55
- ```java
56
-
57
- @Controller
58
-
59
- @RequestMapping("/mailer")
60
-
61
- public class MailController {
62
-
63
-
64
-
65
- final String charset = "UTF-8";
66
-
67
- private final JavaMailSender javaMailSender;
68
-
69
-
70
-
71
- @Autowired
72
-
73
- MailController(JavaMailSender javaMailSender) {
74
-
75
- this.javaMailSender = javaMailSender;
76
-
77
- }
78
-
79
-
80
-
81
- @RequestMapping(value = "/resubmit/{id}", method = RequestMethod.GET)
82
-
83
- public String resubmitMail(@PathVariable Integer id) {
84
-
85
- UserMaster user = usermasterservice.findOne(id);
86
-
87
- String username = user.getFullname();
88
-
89
- String mailAddress = user.getMailaddress();
90
-
91
-
92
-
93
- SimpleMailMessage msg = new SimpleMailMessage();
94
-
95
- msg.setFrom("hogehoge.com");
96
-
97
- msg.setTo(mailAddress);
98
-
99
- msg.setSubject("タイトル");//タイトルの設定
100
-
101
- String message = "【再提出通知】\n"
102
-
103
- + username + "さん"
104
-
105
- + "\n" + username + "さんの内容が再提出となりました。"
106
-
107
- + "\n 下記URLよりログインして内容を確認し、編集してください。"
108
-
109
- + "\n 【管理システム】"
110
-
111
- + "\n http://〇〇〇"
112
-
113
- ;
114
-
115
- msg.setText(message); //本文の設定
116
-
117
- javaMailSender.send(msg);
118
-
119
- return "redirect:/memberdetail/{id}";
120
-
121
- }
122
-
123
- @RequestMapping("/id_confirm/{id}")
124
-
125
- public String idConfirmMail(@PathVariable Integer id) {
126
-
127
- //送信情報取得
128
-
129
- UserMaster user = usermasterservice.findOne(id);
130
-
131
- String mailAddress=user.getMailaddress();
132
-
133
- String userId=user.getUsername();
134
-
135
-
136
-
137
- SimpleMailMessage msg = new SimpleMailMessage();
138
-
139
- msg.setFrom("hogehoge.com");
140
-
141
- msg.setTo(mailAddress);
142
-
143
- msg.setSubject("ユーザーID通知(管理システム)");//タイトルの設定
144
-
145
- String message = "【ユーザーID通知】\n"
146
-
147
- + "ユーザーIDを忘れた方へ"
148
-
149
- + "\nあなたの登録されたユーザーIDは以下になります。"
150
-
151
- + "\n\n" +userId
152
-
153
- + "\n\nお忘れにならないようにご注意ください。"
154
-
155
- + "\n\n※ このメールに覚えの無い方は恐れ入りますが、本メールを破棄してください。"
156
-
157
- + "\n\n■管理トップページへ"
158
-
159
- + "\n http://〇〇〇"
160
-
161
- ;
162
-
163
- msg.setText(message); //本文の設定
164
-
165
- javaMailSender.send(msg);
166
-
167
- //完了画面を出す
168
-
169
- return "redirect:/id_confirm/received";
170
-
171
- }
172
-
173
-    }
174
-
175
- ```
176
-
177
- pom.xml
178
-
179
- ```xml
180
-
181
- <?xml version="1.0" encoding="UTF-8"?>
182
-
183
- <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
184
-
185
- xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
186
-
187
- <modelVersion>4.0.0</modelVersion>
188
-
189
- <parent>
190
-
191
- <groupId>org.springframework.boot</groupId>
192
-
193
- <artifactId>spring-boot-starter-parent</artifactId>
194
-
195
- <version>2.2.2.RELEASE</version>
196
-
197
- <relativePath/> <!-- lookup parent from repository -->
198
-
199
- </parent>
200
-
201
- <groupId>jp.co.itc.mbo</groupId>
202
-
203
- <artifactId>mboapp</artifactId>
204
-
205
- <version>0.0.1-SNAPSHOT</version>
206
-
207
- <name>mboapp</name>
208
-
209
- <description>Demo project for Spring Boot</description>
210
-
211
-
212
-
213
- <properties>
214
-
215
- <java.version>1.8</java.version>
216
-
217
- <thymeleaf.version>3.0.11.RELEASE</thymeleaf.version>
218
-
219
- <thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
220
-
221
- </properties>
222
-
223
-
224
-
225
- <dependencies>
226
-
227
- <dependency>
228
-
229
- <groupId>org.springframework.boot</groupId>
230
-
231
- <artifactId>spring-boot-starter-data-jpa</artifactId>
232
-
233
- </dependency>
234
-
235
- <dependency>
236
-
237
- <groupId>org.springframework.boot</groupId>
238
-
239
- <artifactId>spring-boot-starter-jdbc</artifactId>
240
-
241
- </dependency>
242
-
243
- <dependency>
244
-
245
- <groupId>org.springframework.boot</groupId>
246
-
247
- <artifactId>spring-boot-starter-security</artifactId>
248
-
249
- </dependency>
250
-
251
- <dependency>
252
-
253
- <groupId>org.thymeleaf.extras</groupId>
254
-
255
- <artifactId>thymeleaf-extras-springsecurity5</artifactId>
256
-
257
- </dependency>
258
-
259
- <dependency>
260
-
261
- <groupId>org.springframework.boot</groupId>
262
-
263
- <artifactId>spring-boot-starter-thymeleaf</artifactId>
264
-
265
- </dependency>
266
-
267
- <dependency>
268
-
269
- <groupId>org.springframework.boot</groupId>
270
-
271
- <artifactId>spring-boot-starter-web</artifactId>
272
-
273
- </dependency>
274
-
275
-
276
-
277
- <dependency>
278
-
279
- <groupId>org.springframework.boot</groupId>
280
-
281
- <artifactId>spring-boot-devtools</artifactId>
282
-
283
- <scope>runtime</scope>
284
-
285
- <optional>true</optional>
286
-
287
- </dependency>
288
-
289
- <dependency>
290
-
291
- <groupId>mysql</groupId>
292
-
293
- <artifactId>mysql-connector-java</artifactId>
294
-
295
- <scope>runtime</scope>
296
-
297
- </dependency>
298
-
299
- <dependency>
300
-
301
- <groupId>org.springframework.boot</groupId>
302
-
303
- <artifactId>spring-boot-starter-test</artifactId>
304
-
305
- <scope>test</scope>
306
-
307
- <exclusions>
308
-
309
- <exclusion>
310
-
311
- <groupId>org.junit.vintage</groupId>
312
-
313
- <artifactId>junit-vintage-engine</artifactId>
314
-
315
- </exclusion>
316
-
317
- </exclusions>
318
-
319
- </dependency>
320
-
321
- <dependency>
322
-
323
- <groupId>org.springframework.security</groupId>
324
-
325
- <artifactId>spring-security-test</artifactId>
326
-
327
- <scope>test</scope>
328
-
329
- </dependency>
330
-
331
- <dependency>
332
-
333
- <groupId>org.webjars</groupId>
334
-
335
- <artifactId>bootstrap</artifactId>
336
-
337
- <version>3.3.5</version>
338
-
339
- </dependency>
340
-
341
- <dependency>
342
-
343
- <groupId>org.springframework.boot</groupId>
344
-
345
- <artifactId>spring-boot-starter-mail</artifactId>
346
-
347
- </dependency>
348
-
349
- <dependency>
350
-
351
- <groupId>org.webjars</groupId>
352
-
353
- <artifactId>jquery</artifactId>
354
-
355
- <version>3.4.1</version>
356
-
357
- </dependency>
358
-
359
- <dependency>
360
-
361
- <groupId>org.hibernate</groupId>
362
-
363
- <artifactId>hibernate-jpamodelgen</artifactId>
364
-
365
- </dependency>
366
-
367
-
368
-
369
- </dependencies>
370
-
371
-
372
-
373
- <build>
374
-
375
- <plugins>
376
-
377
- <plugin>
378
-
379
- <groupId>org.springframework.boot</groupId>
380
-
381
- <artifactId>spring-boot-maven-plugin</artifactId>
382
-
383
- </plugin>
384
-
385
- </plugins>
386
-
387
- </build>
388
-
389
-
390
-
391
- </project>
392
-
393
-
394
-
395
- ```
396
-
397
387
  テキストメール本文にある変数を引っ張って、別クラスの別メソッドでthymeleafの変数に入れることができるのでしょうか…?
398
388
 
399
389