2017-07-13 4 views
0

私は添付ファイルとして作成されたPDF文書に電子メールを送信しようとしています、私が働いている環境は、RESTベースのJava春ブートアプリケーションで、どのようにメールにPDFファイルを作成して送信するスプリングブートアプリケーションの

実際に私はthymeleafテンプレートエンジンを使って電子メールを送信する方法を知っていますが、メモリにpdfドキュメントを作成して添付ファイルとして送信するにはどうすればいいですか?これは電子メールの送信に使用しているコードです。

Context cxt = new Context(); 
cxt.setVariable("doctorFullName", doctorFullName); 
String message = templateEngine.process(mailTemplate, cxt); 
emailService.sendMail(MAIL_FROM, TO_EMAIL,"Subject", "message"); 

と、この本はsendmail()関数

@Override 
    public void sendPdfMail(String fromEmail, String recipientMailId, String subject, String body) { 
     logger.info("--in the function of sendMail"); 
     final MimeMessage mimeMessage = this.mailSender.createMimeMessage(); 
     try { 
      final MimeMessageHelper message = new MimeMessageHelper(mimeMessage, true, "UTF-8"); 
      message.setSubject(subject); 
      message.setFrom(fromEmail); 
      message.setTo(recipientMailId); 
      message.setText(body, true); 

      FileSystemResource file = new FileSystemResource("C:\\xampp\\htdocs\\project-name\\logs\\health360-logging.log"); 
      message.addAttachment(file.getFilename(), file); 


      this.mailSender.send(mimeMessage); 
      logger.info("--Mail Sent Successfully"); 
     } catch (MessagingException e) { 
      logger.info("--Mail Sent failed ---> " + e.getMessage()); 
      throw new RuntimeException(e.getMessage()); 
     } 
    } 

実際に私は、PDFファイルとして2-3ページでレポートの種類を作成し、メールで送信する必要があります。

また、私はこれをどのように行うことができ、メールで、複数のPDFレポートを送信する必要があるが、あなたの友人がこの上で私を助けることができる、私は碧玉と呼ばれるものを見つけ、それが私の環境に関連したものです、

+1

これは広すぎます。質問を小さなもののセットで分割してください。 PDF生成、添付ファイルなどを使用した – StanislavL

答えて

0

このあなたは私たちがwritePdf()命名法に起因bytesから作成DataSourceMimeBodyPartを作成することがわかります

public void email() { 
    String smtpHost = "yourhost.com"; //replace this with a valid host 
    int smtpPort = 587; //replace this with a valid port 

    String sender = "[email protected]"; //replace this with a valid sender email address 
    String recipient = "[email protected]"; //replace this with a valid recipient email address 
    String content = "dummy content"; //this will be the text of the email 
    String subject = "dummy subject"; //this will be the subject of the email 

    Properties properties = new Properties(); 
    properties.put("mail.smtp.host", smtpHost); 
    properties.put("mail.smtp.port", smtpPort);  
    Session session = Session.getDefaultInstance(properties, null); 

    ByteArrayOutputStream outputStream = null; 

    try {   
     //construct the text body part 
     MimeBodyPart textBodyPart = new MimeBodyPart(); 
     textBodyPart.setText(content); 

     //now write the PDF content to the output stream 
     outputStream = new ByteArrayOutputStream(); 
     writePdf(outputStream); 
     byte[] bytes = outputStream.toByteArray(); 

     //construct the pdf body part 
     DataSource dataSource = new ByteArrayDataSource(bytes, "application/pdf"); 
     MimeBodyPart pdfBodyPart = new MimeBodyPart(); 
     pdfBodyPart.setDataHandler(new DataHandler(dataSource)); 
     pdfBodyPart.setFileName("test.pdf"); 

     //construct the mime multi part 
     MimeMultipart mimeMultipart = new MimeMultipart(); 
     mimeMultipart.addBodyPart(textBodyPart); 
     mimeMultipart.addBodyPart(pdfBodyPart); 

     //create the sender/recipient addresses 
     InternetAddress iaSender = new InternetAddress(sender); 
     InternetAddress iaRecipient = new InternetAddress(recipient); 

     //construct the mime message 
     MimeMessage mimeMessage = new MimeMessage(session); 
     mimeMessage.setSender(iaSender); 
     mimeMessage.setSubject(subject); 
     mimeMessage.setRecipient(Message.RecipientType.TO, iaRecipient); 
     mimeMessage.setContent(mimeMultipart); 

     //send off the email 
     Transport.send(mimeMessage); 

     System.out.println("sent from " + sender + 
       ", to " + recipient + 
       "; server = " + smtpHost + ", port = " + smtpPort);   
    } catch(Exception ex) { 
     ex.printStackTrace(); 
    } finally { 
     //clean off 
     if(null != outputStream) { 
      try { outputStream.close(); outputStream = null; } 
      catch(Exception ex) { } 
     } 
    } 
} 

:あなたがメールを送信することができる方法である

public void writePdf(OutputStream outputStream) throws Exception { 
    Document document = new Document(); 
    PdfWriter.getInstance(document, outputStream); 
    document.open(); 
    Paragraph paragraph = new Paragraph(); 
    paragraph.add(new Chunk("hello!")); 
    document.add(paragraph); 
    document.close(); 
} 

FileOutputStreamの代わりにByteOutputStreamを使用しているため、ファイルはディスクに書き込まれません。

+0

ここでDocumentクラスとは何か、javax.swing.textまたはorg.dom4jまたはorg.thymeleaf.domまたはorg.w3c.dom –

+0

これは 'com.itextpdf.text.Document'オブジェクトです。 iText 5.最新版が必要な場合は、iText 7のドキュメントを参照してください:http://developers.itextpdf.com/content/itext-7-jump-start-tutorial –

関連する問題