-
Notifications
You must be signed in to change notification settings - Fork 14
/
SpringBoot2Mail.md
165 lines (152 loc) · 5.5 KB
/
SpringBoot2Mail.md
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
# Spring Boot 2 Mail
## Maven
```xml
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-mail</artifactId>
</dependency>
```
## SMTP configuration
### Gmail
```
debug=true
spring.mail.host=smtp.gmail.com
spring.mail.port=25
spring.mail.username=admin@gmail.com
spring.mail.password=xxxxxx
# Other properties
spring.mail.properties.mail.debug=true
spring.mail.properties.mail.transport.protocol=smtp
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.connectiontimeout=5000
spring.mail.properties.mail.smtp.timeout=5000
spring.mail.properties.mail.smtp.writetimeout=5000
# TLS , port 587
spring.mail.properties.mail.smtp.starttls.enable=true
# SSL, post 465
#spring.mail.properties.mail.smtp.socketFactory.port = 465
#spring.mail.properties.mail.smtp.socketFactory.class = javax.net.ssl.SSLSocketFactory
```
>To be able to use two factor authentication as well as keep the “allow less secure apps” option off, it is highly recommended to use an app password instead of the Gmail password.
>Refer: https://support.google.com/accounts/answer/185833
### Outlook
```
spring.mail.host=smtp-mail.outlook.com
spring.mail.port=587
spring.mail.username=outlookuserid@outlook.com
spring.mail.password=xxxxxx
spring.mail.properties.mail.protocol=smtp
spring.mail.properties.mail.tls=true
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.ssl.trust=smtp-mail.outlook.com
```
### AWS SES
Find relevant configuration from your AWS settings page. [[Read More](https://docs.aws.amazon.com/ses/latest/DeveloperGuide/sending-email.html)]
```
spring.mail.host=email-smtp.us-east-1.amazonaws.com
spring.mail.port=465
spring.mail.username=xxxxxxxxxxx
spring.mail.password=xxxxxxxxxxx
spring.mail.properties.mail.smtp.starttls.enable=true
spring.mail.properties.mail.smtp.starttls.required=true
spring.mail.properties.mail.smtp.ssl.enavle=true
spring.mail.properties.mail.protocol=smtps
spring.mail.properties.mail.smtps.auth=true
```
## Pre-configured email templates
```java
@Configuration
public class EmailConfig {
@Bean
public SimpleMailMessage emailTemplate() {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo("user@gmail.com");
message.setFrom("admin@gmail.com");
message.setSubject("Important email");
message.setText("FATAL - Application crash. Save your job !!");
return message;
}
}
```
## Send simple email
```java
@Service("emailService")
public class EmailService {
@Autowired
private JavaMailSender mailSender;
@Autowired
private SimpleMailMessage preConfiguredMessage;
/**
* This method will send compose and send the message
* */
public void sendMail(String to, String subject, String body) {
SimpleMailMessage message = new SimpleMailMessage();
message.setTo(to);
message.setSubject(subject);
message.setText(body);
mailSender.send(message);
}
/**
* This method will send a pre-configured message
* */
public void sendPreConfiguredMail(String message) {
SimpleMailMessage mailMessage = new SimpleMailMessage(preConfiguredMessage);
mailMessage.setText(message);
mailSender.send(mailMessage);
}
}
```
## Send email with attachment
```
@Autowired
private JavaMailSender mailSender;
public void sendMailWithAttachment(String to, String subject, String body, String fileToAttach) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mimeMessage.setFrom(new InternetAddress("admin@gmail.com"));
mimeMessage.setSubject(subject);
mimeMessage.setText(body);
FileSystemResource file = new FileSystemResource(new File(fileToAttach));
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.addAttachment("logo.jpg", file);
}
};
try {
mailSender.send(preparator);
}
catch (MailException ex) {
// simply log it and go on...
System.err.println(ex.getMessage());
}
}
```
## Send email with inline images
Rich text include media content in between text content. To do so in emails, we have to use `MimeMessageHelper`‘s `addInline()` method.
```
@Autowired
private JavaMailSender mailSender;
public void sendMailWithInlineResources(String to, String subject, String fileToAttach) {
MimeMessagePreparator preparator = new MimeMessagePreparator() {
public void prepare(MimeMessage mimeMessage) throws Exception {
mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to));
mimeMessage.setFrom(new InternetAddress("admin@gmail.com"));
mimeMessage.setSubject(subject);
MimeMessageHelper helper = new MimeMessageHelper(mimeMessage, true);
helper.setText("<html><body><img src='cid:identifier1234'></body></html>", true);
FileSystemResource res = new FileSystemResource(new File(fileToAttach));
helper.addInline("identifier1234", res);
}
};
try {
mailSender.send(preparator);
}
catch (MailException ex) {
// simply log it and go on...
System.err.println(ex.getMessage());
}
}
```
## References
- [Spring boot – Send email with attachment](https://howtodoinjava.com/spring-boot2/send-email-with-attachment/)