我想在我关注的该网络应用程序中使用spring boot MVC
演示电子邮件应用程序 this教程我可以使用spring boot轻松发送电子邮件。
我的问题是
请为我建议任何解决方案,因为我搜索了很多,但我只能发送电子邮件example.please分享我,如果你有任何工作样本使用春季启动。提前谢谢。
答案 0 :(得分:3)
您可以考虑使用Spring integration mail support
为此目的有一个Java DSL 可以找到IMAP配置的示例here
关键方面是这样的
@Configuration
@EnableIntegration
public class IntegrationConfig {
...
@Bean
public IntegrationFlow imapIdleFlow() {
return IntegrationFlows
.from(Mail.imapIdleAdapter("imap://user:pw@localhost:" + imapIdleServer.getPort() + "/INBOX")
.autoStartup(true)
.searchTermStrategy(this::fromAndNotSeenTerm)
.userFlag("testSIUserFlag")
.javaMailProperties(p -> p.put("mail.debug", "false")
.put("mail.imap.connectionpoolsize", "5"))
.shouldReconnectAutomatically(false)
.headerMapper(mailHeaderMapper()))
.channel(MessageChannels.queue("imapIdleChannel"))
.get();
}
@Bean
public HeaderMapper<MimeMessage> mailHeaderMapper() {
return new DefaultMailHeaderMapper();
}
private SearchTerm fromAndNotSeenTerm(Flags supportedFlags, Folder folder) {
try {
FromTerm fromTerm = new FromTerm(new InternetAddress("bar@baz"));
return new AndTerm(fromTerm, new FlagTerm(new Flags(Flags.Flag.SEEN), false));
}
catch (AddressException e) {
throw new RuntimeException(e);
}
}
}
答案 1 :(得分:1)
package com.grandview.service;
import java.io.IOException;
import java.util.Properties;
import javax.annotation.PostConstruct;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Store;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;
import com.grandview.model.EmailConfig;
import com.grandview.util.MessageParser;
@Service
public class EmailService {
@Autowired
private EmailConfig emailConfig;
Folder emailFolder;
Store store;
Properties properties = new Properties();
@PostConstruct
void setup() {
String server = emailConfig.getHost();
properties.put("mail.pop3.host", server);
properties.put("mail.pop3.port", emailConfig.getPort());
//properties.put("mail.pop3.starttls.enable", "true");
Session emailSession = Session.getDefaultInstance(properties);
Store store = null;
try {
store = emailSession.getStore("pop3s");
store.connect(server, emailConfig.getUsername(), emailConfig.getPassword());
emailFolder = store.getFolder("INBOX");
} catch (MessagingException e) {
e.printStackTrace();
}
}
@Scheduled(fixedRate = 5000)
synchronized void read() throws MessagingException, IOException {
emailFolder.open(Folder.READ_ONLY);
Message[] messages = emailFolder.getMessages();
for (int i = 0; i < messages.length; i++) {
Message message = messages[i];
System.out.println(MessageParser.getMessageBody(message));
}
emailFolder.close();
}
}
答案 2 :(得分:0)
如果您要询问如何接收基于Spring Boot和Java邮件接收库(例如Subetha SMTP)的电子邮件,可以检查Receive emails with SubEtha SMTP and Spring Boot