You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JHipster(Angular+Spring Boot)邮件发送参数返回null问题求助

Hey there! Let's figure out why your email's subject, name, and body are showing up as null even though the mail sends successfully. This is almost always a data flow issue between your Angular frontend and Spring Boot backend, or how you're accessing that data in your SmtpMailSender component. Let's break down the possible fixes step by step:

1. Double-Check Your Angular Frontend Data Submission

First, make sure your form is binding data correctly and sending it properly to the backend:

  • Verify your form template uses matching name attributes and ngModel (or reactive form controls) for each field. For example:
    <form #contactForm="ngForm" (ngSubmit)="submitContactForm(contactForm.value)">
      <input type="text" name="fullName" ngModel placeholder="Your Name" required>
      <input type="text" name="subject" ngModel placeholder="Email Subject" required>
      <textarea name="message" ngModel placeholder="Your Message" required></textarea>
      <button type="submit">Send Message</button>
    </form>
    
  • Ensure your Angular service sends the form data in the request body (not as URL params) using HttpClient.post:
    sendContactMessage(formData: any): Observable<any> {
      return this.http.post('/api/contact', formData);
    }
    
  • Add a console log in your submit method to confirm the data is correct before sending:
    submitContactForm(formData: any) {
      console.log('Sending form data:', formData); // Check if all fields have values here
      this.contactService.sendContactMessage(formData).subscribe(
        () => alert('Email sent!'),
        err => console.error('Send failed:', err)
      );
    }
    

2. Fix Backend Data Reception with a DTO

Spring Boot needs a clear way to map the incoming JSON data to Java objects. Use a DTO (Data Transfer Object) with @RequestBody to ensure data is parsed correctly:

  • Create a simple ContactDTO class with matching fields and proper getters/setters:
    public class ContactDTO {
        private String fullName;
        private String subject;
        private String message;
    
        // Getters and Setters (critical for Spring to map the data!)
        public String getFullName() { return fullName; }
        public void setFullName(String fullName) { this.fullName = fullName; }
        public String getSubject() { return subject; }
        public void setSubject(String subject) { this.subject = subject; }
        public String getMessage() { return message; }
        public void setMessage(String message) { this.message = message; }
    }
    
  • Update your controller to use @RequestBody to receive the DTO:
    @PostMapping("/api/contact")
    public ResponseEntity<Void> handleContactRequest(@RequestBody ContactDTO contactDTO) {
        // Add logs here to confirm data is received
        System.out.println("Received Name: " + contactDTO.getFullName());
        System.out.println("Received Subject: " + contactDTO.getSubject());
        smtpMailSender.sendContactEmail(contactDTO);
        return ResponseEntity.ok().build();
    }
    
    Without @RequestBody, Spring won't parse the JSON body, leaving all fields null.

3. Ensure SmtpMailSender Uses the DTO Data Correctly

Double-check your SmtpMailSender component is actually pulling values from the DTO:

@Component
public class SmtpMailSender {
    @Autowired
    private JavaMailSender mailSender;

    public void sendContactEmail(ContactDTO contactDTO) {
        SimpleMailMessage mailMessage = new SimpleMailMessage();
        mailMessage.setTo("your-recipient@example.com");
        mailMessage.setSubject(contactDTO.getSubject()); // Use the DTO's subject getter
        mailMessage.setText("From: " + contactDTO.getFullName() + "\n\n" + contactDTO.getMessage());
        mailSender.send(mailMessage);
    }
}

Make sure you're not accidentally using hardcoded null values or uninitialized variables instead of the DTO's getter methods.

Quick Debugging Tip

If you're still stuck, use the logs we added in the controller and Angular submit method. If the frontend logs show valid data but the backend logs show nulls, the issue is with how Spring is parsing the request. If the frontend logs already show nulls, fix the form binding first.

内容的提问来源于stack exchange,提问作者henna994

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 06:41:22