gpt4 book ai didi

java - 如何创建pdf文件并在spring boot应用程序的邮件中发送

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:34:50 25 4
gpt4 key购买 nike

我正在尝试发送带有创建的 pdf 文档作为附件的电子邮件,我正在工作的环境是基于 REST 的 java spring boot 应用程序,

实际上我知道如何使用 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());
}
}

实际上我需要创建一种 2-3 页的报告作为 pdf 文件,并通过邮件发送。

我还需要发送多个pdf报告,在邮件中,我该怎么办,各位 friend 能帮我解决一下吗,我发现了一个叫jasper的东西,是不是和我的环境有关,

最佳答案

这是发送邮件的方式:

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

String sender = "sender@yourhost.com"; //replace this with a valid sender email address
String recipient = "recipient@anotherhost.com"; //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) { }
}
}
}

您可以看到我们创建了一个 MimeBodyPart,其中包含一个从 bytes 创建的 DataSource,它是由一个名为 writePdf() 的方法产生的:

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();
}

由于我们使用 ByteOutputStream 而不是 FileOutputStream,所以没有文件写入磁盘。

关于java - 如何创建pdf文件并在spring boot应用程序的邮件中发送,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45083327/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com