gpt4 book ai didi

java - Spring MVC java邮件IMAP,如何在thymeleaf中显示通过表单收到的电子邮件列表?

转载 作者:行者123 更新时间:2023-12-01 16:17:21 25 4
gpt4 key购买 nike

我正在制作一个电子邮件客户端应用程序,为此我使用 Spring MVC 和“javax.mail”邮件 API,以及用于 View 的 thymeleaf。我已经将服务配置为通过 IMAP 读取消息,到目前为止一切顺利,我遇到的问题是,当我尝试通过 View 中的表单显示电子邮件列表时,我只能看到列表中的一项,尽管在debug 可以看到指定文件夹(收件箱)中的所有邮件都列出来了。那么如何通过thymeleaf中的表单显示“收件箱”文件夹的电子邮件列表呢?

public class ReadDto {

private String from;
private String subject;
private Date sentDate;

public ReadDto() {
}

public ReadDto(String from, String subject, Date sentDate) {
this.from = from;
this.subject = subject;
this.sentDate = sentDate;
}

public String getFrom() {
return from;
}

public void setFrom(String from) {
this.from = from;
}

public String getSubject() {
return subject;
}

public void setSubject(String subject) {
this.subject = subject;
}

public Date getSentDate() {
return sentDate;
}

public void setSentDate(Date sentDate) {
this.sentDate = sentDate;
}
}

我的服务:

public interface EmailReadService {
ReadDto downloadEmails();
}

实现:

    @Override
public ReadDto downloadEmails(){

ReadDto readDto = new ReadDto();

if (getLoginSession()==null){
logger.error("Session error!");
}

try {
store = getLoginSession().getStore(emailAccountComponent.getProtocol());
store.connect(connectService.javaMailSenderImpl.getHost(),
connectService.javaMailSenderImpl.getUsername(),
connectService.javaMailSenderImpl.getPassword());

folder = store.getFolder(emailAccountComponent.getFolder());
folder.open(Folder.READ_ONLY);

Message[] messages = folder.search(new FlagTerm(new Flags(Flags.Flag.SEEN), false));
for (Message message: messages){
readDto.setFrom(Arrays.toString(message.getFrom()));
readDto.setSubject(message.getSubject());
readDto.setSentDate(message.getSentDate());
}
folder.close(false);
store.close();
} catch (NoSuchProviderException ex) {
System.out.println("No provider.");
ex.printStackTrace();
} catch (MessagingException ex) {
System.out.println("Could not connect to the message store");
ex.printStackTrace();
}

return readDto;
}

我的 Controller

    @GetMapping("/read")
public String getEmails(Model model){
ReadDto readDto = emailReadService.downloadEmails();
model.addAttribute("emails", readDto);
return READ_VIEW;
}

在 View 中:

<div class="container">
<div class="card-body">
<table class="table table-striped table-dark">
<thead>
<tr>
<th>From</th>
<th>Subject</th>
<th>Sent date</th>
<th></th>
</tr>
</thead>
<tbody>
<tr th:each="readDto: ${emails}">
<td><span th:text="${emails.from}"> From </span></td>
<td><span th:text="${emails.subject}"> Subject </span></td>
<td><span th:text="${emails.sentDate}"> Sent date </span></td>

</tr>
</tbody>
</table>
</div>
</div>

最佳答案

而不是这个:

<tr th:each="readDto: ${emails}">
<td><span th:text="${emails.from}"> From </span></td>
<td><span th:text="${emails.subject}"> Subject </span></td>
<td><span th:text="${emails.sentDate}"> Sent date </span></td>
</tr>

您需要使用这个:

<tr th:each="readDto : ${emails}">
<td><span th:text="${readDto.from}"> From </span></td>
<td><span th:text="${readDto.subject}"> Subject </span></td>
<td><span th:text="${readDto.sentDate}"> Sent date </span></td>
</tr>

readDto 变量代表 ${emails} 集合中的每个对象。

然后,在此之后,您需要确保从此处填充 readDTO 对象...

ReadDto readDto = emailReadService.downloadEmails();

...实际上是一个集合而不是单个对象。它需要是这样的:

public interface EmailReadService {
List<ReadDto> downloadEmails();
}

现在,您的代码通过上述接口(interface)返回一封电子邮件:

for (Message message: messages){
readDto.setFrom(Arrays.toString(message.getFrom()));
readDto.setSubject(message.getSubject());
readDto.setSentDate(message.getSentDate());
}

因此,您需要在创建这些 readDto 对象时将它们存储在列表中。

关于java - Spring MVC java邮件IMAP,如何在thymeleaf中显示通过表单收到的电子邮件列表?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62367062/

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