Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

222
Views
How to send email of webpage as an attachment using Spring Boot, JavaScript, Ajax?

I have implemented a feature to generate invoice in PDF format. So basically there are two buttons one for print and another one for Email. So when user clicks on email button then the invoice should be sent to specific email.

The thing is I am bit confused about the implementation. So If any one have any suggestion, would be appreciable.

Screenshot to display email invoice

Screenshot to display generated PDF

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

@Controller
@RequestMapping("/mail")
public class MailController {

    @Autowired
    public EmailService emailService;


    @RequestMapping(value = {"/send"}, method = RequestMethod.POST)
    public String sendEmailWithAttachment(Model model,
                             HttpServletRequest request) {

             //Logic to create PDF file

            emailService.sendMessageWithAttachment(to,subject,body, pdfFile);

    }

public interface EmailService{

      sendMessageWithAttachment(
          String to, String subject, String text, String pathToAttachment, FileSystemResource pdfFileToSend); 

}

@Component
public class EmailServiceImpl implements EmailService {

  @Autowired
  private JavaMailSender emailSender;

  @Override
  public void sendMessageWithAttachment(
  String to, String subject, String text, String pathToAttachment, FileSystemResource pdfFileToSend) {
    // ...
    
    MimeMessage message = emailSender.createMimeMessage();
     
    MimeMessageHelper helper = new MimeMessageHelper(message, true);
    
    helper.setFrom("noreply@baeldung.com");
    helper.setTo(to);
    helper.setSubject(subject);
    helper.setText(text);
    
    helper.addAttachment("PDFFile", pdfFileToSend);

    emailSender.send(message);
    // ...
}

Your Javascript then calls this /mail/send endpoint, which should generate the PDF for this user, create an Email, and send Email using spring-boot-mail-starter

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-mail</artifactId>
</dependency>

References:

https://howtodoinjava.com/spring-boot2/send-email-with-attachment/#5

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!