1 | package finki.it.phoneluxbackend.services;
|
---|
2 |
|
---|
3 | import finki.it.phoneluxbackend.security.email.EmailSender;
|
---|
4 | import lombok.AllArgsConstructor;
|
---|
5 | import org.slf4j.Logger;
|
---|
6 | import org.slf4j.LoggerFactory;
|
---|
7 | import org.springframework.mail.javamail.JavaMailSender;
|
---|
8 | import org.springframework.mail.javamail.MimeMessageHelper;
|
---|
9 | import org.springframework.scheduling.annotation.Async;
|
---|
10 | import org.springframework.stereotype.Service;
|
---|
11 |
|
---|
12 | import javax.mail.MessagingException;
|
---|
13 | import javax.mail.internet.MimeMessage;
|
---|
14 |
|
---|
15 | @Service
|
---|
16 | @AllArgsConstructor
|
---|
17 | public class EmailService implements EmailSender {
|
---|
18 |
|
---|
19 | private final JavaMailSender mailSender;
|
---|
20 | private final static Logger LOGGER = LoggerFactory.getLogger(EmailService.class);
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | @Override
|
---|
25 | @Async
|
---|
26 | public void send(String to, String email) {
|
---|
27 | try{
|
---|
28 | MimeMessage mimeMessage = mailSender.createMimeMessage();
|
---|
29 | MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,"utf-8");
|
---|
30 | helper.setText(email,true);
|
---|
31 | helper.setTo(to);
|
---|
32 | helper.setSubject("Confirm your email - Phonelux");
|
---|
33 | helper.setFrom("phonelux_official@gmail.com");
|
---|
34 | mailSender.send(mimeMessage);
|
---|
35 | } catch (MessagingException e){
|
---|
36 | LOGGER.error("Failed to send email ", e);
|
---|
37 | throw new IllegalStateException("Failed to send email");
|
---|
38 | }
|
---|
39 | }
|
---|
40 | }
|
---|