364 Email Document

Email Document

Generate a document and attach it to an e-mail in one step. The message is a real MIME e-mail (HTML + text + PDF attachment).

Outbox mode - no SMTP server is configured, so e-mails are saved as .eml files in C:\GumroadArtifactsSoft\spring-boot-print-document-lab\data\outbox and nothing leaves this PC. Download one and open it with Outlook or Thunderbird to see exactly what the customer would get. To send for real, set spring.mail.host (see the box below).

New e-mail

Attachment

Sent / Outbox

table email_log
WhenTo / subjectAttachmentStatus
No e-mails yet.
How it works
MimeMessage msg = mailSender.createMimeMessage();           // or new MimeMessage(Session) in Outbox mode
MimeMessageHelper h = new MimeMessageHelper(msg, true, "UTF-8"); // true = multipart (needed for attachments)
h.setFrom("PrintDoc Lab <documents@printdoc.local>");
h.setTo("accounts@siamfresh.example");
h.setSubject("Invoice INV-202610-001");
h.setText(plainText, html);                                     // text + HTML alternative
h.addAttachment("invoice-INV-202610-001.pdf", new ByteArrayResource(pdfBytes), "application/pdf");
msg.writeTo(new FileOutputStream("data/outbox/....eml"));       // Outbox
mailSender.send(msg);                                           // SMTP

Send real mail: add to config/application-local.properties

spring.mail.host=smtp.gmail.com
spring.mail.port=587
spring.mail.username=you@gmail.com
spring.mail.password=your-16-letter-app-password
spring.mail.properties.mail.smtp.auth=true
spring.mail.properties.mail.smtp.starttls.enable=true

Spring Boot only creates the JavaMailSender bean when spring.mail.host is set; the service asks for it with ObjectProvider<JavaMailSender> and falls back to the Outbox. Files: service/EmailService.java.