6
6
import org .springframework .mail .SimpleMailMessage ;
7
7
import org .springframework .mail .javamail .JavaMailSender ;
8
8
import org .springframework .mail .javamail .MimeMessageHelper ;
9
+ import org .springframework .scheduling .annotation .Async ;
10
+ import org .springframework .scheduling .annotation .AsyncResult ;
9
11
import org .springframework .stereotype .Service ;
10
12
import org .springframework .web .multipart .MultipartFile ;
11
13
import org .springframework .web .util .UriComponentsBuilder ;
26
28
import java .time .format .FormatStyle ;
27
29
import java .util .Collections ;
28
30
import java .util .Locale ;
31
+ import java .util .concurrent .CompletableFuture ;
32
+ import java .util .concurrent .Future ;
29
33
30
34
/**
31
35
* Created by mjacobson on 19/01/18.
@@ -43,8 +47,7 @@ public class EmailServiceImpl implements EmailService {
43
47
@ Autowired
44
48
private MessageSource messageSource ;
45
49
46
- private void sendSimpleMessage ( String subject , String content , InternetAddress to , InternetAddress replyTo , InternetAddress cc ) throws AddressException {
47
-
50
+ private Future <Void > sendSimpleMessage ( String subject , String content , InternetAddress to , InternetAddress replyTo , InternetAddress cc ) throws AddressException {
48
51
SimpleMailMessage email = new SimpleMailMessage ();
49
52
50
53
email .setSubject ( subject );
@@ -58,11 +61,10 @@ private void sendSimpleMessage( String subject, String content, InternetAddress
58
61
email .setCc ( cc .toString () );
59
62
}
60
63
61
- emailSender .send ( email );
62
-
64
+ return CompletableFuture .runAsync ( () -> emailSender .send ( email ) );
63
65
}
64
66
65
- private void sendMultipartMessage ( String subject , String content , InternetAddress to , InternetAddress replyTo , MultipartFile attachment ) throws MessagingException {
67
+ private Future < Void > sendMultipartMessage ( String subject , String content , InternetAddress to , InternetAddress replyTo , MultipartFile attachment ) throws MessagingException {
66
68
MimeMessage message = emailSender .createMimeMessage ();
67
69
MimeMessageHelper helper = new MimeMessageHelper ( message , true );
68
70
@@ -76,11 +78,11 @@ private void sendMultipartMessage( String subject, String content, InternetAddre
76
78
77
79
helper .addAttachment ( attachment .getOriginalFilename (), attachment );
78
80
79
- emailSender .send ( message );
81
+ return CompletableFuture . runAsync ( () -> emailSender .send ( message ) );
80
82
}
81
83
82
84
@ Override
83
- public void sendSupportMessage ( String message , String name , User user , String userAgent , MultipartFile attachment , Locale locale ) throws MessagingException {
85
+ public Future < Void > sendSupportMessage ( String message , String name , User user , String userAgent , MultipartFile attachment , Locale locale ) throws MessagingException {
84
86
InternetAddress replyTo = user .getVerifiedContactEmail ().orElseThrow ( () -> new MessagingException ( "Could not find a verified email address for user." ) );
85
87
String shortName = messageSource .getMessage ( "rdp.site.shortname" , new String []{ siteSettings .getHostUri ().toString () }, Locale .getDefault () );
86
88
String subject = messageSource .getMessage ( "EmailService.sendSupportMessage.subject" , new String []{ shortName }, locale );
@@ -90,14 +92,14 @@ public void sendSupportMessage( String message, String name, User user, String u
90
92
"Message: " + message + "\r \n " +
91
93
"File Attached: " + ( attachment != null && !attachment .getOriginalFilename ().equals ( "" ) );
92
94
if ( attachment == null ) {
93
- sendSimpleMessage ( subject , content , getAdminAddress (), replyTo , null );
95
+ return sendSimpleMessage ( subject , content , getAdminAddress (), replyTo , null );
94
96
} else {
95
- sendMultipartMessage ( subject , content , getAdminAddress (), replyTo , attachment );
97
+ return sendMultipartMessage ( subject , content , getAdminAddress (), replyTo , attachment );
96
98
}
97
99
}
98
100
99
101
@ Override
100
- public void sendResetTokenMessage ( User user , PasswordResetToken token , Locale locale ) throws MessagingException {
102
+ public Future < Void > sendResetTokenMessage ( User user , PasswordResetToken token , Locale locale ) throws MessagingException {
101
103
String url = UriComponentsBuilder .fromUri ( siteSettings .getHostUri () )
102
104
.path ( "updatePassword" )
103
105
.queryParam ( "id" , user .getId () )
@@ -116,11 +118,11 @@ public void sendResetTokenMessage( User user, PasswordResetToken token, Locale l
116
118
String content = messageSource .getMessage ( "EmailService.sendResetTokenMessage" , new String []{
117
119
user .getProfile ().getName (), url , dateTimeFormatter .format ( token .getExpiryDate ().toInstant () ) }, locale );
118
120
119
- sendSimpleMessage ( subject , content , to , null , null );
121
+ return sendSimpleMessage ( subject , content , to , null , null );
120
122
}
121
123
122
124
@ Override
123
- public void sendRegistrationMessage ( User user , VerificationToken token , Locale locale ) throws MessagingException {
125
+ public Future < Void > sendRegistrationMessage ( User user , VerificationToken token , Locale locale ) throws MessagingException {
124
126
String shortName = messageSource .getMessage ( "rdp.site.shortname" , new String []{ siteSettings .getHostUri ().toString () }, locale );
125
127
String registrationWelcome = messageSource .getMessage ( "rdp.site.email.registration-welcome" , new String []{ siteSettings .getHostUri ().toString (), shortName }, locale );
126
128
String registrationEnding = messageSource .getMessage ( "rdp.site.email.registration-ending" , new String []{ siteSettings .getContactEmail () }, locale );
@@ -136,11 +138,11 @@ public void sendRegistrationMessage( User user, VerificationToken token, Locale
136
138
String message = registrationWelcome + "\r \n \r \n " +
137
139
messageSource .getMessage ( "EmailService.sendRegistrationMessage" , new String []{ confirmationUrl }, locale ) + "\r \n \r \n " +
138
140
registrationEnding ;
139
- sendSimpleMessage ( subject , message , recipientAddress , null , null );
141
+ return sendSimpleMessage ( subject , message , recipientAddress , null , null );
140
142
}
141
143
142
144
@ Override
143
- public void sendContactEmailVerificationMessage ( User user , VerificationToken token , Locale locale ) throws MessagingException {
145
+ public Future < Void > sendContactEmailVerificationMessage ( User user , VerificationToken token , Locale locale ) throws MessagingException {
144
146
InternetAddress recipientAddress = new InternetAddress ( user .getProfile ().getContactEmail () );
145
147
String shortName = messageSource .getMessage ( "rdp.site.shortname" , new String []{ siteSettings .getHostUri ().toString () }, locale );
146
148
String subject = messageSource .getMessage ( "EmailService.sendContactEmailVerificationMessage.subject" , new String []{ shortName }, locale );
@@ -151,21 +153,21 @@ public void sendContactEmailVerificationMessage( User user, VerificationToken to
151
153
.encode ()
152
154
.toUriString ();
153
155
String message = messageSource .getMessage ( "EmailService.sendContactEmailVerificationMessage" , new String []{ confirmationUrl }, locale );
154
- sendSimpleMessage ( subject , message , recipientAddress , null , null );
156
+ return sendSimpleMessage ( subject , message , recipientAddress , null , null );
155
157
}
156
158
157
159
@ Override
158
- public void sendUserRegisteredEmail ( User user ) throws MessagingException {
160
+ public Future < Void > sendUserRegisteredEmail ( User user ) throws MessagingException {
159
161
// unfortunately, there's no way to tell the dmin locale
160
162
Locale locale = Locale .getDefault ();
161
163
String shortname = messageSource .getMessage ( "rdp.site.shortname" , null , locale );
162
164
String subject = messageSource .getMessage ( "EmailService.sendUserRegisteredEmail.subject" , new String []{ shortname }, locale );
163
165
String content = messageSource .getMessage ( "EmailService.sendUserRegisteredEmail" , new String []{ user .getEmail () }, locale );
164
- sendSimpleMessage ( subject , content , getAdminAddress (), null , null );
166
+ return sendSimpleMessage ( subject , content , getAdminAddress (), null , null );
165
167
}
166
168
167
169
@ Override
168
- public void sendUserGeneAccessRequest ( UserGene userGene , User replyTo , String reason ) throws MessagingException {
170
+ public Future < Void > sendUserGeneAccessRequest ( UserGene userGene , User replyTo , String reason ) throws MessagingException {
169
171
String viewUserUrl = UriComponentsBuilder .fromUri ( siteSettings .getHostUri () )
170
172
.path ( "userView/{userId}" )
171
173
.buildAndExpand ( Collections .singletonMap ( "userId" , replyTo .getId () ) )
@@ -179,7 +181,7 @@ public void sendUserGeneAccessRequest( UserGene userGene, User replyTo, String r
179
181
String subject = messageSource .getMessage ( "EmailService.sendUserGeneAccessRequest.subject" , new String []{ shortname }, locale );
180
182
String content = messageSource .getMessage ( "EmailService.sendUserGeneAccessRequest" ,
181
183
new String []{ replyTo .getProfile ().getFullName (), userGene .getSymbol (), reason , viewUserUrl }, locale );
182
- sendSimpleMessage ( subject , content , to , replyToAddress , getAdminAddress () );
184
+ return sendSimpleMessage ( subject , content , to , replyToAddress , getAdminAddress () );
183
185
}
184
186
185
187
private InternetAddress getAdminAddress () throws AddressException {
0 commit comments