Email module for sending emails.
- Works with Groovy and Java.
- Works with Spring Boot and regular Spring.
This will be the last email service I will ever write in my whole lifetime.
<dependency>
<groupId>com.github.choonchernlim</groupId>
<artifactId>spring-boot-mail</artifactId>
<version>0.1.4</version>
</dependency>| Property | Required? | Description |
|---|---|---|
| from | Yes | Sender email. |
| tos | Yes | Recipient email(s). |
| subject | Yes | Subject line. |
| text | Yes | Email message. |
| replyTo | No | Email for user to reply to. Default is value from from property. |
| ccs | No | Carbon copy emails. |
| bccs | No | Blind carbon copy emails. |
| attachments | No | Map of attachments when key is the filename and extension and value is the file. |
| isHtmlText | No | true to render message as HTML, otherwise false to render message as plain text. Default is false. |
| Method | Description |
|---|---|
send(MailBean) |
Sends email. |
sendException(MailBean, Exception) |
Sends email where email message contains mailBean.text and properly formatted caught exception. |
sendWebException(MailBean, Exception) |
Sends email where email message contains mailBean.text, properly formatted caught exception and pertinent information from HttpServletRequest object for debugging purpose. |
The email message will be formatted accordingly based on mailBean.isHtmlText.
Spring Boot automatically supplies JavaMailSender if any spring.mail namespace exists.
Typically, all you need is to specify your institution's SMTP hostname:-
spring.mail.host: [YOUR_SMTP_HOST]... or, if you are using Google Mail:-
spring.mail:
host: smtp.gmail.com
username: [YOUR_EMAIL]@gmail.com
password: [YOUR_PASSWORD]
properties.mail.smtp:
auth: true
socketFactory:
port: 465
class: javax.net.ssl.SSLSocketFactory
fallback: false... but to get it working properly using Google Mail, go to https://myaccount.google.com/secureaccount first and set "Allow less secure apps" to "ON".
Create a Spring Configuration that imports SpringBootMailConfig:-
@Configuration
@Import(SpringBootMailConfig)
class AppConfig {
}Besides importing SpringBootMailConfig, you also have to supply JavaMailSender with all the mail properties:-
@Configuration
@Import(SpringBootMailConfig)
class AppConfig {
@Bean
JavaMailSender javaMailSender() {
new JavaMailSenderImpl(
host: [YOUR_SMTP_HOST]
)
}
}At the simplest level, this is all you need to send an email:-
@Autowired
MailService mailService
mailService.send(MailBean.builder().
from('[email protected]').
tos(['[email protected]'] as Set).
subject('subject').
text('text').
build())