Skip to content

Commit

Permalink
mail
Browse files Browse the repository at this point in the history
  • Loading branch information
teachingprogramming committed Jul 3, 2018
1 parent 5af0a08 commit ad865de
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
1 change: 1 addition & 0 deletions build.gradle
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,4 +31,5 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-jdbc') compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('com.h2database:h2') compile('com.h2database:h2')
compile('org.springframework.boot:spring-boot-starter-security') compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-mail')
} }
40 changes: 40 additions & 0 deletions src/main/java/d00000/webapp/MailController.java
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,40 @@
package d00000.webapp;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.MailSender;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

@Controller
@RequestMapping("/mail")
public class MailController {

private MailSender mailSender;

/** コンストラクタ */
public MailController(@Autowired MailSender mailSender) {
this.mailSender = mailSender;
}

/** メール送信フォーム */
@GetMapping("/send")
public String sendGet() {
return "mail/send";
}

/** メール送信処理 */
@PostMapping("/send")
public String sendPost(@RequestParam("to") String to, @RequestParam("subject") String subject,
@RequestParam("text") String text) {
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(to);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(text);
this.mailSender.send(simpleMailMessage);
return "redirect:/mail/send";
}
}
6 changes: 6 additions & 0 deletions src/main/resources/application.properties
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -4,3 +4,9 @@ spring.datasource.driver-class-name = org.h2.Driver
spring.datasource.url = jdbc:h2:~/webappdb/database spring.datasource.url = jdbc:h2:~/webappdb/database
spring.datasource.username = sa spring.datasource.username = sa
spring.datasource.password = sa spring.datasource.password = sa
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=<Gmailのメールアドレス>
spring.mail.password=<アプリパスワード>
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
21 changes: 21 additions & 0 deletions src/main/resources/templates/mail/send.html
Original file line number Original file line Diff line number Diff line change
@@ -0,0 +1,21 @@
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"/>
<title>メール送信</title>
</head>
<body>
<h1>メール送信サンプル</h1>
<form th:action="@{/mail/send}" method="post">
<dl>
<dt>to</dt>
<dd><input name="to" type="email" style="width: 400px"/></dd>
<dt>subject</dt>
<dd><input name="subject" type="text" style="width: 400px"/></dd>
<dt>text</dt>
<dd><textarea name="text" style="width: 400px; height: 200px"></textarea></dd>
</dl>
<button type="submit">送信</button>
</form>
</body>
</html>

0 comments on commit ad865de

Please sign in to comment.