I'm working on an app that needs to send an authentication token to a user whenever the registration is successful through the user's email address. I used a java mail sender for it but I'm getting this error from the stack trace. I have disabled Microsoft Firewalls from blocking anything incase that was the issue but it's still giving me the same error message.I don't really know what the issue is. Here is the error message.Thanks for your response
org.springframework.mail.MailSendException: Mail server connection failed; nested exception is com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect. Failed messages: com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect; message exception details (1) are:
Failed message 1:
com.sun.mail.util.MailConnectException: Couldn't connect to host, port: smtp.gmail.com, 587; timeout -1;
nested exception is:
java.net.ConnectException: Connection timed out: connect
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:2210)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:722)
at javax.mail.Service.connect(Service.java:342)
at org.springframework.mail.javamail.JavaMailSenderImpl.connectTransport(JavaMailSenderImpl.java:518)
at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:437)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:323)
at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:312)
at com.elijah.doctorsappointmentbookingsystem.email.EmailServiceClient.sendSimpleEmail(EmailServiceClient.java:22)
at com.elijah.doctorsappointmentbookingsystem.service.PatientService.signUpUser(PatientService.java:89)
at com.elijah.doctorsappointmentbookingsystem.service.PatientService$$FastClassBySpringCGLIB$$bca592ac.invoke(<generated>)
Here is my configuration in the application.properties files
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=ukemespring@gmail.com
spring.mail.password=nyvjazolyqrkubxg
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
spring.datasource.url=jdbc:mysql://localhost:3306/patientsdb
spring.datasource.username=root
spring.datasource.password=elijah
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.dialect.=org.hibernate.dialect.MySQL5Dialect
Here is the service class for sending the mail
@Service
public class EmailServiceClient {
@Autowired
private JavaMailSender mailSender;
public void sendSimpleEmail(String toEmail,String subject,String body) {
try {
SimpleMailMessage mailMessage = new SimpleMailMessage();
mailMessage.setFrom("ukemespring@gmail.com");
mailMessage.setTo(toEmail);
mailMessage.setSubject(subject);
mailMessage.setText(body);
mailSender.send(mailMessage);
System.out.println("Email Send.....");
}catch (Exception exception){
exception.printStackTrace();
}
}
}
Here is the class that I called the sendSimpleEmail method
@Slf4j
@Service
public class PatientService {
@Autowired
private PatientRepository patientRepository;
@Autowired
private EmailServiceClient emailServiceClient;
@Autowired
private AuthenticationTokenService authenticationTokenService;
@Transactional
public ResponseEntity<ApiResponse> signUpUser(SignUpDto signUpDto) throws DataAlreadyExistException {
//first check if the email address has already been taken by another user
//if true, returns an error message to choose another email else go ahead and signup the user
//hash the password so that it can't be seen
//create a token for each user that signUp
if (Objects.nonNull(patientRepository.findByEmail(signUpDto.getEmail()))){
throw new DataAlreadyExistException("This Email Address has already been taken by another User");
}
String encryptedPassword = signUpDto.getPassword();
try {
encryptedPassword = hashPassword(signUpDto.getPassword());
}catch (Exception e){
e.printStackTrace();
}
// signUpDto.setDateOfBirth(LocalDate.of(2000,OCTOBER,23));
// signUpDto.setAge(Period.between(signUpDto.getDateOfBirth(), LocalDate.now()).getYears());
Patient patient = new Patient();
//users.setDateOfBirth(signUpDto.getDateOfBirth());
patient.setAge(Period.between(signUpDto.getDateOfBirth(),LocalDate.now()).getYears());
if (patient.getAge()<5){
patient.setCardFee(200);
patient.setCategory("Infant");
}else if (patient.getAge()>5 && patient.getAge() <18){
patient.setCardFee(500);
patient.setCategory("Teenager");
}else if (patient.getAge() >=18){
patient.setCardFee(1000);
patient.setCategory("Adults");
}else {
patient.setCardFee(0);
patient.setCategory("Wrong Age Selection");
}
patient.setEmail(signUpDto.getEmail());
patient.setName(signUpDto.getName());
patient.setPassword(encryptedPassword);
patient.setProfileImage(signUpDto.getProfileImage());
patient.setDateOfBirth(signUpDto.getDateOfBirth());
patient.setCreatedDate(new Date());
patientRepository.save(patient);
final AuthenticationToken authenticationToken = new AuthenticationToken(patient);
authenticationTokenService.saveConfirmationToken(authenticationToken);
emailServiceClient.sendSimpleEmail(patient.getEmail(),
"Confirmation Email from the Doctors Appointment Booking System",
"Dear "+patient.getName()+"\nThis is to inform you that your registration" +
"to the Doctors Appointment Booking application was successful\nHere is your authentication token "+authenticationToken+
"\nPlease keep it private and save because you will need it in several occasion in the app especially when you want to book an appointment with a doctor\nThanks for your patronage" +
"\n\n\n\nFor More Enquiries,\nCall 08167988220\nElijah Ukeme\nThe Technical Officer");
return new ResponseEntity<>(new ApiResponse(true,"Registration Successful, you will be charged "+
patient.getCardFee()+" Naira for card fee on arrival to the hospital by the accounting department before" +
" issuing a patient's card to you. Thanks"), HttpStatus.CREATED);
}
}