Once you have a working audit and data export system, the next logical step is
automating reports and
sending them via email to admins or stakeholders.
This helps ensure that regular updates happen without manual intervention.
1. Add Mail Configuration
In
application.properties, add your SMTP settings:
spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=your-email@gmail.com
spring.mail.password=your-app-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true
Make sure you generate an
App Password if using Gmail for security.
2. Create Email Service
Now, create a simple email service to send messages and attachments.
@Service
public class EmailService {
@Autowired
private JavaMailSender mailSender;
public void sendEmailWithAttachment(String to, String subject, String body, File file)
throws MessagingException {
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper helper = new MimeMessageHelper(message, true);
helper.setTo(to);
helper.setSubject(subject);
helper.setText(body);
helper.addAttachment(file.getName(), file);
mailSender.send(message);
}
}
This service will handle both plain text and file attachments like CSV or Excel reports.
3. Automate Report Generation
Let’s use Spring’s built-in scheduling to automate daily report exports.
Add this to your main application class:
@EnableScheduling
@SpringBootApplication
public class FirebirdApp {
public static void main(String[] args) {
SpringApplication.run(FirebirdApp.class, args);
}
}
Then create a scheduler class:
@Component
public class ReportScheduler {
@Autowired
private EmailService emailService;
@Autowired
private UserRepository userRepo;
@Scheduled(cron = "0 0 8 * * ?") // Runs every day at 8 AM
public void sendDailyReport() throws Exception {
File report = generateCSVReport();
emailService.sendEmailWithAttachment(
"admin@company.com",
"Daily Firebird Report",
"Here is your daily system report.",
report
);
}
private File generateCSVReport() throws IOException {
File file = File.createTempFile("report_", ".csv");
PrintWriter writer = new PrintWriter(file);
writer.println("ID,Username,Role");
for (User user : userRepo.findAll()) {
writer.println(user.getId() + "," + user.getUsername() + "," + user.getRole());
}
writer.flush();
writer.close();
return file;
}
}
This script automatically exports all user data every morning and emails it to the admin./
4. Add Audit Logging for Automation
To keep everything traceable, log the automation process:
@Autowired
private AuditLogRepository logRepo;
logRepo.save(new AuditLog("Daily report sent via email", "system"));
Now every automated report will also appear in your audit log dashboard.
5. Test It Manually
Before waiting for the schedule, you can test the task manually:
@Autowired
private ReportScheduler scheduler;
@PostMapping("/send-report")
public String triggerReport() throws Exception {
scheduler.sendDailyReport();
return "Report sent manually!";
}
Access
/send-report to send an instant report and confirm everything works correctly.
image quote pre code