Gửi mail confirm bằng java. Lỗi Nullpoint

em làm phần gửi mail xác nhận thay đổi password qua mail… nhưng không biết tại sao lại bị lỗi

 public class Mailservice {
@Autowired
private JavaMailSender mailSender;

public void sendSimpleMail(String to,String content) {

    SimpleMailMessage message = new SimpleMailMessage();
    EmailConfirm emailConfirm=new EmailConfirm();
    message.setTo(to);
    System.out.println("'''''''''"+to);
    message.setSubject(emailConfirm.getTitle());
    System.out.println(".'.'.'.'.'.'"+emailConfirm.getTitle());
    message.setText(content);
    System.out.println(",,,,,"+content);
    mailSender.send(message);
}}

controller

@RequestMapping(value = "/forgotpassword", method = RequestMethod.POST)
public ResponseEntity<?> forgotpass(InputStream inputStream) {
    try {
        responseData = new ResponseData(1, "Lỗi", null);
        BaseRequest baseRequest = RequestUtils.convertToBaseRequest(inputStream);
        if (baseRequest.getWsRequest() != null) {
            ObjectMapper objectMapper = new ObjectMapper();
            JsonNode jsonNode = objectMapper.readTree(objectMapper.writeValueAsString(baseRequest.getWsRequest()));
            String email = "";
            if (jsonNode.get("email") != null ) {
                email = jsonNode.get("email").asText();
                Personal member=personalRepository.findEmail(email);
                if (member != null) {
                    int a= StringUtils.Radom(100000,999999);
                    String password="Mật khẩu mới của bạn là: "+a;
                    personalService.forgotPassword(email,password);

                } else {
                    responseData = new ResponseData(1, "Account không tồn tại", null);
                }
            }
        } else {
            responseData = new ResponseData(1, "Lỗi", null);
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    return ResponseEntity.status(HttpStatus.OK).body(responseData);
}

Imp

 @Override
@Transactional
public void forgotPassword(String mail,String password) {
    Personal personal=findEmail(mail);
    Mailservice mailService=new Mailservice();
    mailService.sendSimpleMail(mail,password);
    int id=personal.getIdPersonal();
    personalRepository.updatePassword(id,password);
}

Cái quan trọng là thông báo lỗi, ở dòng nào???

mailSender.send(message);

 mailService.sendSimpleMail(mail,password);

báo lỗi ở 2 dòng này ạ

Chưa khởi tạo cho private JavaMailSender mailSender;

khởi tạo là như thế nào ạ… em tham khảo trên mấy trang thấy chỉ khai báo thế… em sẽ khởi tạo nó như thế nào

Bạn khởi tạo trực tiếp bằng new thì mailSender trong MailService có giá trị null nha.

Mailservice mailService = new Mailservice();
mailService.sendSimpleMail(mail, password);
1 Like

JavaMailSender có lớp con (triển khai) là: JavaMailSenderImpl

Nó sẽ yêu cầu các thông tin về máy chủ lưu trữ, địa chỉ, mật khẩu,… của thư điện tử gửi.

1 Like
83% thành viên diễn đàn không hỏi bài tập, còn bạn thì sao?