Skip to content
Permalink
Browse files

mail

  • Loading branch information...
teachingprogramming committed Jul 3, 2018
1 parent 5af0a08 commit ad865de5f54d6c0c2a4cd1f9900e754bf86e0a1b
@@ -31,4 +31,5 @@ dependencies {
compile('org.springframework.boot:spring-boot-starter-jdbc')
compile('com.h2database:h2')
compile('org.springframework.boot:spring-boot-starter-security')
compile('org.springframework.boot:spring-boot-starter-mail')
}
@@ -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";
}
}
@@ -4,3 +4,9 @@ spring.datasource.driver-class-name = org.h2.Driver
spring.datasource.url = jdbc:h2:~/webappdb/database
spring.datasource.username = 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
@@ -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.
You can’t perform that action at this time.