Reputation: 133
I want to demonstrate the email application using the spring boot MVC
in that web application I follow this tutorial I can easily send the emails using spring boot.
Here my question is
please suggest me any solutions for that because I searched a lot but I can find only send email example.please share me if you have any working samples for that using spring boot. Thanks in advance.
Upvotes: 1
Views: 22469
Reputation: 184
Sharing another solution with a working example:
Read incoming emails with JavaMail API, IMAP protocol and IMAP IDLE feature.
Refer detailed implementation of this here: https://medium.com/@sushant7/how-to-monitor-incoming-emails-in-a-spring-boot-application-indefinitely-7dabbdb74b2d
Upvotes: 0
Reputation: 109
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();
}
}
Upvotes: 3
Reputation: 1144
If you're asking about how to receive emails based on Spring Boot and a Java library for mail-receiving (e.g Subetha SMTP), you can check Receive emails with SubEtha SMTP and Spring Boot
Upvotes: 1
Reputation: 1006
You could consider using Spring integration mail support
There is a Java DSL for this purposes.
An example of IMAP config could be found here
The key aspects are like this
@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);
}
}
}
Upvotes: 6