gpt4 book ai didi

java - 使用 Spring Mail 将电子邮件保存到已发送文件夹中

转载 作者:行者123 更新时间:2023-12-02 08:55:29 26 4
gpt4 key购买 nike

我有一个功能可以向客户发送电子邮件以确认客户已订购的订单。

代码:

@Bean
public JavaMailSender orderMailSender() {
JavaMailSenderImpl mailSender = new JavaMailSenderImpl();
mailSender.setHost("mail.myserver.vn");
mailSender.setPort(25);

mailSender.setUsername(SystemParams.ORDER_EMAIL_ADDRESS);
mailSender.setPassword(SystemParams.ORDER_EMAIL_PASSWORD);

Properties props = mailSender.getJavaMailProperties();
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.store.protocol", "imaps");
props.put("mail.smtp.starttls.enable", "false");
props.put("mail.debug", "true");

return mailSender;
}



private void sendEmailConfirm(HttpSession session) {
try {
MimeMessage message = emailSender.createMimeMessage();
boolean multipart = true;
MimeMessageHelper helper = new MimeMessageHelper(message, multipart);
Object object = session.getAttribute(Constants.CART_CONFIRM_ATTRIBUTE_NAME);
String htmlMsg = "<h4>Đơn hàng #" + object + " đã được tạo thành công.<h4>";
message.setContent(htmlMsg, "text/html; charset=utf-8");
message.setSubject("Xác nhận đơn hàng #" + object, StandardCharsets.UTF_8.displayName());
message.setFrom(SystemParams.ORDER_EMAIL_ADDRESS);
helper.setTo("customeremail@gmail.com");
// helper.setSubject();
this.emailSender.send(message);
} catch (Exception e) {
e.printStackTrace();
}
}

我当前存储在 session 中的所有数据。使用此代码,电子邮件已成功发送,但未存储在已发送文件夹中,如何将已发送电子邮件保存到电子邮件服务器中的已发送文件夹中?

最佳答案

您可能需要明确地执行此操作

Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imap");
store.connect(host, "user", "userpwd");

Folder folder = (Folder) store.getFolder("Sent");
if (!folder.exists()) {
folder.create(Folder.HOLDS_MESSAGES);
}
folder.open(Folder.READ_WRITE);
System.out.println("appending...");
try {
folder.appendMessages(new Message[]{message});
// Message[] msgs = folder.getMessages();
message.setFlag(FLAGS.Flag.RECENT, true);
} catch (Exception ignore) {
System.out.println("error processing message " + ignore.getMessage());
} finally {
store.close();
folder.close(false);
}

关于java - 使用 Spring Mail 将电子邮件保存到已发送文件夹中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60518697/

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