68. Test email notification (#7)
This commit is contained in:
@ -1,6 +1,8 @@
|
|||||||
package net.shyshkin.study.fullstack.supportportal.backend.service;
|
package net.shyshkin.study.fullstack.supportportal.backend.service;
|
||||||
|
|
||||||
|
import lombok.RequiredArgsConstructor;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
import org.springframework.core.env.Environment;
|
||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
|
|
||||||
import javax.mail.Message;
|
import javax.mail.Message;
|
||||||
@ -16,12 +18,19 @@ import static net.shyshkin.study.fullstack.supportportal.backend.constant.EmailC
|
|||||||
|
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Service
|
@Service
|
||||||
|
@RequiredArgsConstructor
|
||||||
public class EmailService {
|
public class EmailService {
|
||||||
|
|
||||||
|
private final Environment environment;
|
||||||
|
|
||||||
public void sendNewPasswordEmail(String firstName, String password, String email) throws MessagingException {
|
public void sendNewPasswordEmail(String firstName, String password, String email) throws MessagingException {
|
||||||
Message message = createEmail(firstName, password, email);
|
Message message = createEmail(firstName, password, email);
|
||||||
Transport transport = getEmailSession().getTransport(SIMPLE_MAIL_TRANSFER_PROTOCOL);
|
Transport transport = getEmailSession().getTransport(SIMPLE_MAIL_TRANSFER_PROTOCOL);
|
||||||
transport.connect(GMAIL_SMTP_SERVER, USERNAME, PASSWORD);
|
|
||||||
|
String mailUsername = environment.getProperty("PORTAL_MAIL_USERNAME", USERNAME);
|
||||||
|
String mailPassword = environment.getProperty("PORTAL_MAIL_PASSWORD", PASSWORD);
|
||||||
|
|
||||||
|
transport.connect(GMAIL_SMTP_SERVER, mailUsername, mailPassword);
|
||||||
transport.sendMessage(message, message.getAllRecipients());
|
transport.sendMessage(message, message.getAllRecipients());
|
||||||
transport.close();
|
transport.close();
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,6 +17,7 @@ import org.springframework.security.crypto.password.PasswordEncoder;
|
|||||||
import org.springframework.stereotype.Service;
|
import org.springframework.stereotype.Service;
|
||||||
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
import org.springframework.web.servlet.support.ServletUriComponentsBuilder;
|
||||||
|
|
||||||
|
import javax.mail.MessagingException;
|
||||||
import javax.transaction.Transactional;
|
import javax.transaction.Transactional;
|
||||||
import java.time.LocalDateTime;
|
import java.time.LocalDateTime;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
@ -37,6 +38,7 @@ public class UserServiceImpl implements UserService {
|
|||||||
private final UserRepository userRepository;
|
private final UserRepository userRepository;
|
||||||
private final PasswordEncoder passwordEncoder;
|
private final PasswordEncoder passwordEncoder;
|
||||||
private final LoginAttemptService loginAttemptService;
|
private final LoginAttemptService loginAttemptService;
|
||||||
|
private final EmailService emailService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@Transactional
|
@Transactional
|
||||||
@ -68,7 +70,6 @@ public class UserServiceImpl implements UserService {
|
|||||||
|
|
||||||
String rawPassword = generatePassword();
|
String rawPassword = generatePassword();
|
||||||
String encodedPassword = passwordEncoder.encode(rawPassword);
|
String encodedPassword = passwordEncoder.encode(rawPassword);
|
||||||
log.debug("Raw password: {}. Encoded password: {}", rawPassword, encodedPassword);
|
|
||||||
|
|
||||||
User newUser = User.builder()
|
User newUser = User.builder()
|
||||||
.email(email)
|
.email(email)
|
||||||
@ -86,7 +87,15 @@ public class UserServiceImpl implements UserService {
|
|||||||
.role(defaultRole.name())
|
.role(defaultRole.name())
|
||||||
.authorities(defaultRole.getAuthorities())
|
.authorities(defaultRole.getAuthorities())
|
||||||
.build();
|
.build();
|
||||||
return userRepository.save(newUser);
|
userRepository.save(newUser);
|
||||||
|
|
||||||
|
try {
|
||||||
|
emailService.sendNewPasswordEmail(newUser.getFirstName(), rawPassword, newUser.getEmail());
|
||||||
|
} catch (MessagingException exception) {
|
||||||
|
log.error("Can't send message", exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
return newUser;
|
||||||
}
|
}
|
||||||
|
|
||||||
private String getTemporaryProfileImageUrl() {
|
private String getTemporaryProfileImageUrl() {
|
||||||
|
|||||||
Reference in New Issue
Block a user