-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
0 parents
commit c4d706d
Showing
3 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# EditorConfig helps developers define and maintain consistent | ||
# coding styles between different editors and IDEs | ||
# editorconfig.org | ||
|
||
root = true | ||
|
||
|
||
[*] | ||
|
||
# Change these settings to your own preference | ||
indent_style = space | ||
indent_size = 4 | ||
|
||
# We recommend you to keep these unchanged | ||
end_of_line = lf | ||
charset = utf-8 | ||
trim_trailing_whitespace = true | ||
insert_final_newline = true | ||
|
||
[*.md] | ||
trim_trailing_whitespace = false | ||
|
||
[*.yml] | ||
indent_style = space | ||
indent_size = 2 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
|
||
.idea/ | ||
*.iml | ||
target/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
# Mail Module for asynchronous mail shipping | ||
|
||
Persists the mail in a database and configures a scheduled task to actually send it. | ||
This module is auto configured and depends on spring mail. | ||
|
||
Currently only MongoDB is supported. For other databases implement the `MailStorage` interface. | ||
|
||
## Installation | ||
|
||
<dependency> | ||
<groupId>de.upsource</groupId> | ||
<artifactId>spring-mailing</artifactId> | ||
<version>1.0.0</version> | ||
</dependency> | ||
|
||
## Configuration | ||
See spring mail configuration. | ||
|
||
<http://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-email.html> | ||
|
||
It uses the default ThreadPoolTaskScheduler which comes by default with only one thread. | ||
To configure the scheduler define a configuration class which implements `SchedulingConfigurer`. | ||
For example: | ||
|
||
@Configuration | ||
@EnableScheduling | ||
@Profile("!test") | ||
public class TaskExecutionConfig implements SchedulingConfigurer { | ||
|
||
@Override | ||
public void configureTasks(ScheduledTaskRegistrar taskRegistrar) { | ||
ThreadPoolTaskScheduler threadPoolTaskScheduler = new ThreadPoolTaskScheduler(); | ||
threadPoolTaskScheduler.setPoolSize(3); | ||
threadPoolTaskScheduler.initialize(); | ||
taskRegistrar.setTaskScheduler(threadPoolTaskScheduler); | ||
} | ||
} | ||
|
||
## Usage | ||
|
||
Define a Mailer class in your project and inject the `MailService`. | ||
|
||
@Component | ||
public class MyMailer { | ||
|
||
private final MailService mailService; | ||
private final TemplateEngine templateEngine; | ||
private final MessageSource messageSource; | ||
|
||
@Autowired | ||
public MyMailer(MailService mailService, TemplateEngine templateEngine, Environment environment, MessageSource messageSource) { | ||
this.mailService = mailService; | ||
this.templateEngine = templateEngine; | ||
this.messageSource = messageSource; | ||
} | ||
|
||
public void sendMail() { | ||
|
||
Context ctx = createContext(); | ||
|
||
try { | ||
String subject = createSubject(); | ||
String content = createContent(ctx); | ||
|
||
MimeMessage mimeMessage = mailService.createMimeMessage(subject, from, to, content, true); | ||
|
||
mailService.scheduleMail(mimeMessage); | ||
|
||
} catch (Exception e) { | ||
log.error("Could not create mail!", e); | ||
} | ||
} | ||
} |