replace dep javax.mail to spring-boot-starter-mail, add actuator, fix logging
This commit is contained in:
9
pom.xml
9
pom.xml
@@ -72,15 +72,18 @@
|
||||
<version>5.4.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>1.5.5</version>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-mail</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -60,7 +60,7 @@ public class EmailService {
|
||||
return;
|
||||
}
|
||||
List<Emails> alreadyProcessed = emailsRepository.findTop10ByOrderByIdDesc();
|
||||
List<EmailData> emailData = new ReceiveEmailService("imap.gmail.com", 993, params.getFrom(), params.getToken(), params.getSpecialEmail())
|
||||
List<EmailData> emailData = new ReceiveEmailService("imap.gmail.com", 993, params.getFrom(), params.getToken(), params.getSpecialEmail(), log)
|
||||
.downloadEmails(alreadyProcessed);
|
||||
int sendLetters = 0;
|
||||
log.error("Extracted letters = {}", emailData.size());
|
||||
|
||||
@@ -1,14 +1,15 @@
|
||||
package com.pledge.torgi.service;
|
||||
|
||||
import com.pledge.torgi.model.Emails;
|
||||
import com.sun.mail.imap.IMAPMessage;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.apache.poi.ss.usermodel.Workbook;
|
||||
import org.apache.poi.util.IOUtils;
|
||||
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
||||
import jakarta.mail.*;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMultipart;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import java.io.InputStream;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
@@ -21,14 +22,16 @@ public class ReceiveEmailService {
|
||||
private String password;
|
||||
private String specialEmail;
|
||||
private String protocol = "imaps";
|
||||
private Logger logger;
|
||||
|
||||
private final Properties prop;
|
||||
public ReceiveEmailService(String host, int port, String username, String password, String specialEmail) {
|
||||
public ReceiveEmailService(String host, int port, String username, String password, String specialEmail, Logger logger) {
|
||||
prop = getServerProperties(host, port);
|
||||
|
||||
this.username = username;
|
||||
this.password = password;
|
||||
this.specialEmail = specialEmail;
|
||||
this.logger = logger;
|
||||
}
|
||||
|
||||
public List<EmailData> downloadEmails(List<Emails> alreadyProcessed) {
|
||||
@@ -60,7 +63,7 @@ public class ReceiveEmailService {
|
||||
continue;
|
||||
}
|
||||
|
||||
String messageID = ((IMAPMessage) msg).getMessageID();
|
||||
String messageID = ((MimeMessage) msg).getMessageID();
|
||||
if (alreadyProcessed.stream().anyMatch(processed -> processed.getMailId().equals(messageID))) {
|
||||
continue;
|
||||
}
|
||||
@@ -75,16 +78,15 @@ public class ReceiveEmailService {
|
||||
List<String> replyMails = new ArrayList<>(Arrays.stream(toList).map(addr -> ((InternetAddress) addr).getAddress())
|
||||
.filter(email -> !email.equals(specialEmail))
|
||||
.toList());
|
||||
replyMails.add(((InternetAddress)((IMAPMessage) msg).getSender()).getAddress());
|
||||
replyMails.add(((InternetAddress)((MimeMessage) msg).getSender()).getAddress());
|
||||
EmailData data = new EmailData();
|
||||
data.setEmailId(messageID);
|
||||
data.setFile(wb);
|
||||
data.setEmails(String.join(",", replyMails));
|
||||
System.out.println("Emails for reply: " + data.getEmails());
|
||||
logger.debug("Emails for reply: {}", data.getEmails());
|
||||
result.add(data);
|
||||
} catch (Exception ex) {
|
||||
System.out.println("No provider for protocol: " + protocol);
|
||||
ex.printStackTrace();
|
||||
logger.error("Error during send mail", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -92,11 +94,9 @@ public class ReceiveEmailService {
|
||||
folderInbox.close(false);
|
||||
store.close();
|
||||
} catch (NoSuchProviderException ex) {
|
||||
System.out.println("No provider for protocol: " + protocol);
|
||||
ex.printStackTrace();
|
||||
logger.error("No provider for protocol: {}", protocol);
|
||||
} catch (MessagingException ex) {
|
||||
System.out.println("Could not connect to the message store");
|
||||
ex.printStackTrace();
|
||||
logger.error("Could not connect to the message store", ex);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@@ -1,13 +1,14 @@
|
||||
package com.pledge.torgi.service;
|
||||
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.DataSource;
|
||||
import javax.activation.FileDataSource;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import jakarta.activation.DataHandler;
|
||||
import jakarta.activation.DataSource;
|
||||
import jakarta.activation.FileDataSource;
|
||||
import jakarta.mail.*;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeBodyPart;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import jakarta.mail.internet.MimeMultipart;
|
||||
|
||||
import java.io.File;
|
||||
import java.util.Properties;
|
||||
|
||||
|
||||
@@ -13,3 +13,5 @@ spring.servlet.multipart.max-request-size=20MB
|
||||
server.tomcat.connection-timeout=300000
|
||||
server.tomcat.keep-alive-timeout=300000
|
||||
server.connection-timeout=300000
|
||||
management.endpoints.web.exposure.include=health,metrics,prometheus
|
||||
management.endpoint.health.show-details=always
|
||||
Reference in New Issue
Block a user