Reputation: 11
I'm trying to send a mail to the user after logging in. I'm working with Spring as the back-end. I've implemented a @GetMapping in my UserController, which calls the emailservice.mfaSenden method, sending 6 random digits.
Can someone help me out to solve this "third party problem"?
My EmailService class
package com.sep.cardgame.email;
import com.sep.cardgame.user.User;
import com.sep.cardgame.user.UserService;
import lombok.RequiredArgsConstructor;
import org.simplejavamail.api.mailer.Mailer;
import org.simplejavamail.email.EmailBuilder;
import org.simplejavamail.mailer.MailerBuilder;
import org.simplejavamail.api.email.Email;
import org.springframework.stereotype.Service;
import java.util.Random;
@Service
@RequiredArgsConstructor
public class EmailService {
private final AppConfigs appConfigs;
private final UserService userService;
public void mfaSenden (User user) {
Mailer mailer = MailerBuilder
.withSMTPServer("localhost", 1025)
.withDebugLogging(true)
.async()
.buildMailer();
Random rand = new Random();
int mfa = 100000 + rand.nextInt(900000);
user.setMfaCode(mfa);
userService.updateUser(user);
Email email = EmailBuilder
.startingBlank()
.to(user.getUsername(), user.getEmail())
.from("noReply", "[email protected]")
.withPlainText("Ihr 2FA Code lautet: " + mfa )
.buildEmail();
mailer.sendMail(email);
//erwer
}
}
My UserController
@GetMapping("/login/{username}/{password}")
public ResponseEntity<User> login(@PathVariable("username") String username, @PathVariable("password") String password) {
try{
if(userService.findUserBySearchUsername(username) == null)
{
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
else
{
User user = userService.findUserByUsername(username);
String correctPassword = user.getPassword();
if(correctPassword.equals(password))
{
if(user.isMfaActivated())
{
emailService.mfaSenden(user);
return new ResponseEntity<>(user, HttpStatus.OK);
}
return new ResponseEntity<>(user, HttpStatus.OK);
}
else
return new ResponseEntity<>(null, HttpStatus.UNAUTHORIZED);
}
}
catch(UserNotFoundException e)
{
System.out.println("note: user not in db");
return new ResponseEntity<>(null, HttpStatus.NOT_FOUND);
} catch (Exception e)
{
throw new RuntimeException(e);
}
}
pom.xml:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>3.2.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.sep</groupId>
<artifactId>cardgame</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>cardgame</name>
<description>Web-based Card Game</description>
<properties>
<java.version>21</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.postgresql</groupId>
<artifactId>postgresql</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.simplejavamail</groupId>
<artifactId>simple-java-mail</artifactId>
<version>8.10.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
Exception:
Failed to run sendMail process
spring-boot |
spring-boot | org.simplejavamail.mailer.internal.MailerException: Failed to send email [Subject: 'null'], reason: Third party error
Upvotes: 0
Views: 174