Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

229
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda