Class EMailService
java.lang.Object
de.gustavblass.fsu.fmi.roombooking.service.EMailService
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic enumRepresents the templates available for e-mails to be sent to end users. -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate final @NonNull org.springframework.core.env.EnvironmentUsed by certain e-mail templates.private final @NonNull org.springframework.mail.javamail.JavaMailSenderUsed to actually send the e-mails.private final @NonNull org.springframework.context.MessageSourceUsed to translate the e-mail subject.private final @NonNull StringThe e-mail address of the account from which the e-mails are sent.private final @NonNull org.thymeleaf.TemplateEngineUsed to construct the actual content s of the e-mails. -
Constructor Summary
ConstructorsConstructorDescriptionEMailService(@NonNull org.springframework.mail.javamail.JavaMailSender mailSender, @NonNull String senderAddress, @NonNull org.springframework.context.MessageSource messageSource, @NonNull org.thymeleaf.TemplateEngine eMailTemplateEngine, @NonNull org.springframework.core.env.Environment environment) Constructs a newEMailService. -
Method Summary
Modifier and TypeMethodDescriptionvoidonEMailChange(@NonNull EMailChangeEvent event) Sends an e-mail to theEMailChangeEvent.EMailChange.actorasking them to open the link with theEMailChangeEvent.EMailChange.verificationToken, in order to verify their new e-mail address.voidonRegistration(@NonNull UnconfirmedRegistrationEvent event) Sends an e-mail to the newActorCreatedEvent.actorasking them to open the link with theUnconfirmedRegistrationEvent.verificationToken, in order to verify theirActor.eMailAddress.voidonReservationConfirmation(@NonNull ReservationConfirmedEvent event) Sends an e-mail to theReservation.applicanttelling them that their newReservationwas just accepted by the server.private voidprepareMail(@NonNull org.springframework.mail.javamail.MimeMessageHelper mail, @NonNull String recipient, @NonNull String subject, @NonNull Locale locale) Adds the given recipient and subject to the givenMimeMessageHelperin the givenLocale.private voidsendEMail(@NonNull String recipientAddress, @NonNull String subject, @NonNull EMailService.Template template, @NonNull org.thymeleaf.context.Context context, @NonNull Locale locale) Sends an e-mail to the given recipient's address, with the given subject and content in the given language.
-
Field Details
-
mailSender
@NonNull private final @NonNull org.springframework.mail.javamail.JavaMailSender mailSenderUsed to actually send the e-mails. -
senderAddress
The e-mail address of the account from which the e-mails are sent. -
messageSource
@NonNull private final @NonNull org.springframework.context.MessageSource messageSourceUsed to translate the e-mail subject. -
templateEngine
@NonNull private final @NonNull org.thymeleaf.TemplateEngine templateEngineUsed to construct the actual content s of the e-mails. -
environment
@NonNull private final @NonNull org.springframework.core.env.Environment environmentUsed by certain e-mail templates.
-
-
Constructor Details
-
EMailService
public EMailService(@NonNull @NonNull org.springframework.mail.javamail.JavaMailSender mailSender, @NonNull @Value("${spring.mail.username:}") @NonNull String senderAddress, @NonNull @NonNull org.springframework.context.MessageSource messageSource, @NonNull @NonNull org.thymeleaf.TemplateEngine eMailTemplateEngine, @NonNull @NonNull org.springframework.core.env.Environment environment) Constructs a newEMailService.- Parameters:
mailSender- ThemailSender.senderAddress- ThesenderAddress.messageSource- ThemessageSource.eMailTemplateEngine- ThetemplateEngine.environment- Theenvironment.
-
-
Method Details
-
onRegistration
@Async @EventListener public void onRegistration(@NonNull @NonNull UnconfirmedRegistrationEvent event) throws IllegalStateException, IOException Sends an e-mail to the newActorCreatedEvent.actorasking them to open the link with theUnconfirmedRegistrationEvent.verificationToken, in order to verify theirActor.eMailAddress.- Parameters:
event- The event when the newActorsigned up.- Throws:
IllegalStateException- If thesenderAddressorActor.eMailAddressis invalid for some reason or if the e-mail subject or text cannot be generated.IOException- If the e-mail could not be sent.
-
onEMailChange
@Async @EventListener public void onEMailChange(@NonNull @NonNull EMailChangeEvent event) throws IllegalStateException, IOException Sends an e-mail to theEMailChangeEvent.EMailChange.actorasking them to open the link with theEMailChangeEvent.EMailChange.verificationToken, in order to verify their new e-mail address.- Parameters:
event- The event when theActorrequested to have their e-mail address updated.- Throws:
IllegalStateException- If thesenderAddressor the new e-mail address is invalid for some reason or if the e-mail subject or text cannot be generated.IOException- If the e-mail could not be sent.
-
onReservationConfirmation
@Async @EventListener public void onReservationConfirmation(@NonNull @NonNull ReservationConfirmedEvent event) throws IllegalStateException, IOException Sends an e-mail to theReservation.applicanttelling them that their newReservationwas just accepted by the server. The e-mail includes useful information, especially the Reservation's details.- Parameters:
event- The event when theActor's Reservation was confirmed.- Throws:
IllegalStateException- If thesenderAddressor the new e-mail address is invalid for some reason or if the e-mail subject or text cannot be generated.IOException- If the e-mail could not be sent.
-
sendEMail
private void sendEMail(@NonNull @NonNull String recipientAddress, @NonNull @NonNull String subject, @NonNull @NonNull EMailService.Template template, @NonNull @NonNull org.thymeleaf.context.Context context, @NonNull @NonNull Locale locale) throws IllegalStateException, IOException Sends an e-mail to the given recipient's address, with the given subject and content in the given language.- Parameters:
recipientAddress- The e-mail address to which the e-mail shall be sent.subject- What the e-mail is about. Must only be a one-liner.template- TheEMailService.Templatethat shall be used to format the message.context- The content with which the template shall be filled.locale- The language in which the e-mail shall be sent.- Throws:
IllegalStateException- If thesenderAddressor the new e-mail address is invalid for some reason or if the e-mail subject or text cannot be generated.IOException- If the e-mail could not be sent.
-
prepareMail
private void prepareMail(@NonNull @NonNull org.springframework.mail.javamail.MimeMessageHelper mail, @NonNull @NonNull String recipient, @NonNull @NonNull String subject, @NonNull @NonNull Locale locale) throws IllegalStateException, de.gustavblass.commons.exceptions.IllegalArgumentException Adds the given recipient and subject to the givenMimeMessageHelperin the givenLocale.- Parameters:
mail- The e-mail that shall be prepared.recipient- The e-mail address to which themailwill be sent at some point (not by this method).subject- TheMimeMessage.getSubject().locale- The language in which the e-mail will be.- Throws:
IllegalStateException- If thesenderAddressis invalid or if the subject cannot be built.de.gustavblass.commons.exceptions.IllegalArgumentException- If the given recipient's e-mail address is invalid.
-