Upgrade Jakarta Mail (and use the API)
This commit is contained in:
parent
76fd96b0ca
commit
820365bf14
11
core/pom.xml
11
core/pom.xml
@ -111,9 +111,18 @@
|
||||
</dependency>
|
||||
|
||||
<!--Additional stuff-->
|
||||
<dependency>
|
||||
<groupId>jakarta.mail</groupId>
|
||||
<artifactId>jakarta.mail-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.activation</groupId>
|
||||
<artifactId>jakarta.activation-api</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<artifactId>jakarta.mail</artifactId>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.fasterxml.jackson.core</groupId>
|
||||
|
@ -5,9 +5,9 @@ import org.slf4j.LoggerFactory;
|
||||
import se.su.dsv.scipro.forum.dataobjects.ForumPost;
|
||||
import se.su.dsv.scipro.forum.dataobjects.ForumThread;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.Part;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.Multipart;
|
||||
import jakarta.mail.Part;
|
||||
import java.io.IOException;
|
||||
import java.util.ListIterator;
|
||||
|
||||
|
@ -1,17 +1,21 @@
|
||||
package se.su.dsv.scipro.forummail;
|
||||
|
||||
import jakarta.mail.Flags;
|
||||
import jakarta.mail.Folder;
|
||||
import jakarta.mail.Message;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.Multipart;
|
||||
import jakarta.mail.Part;
|
||||
import jakarta.mail.Session;
|
||||
import jakarta.mail.Store;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import jakarta.mail.search.FlagTerm;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.MimeMessage;
|
||||
import javax.mail.search.FlagTerm;
|
||||
import java.io.IOException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.List;
|
||||
import java.util.Properties;
|
||||
import java.util.*;
|
||||
|
||||
public class IMAPReader implements ForumMailReader {
|
||||
|
||||
@ -30,22 +34,11 @@ public class IMAPReader implements ForumMailReader {
|
||||
@Override
|
||||
public void accept(MailHandler mailHandler) {
|
||||
try {
|
||||
Folder folder = null;
|
||||
Store store = null;
|
||||
try {
|
||||
store = connectToIMAP();
|
||||
folder = openFolder(store);
|
||||
try (Store store = connectToIMAP(); Folder folder = openFolder(store)) {
|
||||
List<ForumMailMessage> forumMailMessages = messagesAsList(folder.search(unreadOnly()));
|
||||
for (ForumMailMessage forumMailMessage : forumMailMessages) {
|
||||
mailHandler.accept(forumMailMessage);
|
||||
}
|
||||
} finally {
|
||||
if (folder != null) {
|
||||
folder.close(true);
|
||||
}
|
||||
if (store != null) {
|
||||
store.close();
|
||||
}
|
||||
}
|
||||
} catch (MessagingException | IOException e) {
|
||||
LOGGER.info("Something went wrong while reading e-mail: ", e);
|
||||
|
@ -1,7 +1,7 @@
|
||||
package se.su.dsv.scipro.forummail;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Part;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.Part;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.UncheckedIOException;
|
||||
|
@ -1,6 +1,6 @@
|
||||
package se.su.dsv.scipro.forummail;
|
||||
|
||||
import javax.mail.Part;
|
||||
import jakarta.mail.Part;
|
||||
|
||||
public interface MailContentParser {
|
||||
|
||||
|
@ -1,15 +1,15 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import com.sun.mail.smtp.SMTPMessage;
|
||||
import jakarta.mail.SendFailedException;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import javax.mail.SendFailedException;
|
||||
import java.util.Arrays;
|
||||
import java.util.*;
|
||||
|
||||
class Failure extends SMTPMailResult {
|
||||
private final SendFailedException exception;
|
||||
|
||||
public Failure(final SMTPMessage message, final SendFailedException exception) {
|
||||
public Failure(final MimeMessage message, final SendFailedException exception) {
|
||||
super(message);
|
||||
this.exception = exception;
|
||||
}
|
||||
|
@ -1,11 +1,11 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import jakarta.mail.Session;
|
||||
import jakarta.mail.internet.AddressException;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import se.su.dsv.scipro.file.FileService;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
|
||||
public class Mail extends SMTPMailer {
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import jakarta.mail.MessagingException;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import se.su.dsv.scipro.file.FileDescription;
|
||||
@ -9,9 +10,7 @@ import se.su.dsv.scipro.system.User;
|
||||
import se.su.dsv.scipro.workerthreads.AbstractWorker;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.MessagingException;
|
||||
import java.util.Objects;
|
||||
import java.util.Set;
|
||||
import java.util.*;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
public class MailEventWorker extends AbstractWorker {
|
||||
|
@ -7,7 +7,7 @@ import se.su.dsv.scipro.file.FileService;
|
||||
import se.su.dsv.scipro.generalsystemsettings.GeneralSystemSettingsService;
|
||||
import se.su.dsv.scipro.profiles.CurrentProfile;
|
||||
|
||||
import javax.mail.Session;
|
||||
import jakarta.mail.Session;
|
||||
import java.util.Properties;
|
||||
|
||||
public class MailModule extends PrivateModule {
|
||||
@ -25,16 +25,11 @@ public class MailModule extends PrivateModule {
|
||||
@Provides
|
||||
@Exposed
|
||||
public Mailer mailer(CurrentProfile currentProfile, Session session, FileService fileDescriptionService) {
|
||||
switch (currentProfile.getCurrentProfile()) {
|
||||
case DEV:
|
||||
return new PrintingMailer();
|
||||
case PROD:
|
||||
return new Mail(session, fileDescriptionService);
|
||||
case TEST:
|
||||
return new RedirectingMailer(session, TEST_MAIL_SINK, fileDescriptionService);
|
||||
default:
|
||||
throw new Error("Yay for exhaustive matches");
|
||||
}
|
||||
return switch (currentProfile.getCurrentProfile()) {
|
||||
case DEV -> new PrintingMailer();
|
||||
case PROD -> new Mail(session, fileDescriptionService);
|
||||
case TEST -> new RedirectingMailer(session, TEST_MAIL_SINK, fileDescriptionService);
|
||||
};
|
||||
}
|
||||
|
||||
@Provides
|
||||
@ -42,6 +37,7 @@ public class MailModule extends PrivateModule {
|
||||
String smtpHost = generalSystemSettings.getGeneralSystemSettingsInstance().getSmtpServer();
|
||||
Properties properties = new Properties();
|
||||
properties.setProperty("mail.smtp.host", smtpHost);
|
||||
properties.setProperty("mail.smtp.sendpartial", Boolean.toString(true));
|
||||
return Session.getDefaultInstance(properties);
|
||||
}
|
||||
}
|
||||
|
@ -2,7 +2,7 @@ package se.su.dsv.scipro.mail;
|
||||
|
||||
import se.su.dsv.scipro.file.FileDescription;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import jakarta.mail.MessagingException;
|
||||
|
||||
public interface Mailer {
|
||||
MailResult mail(String fromName, String fromEmail, String[] recipients, String subject, String message, FileDescription attachment) throws MessagingException;
|
||||
|
@ -1,11 +1,11 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import jakarta.mail.Session;
|
||||
import jakarta.mail.internet.AddressException;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import se.su.dsv.scipro.file.FileService;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.Session;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
|
||||
public class RedirectingMailer extends SMTPMailer {
|
||||
private final String redirectTo;
|
||||
|
@ -1,14 +1,13 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import com.sun.mail.smtp.SMTPMessage;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.MessagingException;
|
||||
import jakarta.mail.Address;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
|
||||
abstract class SMTPMailResult implements MailResult {
|
||||
private final SMTPMessage message;
|
||||
private final MimeMessage message;
|
||||
|
||||
SMTPMailResult(final SMTPMessage message) {
|
||||
SMTPMailResult(final MimeMessage message) {
|
||||
this.message = message;
|
||||
}
|
||||
|
||||
|
@ -1,16 +1,16 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import com.sun.mail.smtp.SMTPMessage;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import se.su.dsv.scipro.file.FileDescription;
|
||||
import se.su.dsv.scipro.file.FileService;
|
||||
|
||||
import javax.activation.DataHandler;
|
||||
import javax.activation.DataSource;
|
||||
import javax.mail.*;
|
||||
import javax.mail.internet.AddressException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import jakarta.activation.DataHandler;
|
||||
import jakarta.activation.DataSource;
|
||||
import jakarta.mail.*;
|
||||
import jakarta.mail.internet.AddressException;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeBodyPart;
|
||||
import jakarta.mail.internet.MimeMultipart;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.io.OutputStream;
|
||||
@ -30,9 +30,8 @@ abstract class SMTPMailer implements Mailer {
|
||||
@Override
|
||||
public MailResult mail(String fromName, String fromEmail, String[] recipients, String subject, String message, final FileDescription attachment) throws MessagingException {
|
||||
|
||||
SMTPMessage msg = new SMTPMessage(session);
|
||||
MimeMessage msg = new MimeMessage(session);
|
||||
|
||||
msg.setSendPartial(true);
|
||||
msg.setSentDate(new Date());
|
||||
try {
|
||||
InternetAddress addressFrom = new InternetAddress(fromEmail, fromName, "UTF-8");
|
||||
|
@ -1,10 +1,10 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import com.sun.mail.smtp.SMTPMessage;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
class Success extends SMTPMailResult {
|
||||
public Success(final SMTPMessage message) {
|
||||
public Success(final MimeMessage message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
|
@ -8,11 +8,11 @@ import se.su.dsv.scipro.forum.dataobjects.ForumPost;
|
||||
import se.su.dsv.scipro.forum.dataobjects.ForumThread;
|
||||
import se.su.dsv.scipro.system.User;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import javax.mail.Multipart;
|
||||
import javax.mail.Part;
|
||||
import javax.mail.internet.MimeBodyPart;
|
||||
import javax.mail.internet.MimeMultipart;
|
||||
import jakarta.mail.MessagingException;
|
||||
import jakarta.mail.Multipart;
|
||||
import jakarta.mail.Part;
|
||||
import jakarta.mail.internet.MimeBodyPart;
|
||||
import jakarta.mail.internet.MimeMultipart;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
|
||||
|
@ -1,5 +1,6 @@
|
||||
package se.su.dsv.scipro.forummail;
|
||||
|
||||
import jakarta.mail.Part;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@ -16,9 +17,8 @@ import se.su.dsv.scipro.system.User;
|
||||
import se.su.dsv.scipro.test.ObjectMother;
|
||||
|
||||
import javax.inject.Inject;
|
||||
import javax.mail.Part;
|
||||
import java.time.LocalDate;
|
||||
import java.util.Collections;
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
|
@ -1,6 +1,9 @@
|
||||
package se.su.dsv.scipro.mail;
|
||||
|
||||
import com.sun.mail.smtp.SMTPMessage;
|
||||
import jakarta.mail.Address;
|
||||
import jakarta.mail.SendFailedException;
|
||||
import jakarta.mail.internet.InternetAddress;
|
||||
import jakarta.mail.internet.MimeMessage;
|
||||
import org.junit.jupiter.api.BeforeEach;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.junit.jupiter.api.extension.ExtendWith;
|
||||
@ -9,21 +12,20 @@ import org.mockito.Mock;
|
||||
import org.mockito.junit.jupiter.MockitoExtension;
|
||||
import org.slf4j.Logger;
|
||||
|
||||
import javax.mail.Address;
|
||||
import javax.mail.SendFailedException;
|
||||
import javax.mail.internet.InternetAddress;
|
||||
import java.util.Arrays;
|
||||
import java.util.*;
|
||||
|
||||
import static org.mockito.ArgumentMatchers.anyString;
|
||||
import static org.mockito.ArgumentMatchers.contains;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@ExtendWith(MockitoExtension.class)
|
||||
public class FailureTest {
|
||||
@Mock
|
||||
private Logger logger;
|
||||
@Mock
|
||||
private SMTPMessage message;
|
||||
private MimeMessage message;
|
||||
@Mock
|
||||
private SendFailedException exception;
|
||||
@InjectMocks
|
||||
|
@ -17,7 +17,7 @@ import se.su.dsv.scipro.generalsystemsettings.GeneralSystemSettingsService;
|
||||
import se.su.dsv.scipro.system.User;
|
||||
import se.su.dsv.scipro.test.DomainObjects;
|
||||
|
||||
import javax.mail.MessagingException;
|
||||
import jakarta.mail.MessagingException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Set;
|
||||
import java.util.TreeSet;
|
||||
|
15
pom.xml
15
pom.xml
@ -175,10 +175,21 @@
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>jakarta.mail</groupId>
|
||||
<artifactId>jakarta.mail-api</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>jakarta.activation</groupId>
|
||||
<artifactId>jakarta.activation-api</artifactId>
|
||||
<version>2.0.1</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.sun.mail</groupId>
|
||||
<artifactId>javax.mail</artifactId>
|
||||
<version>1.5.5</version>
|
||||
<artifactId>jakarta.mail</artifactId>
|
||||
<version>2.0.1</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
|
||||
<!-- Test stuff -->
|
||||
|
Loading…
x
Reference in New Issue
Block a user