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
nameattributes andngModel(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
ContactDTOclass 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
@RequestBodyto receive the DTO:
Without@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(); }@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

