Skip to content

Commit 32b669b

Browse files
committed
Spring Boot form contact
1 parent 2b2fafb commit 32b669b

File tree

12 files changed

+338
-0
lines changed

12 files changed

+338
-0
lines changed
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
target
2+
out
3+
.settings
4+
.classpath
5+
.project
6+
.idea
7+
*.iml
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
<groupId>com.hellokoding.springboot</groupId>
5+
<artifactId>freemarker.form.contact</artifactId>
6+
<parent>
7+
<groupId>org.springframework.boot</groupId>
8+
<artifactId>spring-boot-starter-parent</artifactId>
9+
<version>2.1.4.RELEASE</version>
10+
</parent>
11+
12+
<properties>
13+
<java.version>1.8</java.version>
14+
</properties>
15+
16+
<dependencies>
17+
<dependency>
18+
<groupId>org.springframework.boot</groupId>
19+
<artifactId>spring-boot-starter-web</artifactId>
20+
</dependency>
21+
<dependency>
22+
<groupId>org.springframework.boot</groupId>
23+
<artifactId>spring-boot-starter-freemarker</artifactId>
24+
</dependency>
25+
<dependency>
26+
<groupId>com.sun.mail</groupId>
27+
<artifactId>javax.mail</artifactId>
28+
<version>1.6.0</version>
29+
</dependency>
30+
</dependencies>
31+
<build>
32+
<plugins>
33+
<plugin>
34+
<groupId>org.springframework.boot</groupId>
35+
<artifactId>spring-boot-maven-plugin</artifactId>
36+
</plugin>
37+
</plugins>
38+
</build>
39+
</project>
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.hellokoding.form;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class WebApplication {
8+
public static void main(String[] args) throws Exception {
9+
SpringApplication.run(WebApplication.class, args);
10+
}
11+
}
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
package com.hellokoding.form.model;
2+
3+
import org.springframework.boot.context.properties.ConfigurationProperties;
4+
import org.springframework.stereotype.Component;
5+
6+
@Component
7+
@ConfigurationProperties(prefix = "mail")
8+
public class MailProperties {
9+
public static class SMTP {
10+
String host;
11+
String port;
12+
String username;
13+
String password;
14+
15+
public String getHost() {
16+
return host;
17+
}
18+
19+
public void setHost(String host) {
20+
this.host = host;
21+
}
22+
23+
public String getPort() {
24+
return port;
25+
}
26+
27+
public void setPort(String port) {
28+
this.port = port;
29+
}
30+
31+
public String getUsername() {
32+
return username;
33+
}
34+
35+
public void setUsername(String username) {
36+
this.username = username;
37+
}
38+
39+
public String getPassword() {
40+
return password;
41+
}
42+
43+
public void setPassword(String password) {
44+
this.password = password;
45+
}
46+
}
47+
48+
private SMTP smtp;
49+
private String from;
50+
private String fromName;
51+
private String to;
52+
53+
public SMTP getSmtp() {
54+
return smtp;
55+
}
56+
57+
public void setSmtp(SMTP smtp) {
58+
this.smtp = smtp;
59+
}
60+
61+
public String getFrom() {
62+
return from;
63+
}
64+
65+
public void setFrom(String from) {
66+
this.from = from;
67+
}
68+
69+
public String getTo() {
70+
return to;
71+
}
72+
73+
public void setTo(String to) {
74+
this.to = to;
75+
}
76+
77+
public String getFromName() {
78+
return fromName;
79+
}
80+
81+
public void setFromName(String fromName) {
82+
this.fromName = fromName;
83+
}
84+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.hellokoding.form.model;
2+
3+
import javax.validation.constraints.Email;
4+
import javax.validation.constraints.NotEmpty;
5+
6+
public class User {
7+
@NotEmpty
8+
private String name;
9+
10+
@NotEmpty
11+
private String message;
12+
13+
@NotEmpty
14+
@Email
15+
private String email;
16+
17+
public String getName() {
18+
return name;
19+
}
20+
21+
public void setName(String name) {
22+
this.name = name;
23+
}
24+
25+
public String getEmail() {
26+
return email;
27+
}
28+
29+
public void setEmail(String email) {
30+
this.email = email;
31+
}
32+
33+
public String getMessage() {
34+
return message;
35+
}
36+
37+
public void setMessage(String message) {
38+
this.message = message;
39+
}
40+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.hellokoding.form.service;
2+
3+
import com.hellokoding.form.model.MailProperties;
4+
import org.springframework.beans.factory.annotation.Autowired;
5+
import org.springframework.stereotype.Service;
6+
7+
import javax.mail.Message;
8+
import javax.mail.Session;
9+
import javax.mail.Transport;
10+
import javax.mail.internet.InternetAddress;
11+
import javax.mail.internet.MimeMessage;
12+
import java.util.Properties;
13+
import java.util.logging.Level;
14+
import java.util.logging.Logger;
15+
16+
@Service
17+
public class SESService implements SendingMailService {
18+
private final MailProperties mailProperties;
19+
20+
@Autowired
21+
SESService(MailProperties mailProperties){
22+
this.mailProperties = mailProperties;
23+
}
24+
25+
public boolean sendMail(String subject, String body) {
26+
try {
27+
Properties props = System.getProperties();
28+
props.put("mail.transport.protocol", "smtp");
29+
props.put("mail.smtp.port", mailProperties.getSmtp().getPort());
30+
props.put("mail.smtp.starttls.enable", "true");
31+
props.put("mail.smtp.auth", "true");
32+
33+
Session session = Session.getDefaultInstance(props);
34+
35+
MimeMessage msg = new MimeMessage(session);
36+
msg.setFrom(new InternetAddress(mailProperties.getFrom(), mailProperties.getFromName()));
37+
msg.setRecipient(Message.RecipientType.TO, new InternetAddress(mailProperties.getTo()));
38+
msg.setSubject(subject);
39+
msg.setContent(body, "text/html");
40+
41+
Transport transport = session.getTransport();
42+
transport.connect(mailProperties.getSmtp().getHost(), mailProperties.getSmtp().getUsername(), mailProperties.getSmtp().getPassword());
43+
transport.sendMessage(msg, msg.getAllRecipients());
44+
return true;
45+
} catch (Exception ex) {
46+
Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, ex.getMessage(), ex);
47+
}
48+
49+
return false;
50+
}
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
package com.hellokoding.form.service;
2+
3+
public interface SendingMailService {
4+
boolean sendMail(String subject, String body);
5+
}
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.hellokoding.form.web;
2+
3+
import com.hellokoding.form.model.User;
4+
import com.hellokoding.form.service.SendingMailService;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.stereotype.Controller;
7+
import org.springframework.ui.Model;
8+
import org.springframework.validation.BindingResult;
9+
import org.springframework.web.bind.annotation.GetMapping;
10+
import org.springframework.web.bind.annotation.PostMapping;
11+
12+
import javax.validation.Valid;
13+
14+
@Controller
15+
public class FormController {
16+
@Autowired
17+
SendingMailService sendingMailService;
18+
19+
@GetMapping("/")
20+
public String index() {
21+
return "redirect:/form";
22+
}
23+
24+
@GetMapping("/form")
25+
public String formGet(Model model) {
26+
model.addAttribute("user", new User());
27+
return "form";
28+
}
29+
30+
@PostMapping("/form")
31+
public String formPost(@Valid User user, BindingResult bindingResult, Model model) {
32+
if (bindingResult.hasErrors()) {
33+
return "form";
34+
}
35+
36+
model.addAttribute("noErrors", true);
37+
model.addAttribute("user", user);
38+
String subject = user.getName() + " " + user.getEmail() + " sent you a message";
39+
sendingMailService.sendMail(subject, user.getMessage());
40+
return "form";
41+
}
42+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
spring.freemarker.template-loader-path: classpath:/templates
2+
spring.freemarker.suffix: .ftl
3+
4+
mail.smtp.host={YOUR_SES_SMTP_HOST}
5+
mail.smtp.port={YOUR_SES_SMTP_PORT}
6+
mail.smtp.username={YOUR_SES_SMTP_USERNAME}
7+
mail.smtp.password={YOUR_SES_SMTP_PASSWORD}
8+
mail.from={YOUR_FROM_EMAIL}
9+
mail.from-name={YOUR_FROM_NAME}
10+
mail.to={YOUR_TO_EMAIL}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
h2 {
2+
color: darkgreen;
3+
}
4+
5+
form b {
6+
color: red;
7+
}

0 commit comments

Comments
 (0)