Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update mail connector dependencies #291

Merged
merged 3 commits into from
Nov 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 29 additions & 6 deletions docs/src/docs/modules/mail-connector.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@

The module mail-connector allows you to send and receive/read emails as well as solve mail surrounding tasks like encoding or encrypting.

== Project setup
The Mail connector uses https://github.com/jakartaee/mail-api[Jakarta Mail 2].

Latest release image:https://img.shields.io/maven-central/v/io.testerra/mail-connector?label=Testerra%20Mail%20Connector[Maven Central]
== Project setup

.Gradle
[source,gradle,role="primary",subs="attributes+"]
Expand All @@ -25,6 +25,7 @@ compile 'io.testerra:mail-connector:{revnumber}'
</dependency>
</dependencies>
----

=== Configuration Parameters
All MailConnector classes need parameters to connect to the corresponding server. These are set within the `mailconnection.properties` file or by system properties. The following parameters must be set.

Expand Down Expand Up @@ -60,8 +61,30 @@ MAX_READ_TRIES=20
If you want to use an SSL-encrypted connection, you need to set `SMTP_SSL_ENABLED` / `POP3_SSL_ENABLED` to true. The ports must then be adjusted according to the server.
The actual connection to the mail server is implicitly opened within each `MailConnector` class.

== Common notes

Please note that Jakarta Mail 2 changed some packages. If you update to the latest Mail connector you have to change your imports:


[source, java]
----
// Jakarta Mail 1.6.x
import javax.mail.Message;
import javax.mail.internet.MimeBodyPart;
import javax.mail.search.AndTerm;
import javax.mail.search.SearchTerm;
...

// Jakarta Mail 2.x
import jakarta.mail.Message;
import jakarta.mail.internet.MimeBodyPart;
import jakarta.mail.search.AndTerm;
import jakarta.mail.search.SearchTerm;
...
----

== POP3MailConnector
The `POP3MailConnector` provides access to a POP3 server. Emails are read and processed by using methods of the superclass `AbstractInboxConnector`. Specific mails can be extracted with serverside filtering by matching given criteria expressed with SearchTerm (see https://javaee.github.io/javaee-spec/javadocs/javax/mail/search/package-summary.html[JavaMail API])
The `POP3MailConnector` provides access to a POP3 server. Emails are read and processed by using methods of the superclass `AbstractInboxConnector`. Specific mails can be extracted with serverside filtering by matching given criteria expressed with SearchTerm (see https://javadoc.io/doc/com.sun.mail/jakarta.mail/latest/jakarta.mail/module-summary.html[Jakarta Mail API])

.Reading and Deleting Mails
[source,java]
Expand Down Expand Up @@ -194,9 +217,9 @@ connector.configureSessionProperties(properties -> {

See the original documentation for more information:

* IMAP: https://eclipse-ee4j.github.io/mail/docs/api/com/sun/mail/imap/package-summary.html
* SMTP: https://eclipse-ee4j.github.io/mail/docs/api/com/sun/mail/smtp/package-summary.html
* POP3: https://eclipse-ee4j.github.io/mail/docs/api/com/sun/mail/pop3/package-summary.html
* IMAP: https://javadoc.io/static/com.sun.mail/jakarta.mail/2.0.1/jakarta.mail/com/sun/mail/imap/package-summary.html
* SMTP: https://javadoc.io/static/com.sun.mail/jakarta.mail/2.0.1/jakarta.mail/com/sun/mail/smtp/package-summary.html
* POP3: https://javadoc.io/static/com.sun.mail/jakarta.mail/2.0.1/jakarta.mail/com/sun/mail/pop3/package-summary.html

== Debugging the MailConnector

Expand Down
8 changes: 4 additions & 4 deletions mail-connector/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
def mailVersion = '1.6.5'
def mailVersion = '2.0.1'

dependencies {
api "com.sun.mail:jakarta.mail:${mailVersion}"
api "jakarta.mail:jakarta.mail-api:${mailVersion}"
api 'org.bouncycastle:bcmail-jdk15on:1.52'
api 'net.iharder:base64:2.3.8'
api 'org.bouncycastle:bcjmail-jdk18on:1.72'
compileOnly core

testImplementation core
testImplementation 'com.icegreen:greenmail:1.5.9'
testImplementation report
testImplementation 'com.icegreen:greenmail:2.0.0-alpha-2'
}

test {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,15 @@
import eu.tsystems.mms.tic.testframework.exceptions.SystemException;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.mailconnector.util.AbstractInboxConnector;
import jakarta.mail.Flags;
import jakarta.mail.Folder;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.NoSuchProviderException;
import jakarta.mail.Store;

import java.util.Properties;
import javax.mail.Flags.Flag;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Store;


/**
* MailConnector using the IMAP Protocol. Creates a session with values from mailconnection.properties.
Expand Down Expand Up @@ -103,7 +105,7 @@ private void pMarkAllMailsAsSeen() throws SystemException {
folder.open(Folder.READ_WRITE);
final Message[] messages = folder.getMessages();
for (Message message : messages) {
message.setFlag(Flag.SEEN, true);
message.setFlag(Flags.Flag.SEEN, true);
}
store.close();
} catch (final NoSuchProviderException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,20 @@
import eu.tsystems.mms.tic.testframework.exceptions.SystemException;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.mailconnector.util.AbstractMailConnector;
import jakarta.activation.DataHandler;
import jakarta.activation.FileDataSource;
import jakarta.mail.Message;
import jakarta.mail.MessagingException;
import jakarta.mail.Transport;
import jakarta.mail.internet.AddressException;
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.Base64;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Message.RecipientType;
import javax.mail.MessagingException;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import net.iharder.Base64;

/**
* MailConnector using the SMTP Protocol. Creates a session with values from mailconnection.properties.
Expand Down Expand Up @@ -78,6 +78,7 @@ private void init() {

/**
* Open a new SMTP Session and save in session object.
*
* @see {https://eclipse-ee4j.github.io/mail/docs/api/com/sun/mail/smtp/package-summary.html}
*/
@Override
Expand All @@ -91,7 +92,7 @@ protected void openSession() {
}

mailprops.setProperty("mail.transport.protocol", protocol);
mailprops.put("mail."+protocol+".auth", "true");
mailprops.put("mail." + protocol + ".auth", "true");

setSession(createDefaultSession(mailprops, protocol));
}
Expand Down Expand Up @@ -167,7 +168,7 @@ private MimeBodyPart pCreateAttachment(final File file) {
* Add MimeBodyParts to a message. Can only called once, otherwise message text can not saved.
*
* @param attachments An array containing the MimeBodyParts.
* @param message The message to add the attachments.
* @param message The message to add the attachments.
* @return The message with the attached MimeBodyParts.
*/
public MimeMessage addAttachmentsToMessage(final MimeBodyPart[] attachments, final Message message) {
Expand All @@ -178,7 +179,7 @@ public MimeMessage addAttachmentsToMessage(final MimeBodyPart[] attachments, fin
* Add MimeBodyParts to a message. Can only called once, otherwise message text can not saved.
*
* @param attachments An array containing the MimeBodyParts.
* @param message The message to add the attachments.
* @param message The message to add the attachments.
* @return The message with the attached MimeBodyParts.
*/
private MimeMessage pAddAttachmentsToMessage(final MimeBodyPart[] attachments, final Message message) {
Expand All @@ -205,12 +206,12 @@ private MimeMessage pAddAttachmentsToMessage(final MimeBodyPart[] attachments, f
/**
* Send a virus mail.
*
* @param from The from address.
* @param receiver The to address.
* @param from The from address.
* @param receiver The to address.
* @param ccReceiver The cc address. Can be null.
* @param bcc The bcc address. Can be null.
* @param bcc The bcc address. Can be null.
* @return A MimeMessage containing a virus signature.
* @throws SystemException thrown if virus Mail can't generated.
* @throws SystemException thrown if virus Mail can't generated.
* @throws RuntimeException thrown if address parameters were wrong.
*/
public MimeMessage generateVirusMail(final String from, final String receiver,
Expand All @@ -221,12 +222,12 @@ public MimeMessage generateVirusMail(final String from, final String receiver,
/**
* Send a virus mail.
*
* @param from The from address.
* @param receiver The to address.
* @param from The from address.
* @param receiver The to address.
* @param ccReceiver The cc address. Can be null.
* @param bcc The bcc address. Can be null.
* @param bcc The bcc address. Can be null.
* @return A MimeMessage containing a virus signature.
* @throws SystemException thrown if virus Mail can't generated.
* @throws SystemException thrown if virus Mail can't generated.
* @throws RuntimeException thrown if address parameters were wrong.
*/
private MimeMessage pGenerateVirusMail(final String from, final String receiver,
Expand All @@ -235,14 +236,14 @@ private MimeMessage pGenerateVirusMail(final String from, final String receiver,
try {

message.setFrom(new InternetAddress(from));
message.setRecipient(RecipientType.TO, new InternetAddress(receiver));
message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(receiver));

if (ccReceiver != null) {
message.setRecipient(RecipientType.CC, new InternetAddress(ccReceiver));
message.setRecipient(MimeMessage.RecipientType.CC, new InternetAddress(ccReceiver));
}

if (bcc != null) {
message.setRecipient(RecipientType.BCC, new InternetAddress(bcc));
message.setRecipient(MimeMessage.RecipientType.BCC, new InternetAddress(bcc));
}

final String virusPattern = "X5O!P%@AP[4\\PZX54" + "(P^)7CC)7}$EICAR-STANDARD-ANTIVIRUS-TEST-FILE!$H+H*";
Expand All @@ -256,7 +257,7 @@ private MimeMessage pGenerateVirusMail(final String from, final String receiver,

multiPart.addBodyPart(bp1);

final byte[] encoded = Base64.encodeBytes(virusPattern.getBytes()).getBytes();
final byte[] encoded = Base64.getEncoder().encode(virusPattern.getBytes());
final MimeBodyPart bp2 = new MimeBodyPart();
bp2.setFileName("virus.exe");
bp2.setContent(encoded, "application/octet-stream");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,25 +32,26 @@
import java.util.Optional;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import javax.mail.Flags;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessageRemovedException;
import javax.mail.MessagingException;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Store;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.search.AndTerm;
import javax.mail.search.ComparisonTerm;
import javax.mail.search.FromTerm;
import javax.mail.search.MessageIDTerm;
import javax.mail.search.RecipientTerm;
import javax.mail.search.SearchTerm;
import javax.mail.search.SentDateTerm;
import javax.mail.search.SubjectTerm;
import jakarta.mail.Flags;
import jakarta.mail.Folder;
import jakarta.mail.Message;
import jakarta.mail.MessageRemovedException;
import jakarta.mail.MessagingException;
import jakarta.mail.NoSuchProviderException;
import jakarta.mail.Session;
import jakarta.mail.Store;
import jakarta.mail.internet.AddressException;
import jakarta.mail.internet.InternetAddress;
import jakarta.mail.internet.MimeMessage;

import jakarta.mail.search.AndTerm;
import jakarta.mail.search.ComparisonTerm;
import jakarta.mail.search.FromTerm;
import jakarta.mail.search.MessageIDTerm;
import jakarta.mail.search.RecipientTerm;
import jakarta.mail.search.SearchTerm;
import jakarta.mail.search.SentDateTerm;
import jakarta.mail.search.SubjectTerm;
import org.apache.commons.lang3.ArrayUtils;

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,13 @@
import eu.tsystems.mms.tic.testframework.common.PropertyManager;
import eu.tsystems.mms.tic.testframework.logging.Loggable;
import eu.tsystems.mms.tic.testframework.utils.CertUtils;
import jakarta.mail.Authenticator;
import jakarta.mail.PasswordAuthentication;
import jakarta.mail.Session;

import java.security.GeneralSecurityException;
import java.util.Properties;
import java.util.function.Consumer;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;

/**
* abstract class to handle mail connector
Expand Down Expand Up @@ -90,16 +91,16 @@ public AbstractMailConnector configureSessionProperties(Consumer<Properties> con
}

protected Session createDefaultSession(Properties mailProperties, String protocol) {
mailProperties.put("mail."+protocol+".host", getServer());
mailProperties.put("mail."+protocol+".port", getPort());
mailProperties.put("mail." + protocol + ".host", getServer());
mailProperties.put("mail." + protocol + ".port", getPort());

try {
MailSSLSocketFactory sf = new MailSSLSocketFactory();
CertUtils certUtils = CertUtils.getInstance();
if (certUtils.isTrustAllHosts()) {
log().warn("Trusting all hosts");
sf.setTrustAllHosts(true);
mailProperties.put("mail."+protocol+".ssl.trust", "*");
mailProperties.put("mail." + protocol + ".ssl.trust", "*");
} else {
String[] hostsToTrust = certUtils.getTrustedHosts();
if (hostsToTrust != null) {
Expand All @@ -109,7 +110,7 @@ protected Session createDefaultSession(Properties mailProperties, String protoco
sf.setTrustedHosts(hostsToTrust);
}
}
mailProperties.put("mail."+protocol+".ssl.socketFactory", sf);
mailProperties.put("mail." + protocol + ".ssl.socketFactory", sf);
} catch (GeneralSecurityException e) {
throw new RuntimeException("Error opening session", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,12 @@
package eu.tsystems.mms.tic.testframework.mailconnector.util;

import eu.tsystems.mms.tic.testframework.logging.Loggable;
import jakarta.mail.Address;
import jakarta.mail.MessagingException;
import jakarta.mail.Multipart;
import jakarta.mail.internet.MimeMessage;
import org.apache.commons.io.IOUtils;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.IllegalCharsetNameException;
Expand All @@ -31,11 +37,6 @@
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.internet.MimeMessage;
import org.apache.commons.io.IOUtils;

/**
* E-Mail Objekt, das alle Inhalte eines javax.mail.Message-Objekts ausliest.
Expand Down Expand Up @@ -237,7 +238,6 @@ private void readMessageContents() {
* get charset for encoding from Mail, use UTF-8 as default
*
* @param encoding
*
* @return
*/
private String getCharSetForEncoding(String encoding) {
Expand Down Expand Up @@ -276,7 +276,6 @@ public Map<String, String> getAttachments() {
* Gets the content of the given attachment
*
* @param fileName Name of attachment
*
* @return content of attachment
*/
public String getAttachmentsContent(String fileName) {
Expand Down
Loading