content: package com.alkl1m.taskmanager.event.listener;
  
  import com.alkl1m.taskmanager.event.RegistrationCompleteEvent;
  import com.alkl1m.taskmanager.service.user.UserService;
  import lombok.RequiredArgsConstructor;
  import lombok.extern.slf4j.Slf4j;
  import org.springframework.context.ApplicationListener;
  import org.springframework.mail.javamail.JavaMailSender;
  import org.springframework.stereotype.Component;
  import com.alkl1m.taskmanager.entity.User;
  import org.springframework.mail.javamail.MimeMessageHelper;
  import jakarta.mail.MessagingException;
  import jakarta.mail.internet.MimeMessage;
  import java.io.UnsupportedEncodingException;
  import java.util.UUID;
  
  
  @Slf4j
  @Component
  @RequiredArgsConstructor
  public class RegistrationCompleteEventListener implements ApplicationListener<RegistrationCompleteEvent> {
      private final UserService userService;
      private final JavaMailSender mailSender;
      private User theUser;
      @Override
      public void onApplicationEvent(RegistrationCompleteEvent event) {
          theUser = event.getUser();
          String verificationToken = UUID.randomUUID().toString();
          userService.saveUserVerificationToken(theUser, verificationToken);
          String url = event.getApplicationUrl()+"/api/auth/verifyEmail?token="+verificationToken;
          try {
              sendVerificationEmail(url);
          } catch (MessagingException | UnsupportedEncodingException e) {
              throw new RuntimeException(e);
          }
      }
      public void sendVerificationEmail(String url) throws MessagingException, UnsupportedEncodingException {
          String subject = "Email Verification";
          String mailContent = "<p> Hi, "+ theUser.getName()+ ", </p>"+
                  "<p>Thank you for registering with us,"+
                  "Please, follow the link below to complete your registration.</p>"+
                  "<a href=\"" +url+ "\">Verify your email to activate your account</a>"+
                  "<p> Thank you <br> Task Manager Service";
          sendEmail(subject, mailContent);
      }
  
      public void sendPasswordResetVerificationEmail(String url) throws MessagingException, UnsupportedEncodingException {
          String subject = "Password Reset Request Verification";
          String mailContent = "<p> Hi, "+ ", </p>"+
                  "<p><b>You recently requested to reset your password,</b>"+
                  "Please, follow the link below to complete the action.</p>"+
                  "<a href=\"" +url+ "\">Reset password</a>"+
                  "<p> Task Manager Service";
          sendEmail(subject, mailContent);
      }
  
      public void sendEmail(String subject, String mailContent) throws MessagingException, UnsupportedEncodingException {
          String senderName = "Task Manager Service";
          MimeMessage message = mailSender.createMimeMessage();
          var messageHelper = new MimeMessageHelper(message);
          log.info("Sending email to: {}", theUser.getEmail());
          messageHelper.setFrom("forworksomeprojects@gmail.com", senderName);
          messageHelper.setTo(theUser.getEmail());
          messageHelper.setSubject(subject);
          messageHelper.setText(mailContent, true);
          mailSender.send(message);
      }
  }
  