Coder Social home page Coder Social logo

Comments (6)

marcelmay avatar marcelmay commented on June 12, 2024 1

@pihme , thx for the POM.

TL;DR: Use GreenMail 1.6.x / JavaMail 1.6.x for Spring 5, or upgrade to Spring 6 / baseline to Jakarta EE 9 .

See releases and roadmap for version compatibilities:

Regarding tracking Maven transitive dependencies, I'd recommend using mvn dependency:tree.
JavaMail has a confusing Maven coordinats (GAV) history.

These should work for you:

        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>jakarta.mail</artifactId>
            <version>1.6.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.icegreen</groupId>
            <artifactId>greenmail</artifactId>
            <version>1.6.14</version> <!-- NOT 2.0.0 !!! -->
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>com.icegreen</groupId>
            <artifactId>greenmail-junit4</artifactId>
            <version>1.6.7</version> <!-- NOT 2.0.0 !!! -->
            <scope>test</scope>
        </dependency>

You can check your dependencies:

mvn dependency:tree
...
[INFO] --- dependency:2.8:tree (default-cli) @ GreenMail ---
[INFO] org.example:GreenMail:jar:1.0-SNAPSHOT
[INFO] +- com.sun.mail:jakarta.mail:jar:1.6.7:test
[INFO] |  \- com.sun.activation:jakarta.activation:jar:1.2.1:test
[INFO] +- com.icegreen:greenmail:jar:1.6.14:test
[INFO] |  \- org.slf4j:slf4j-api:jar:1.7.36:test
[INFO] +- com.icegreen:greenmail-junit4:jar:1.6.7:test
[INFO] +- junit:junit:jar:4.12:test
[INFO] |  \- org.hamcrest:hamcrest-core:jar:1.3:test
[INFO] +- org.hamcrest:hamcrest-all:jar:1.3:test
[INFO] +- org.assertj:assertj-core:jar:3.24.2:test
[INFO] |  \- net.bytebuddy:byte-buddy:jar:1.12.21:test
[INFO] +- org.springframework:spring-context-support:jar:5.3.25:test
[INFO] |  +- org.springframework:spring-beans:jar:5.3.25:test
[INFO] |  +- org.springframework:spring-context:jar:5.3.25:test
[INFO] |  |  +- org.springframework:spring-aop:jar:5.3.25:test
[INFO] |  |  \- org.springframework:spring-expression:jar:5.3.25:test
[INFO] |  \- org.springframework:spring-core:jar:5.3.25:test
[INFO] |     \- org.springframework:spring-jcl:jar:5.3.25:test
[INFO] \- org.springframework:spring-web:jar:5.3.25:test
...

from greenmail.

marcelmay avatar marcelmay commented on June 12, 2024

@pihme , thx for the fast feedback on latest 2.0.

Any chance of getting the SMTP server output (using verbose, see GreenMailRule below) or a reproducing test case?

E.g. this seems to work:

    @Rule
    public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP.verbose(true));

    @Test
    public void testSmtp() throws MessagingException, IOException {
        Session smtpSession = greenMail.getSmtp().createSession();
        smtpSession.getProperties().setProperty("mail.smtp.allow8bitmime", "true");
        String msg = "Date: Tue, 7 Mar 2023 10:11:11 +0100 (CET)\n" +
                "From: Cawemo <[email protected]>\n" +
                "Reply-To: Cawemo <[email protected]>\n" +
                "To: Test User <[email protected]>\n" +
                "Message-ID: <219228606.1.1678180272020@blackbox-work>\n" +
                "Subject: subject\n" +
                "MIME-Version: 1.0\n" +
                "Content-Type: multipart/alternative; \n" +
                "\tboundary=\"----=_Part_0_593641669.1678180271951\"\n" +
                "X-MSYS-API: {\"campaign_id\":\"test-mail\"}\n" +
                "\n" +
                "------=_Part_0_593641669.1678180271951\n" +
                "Content-Type: text/plain;charset=UTF-8\n" +
                "Content-Transfer-Encoding: 7bit\n" +
                "\n" +
                "plain text body\n" +
                "------=_Part_0_593641669.1678180271951\n" +
                "Content-Type: text/html;charset=UTF-8\n" +
                "Content-Transfer-Encoding: 7bit\n" +
                "\n" +
                "<html><body>html body</body></html>\n" +
                "------=_Part_0_593641669.1678180271951--";
        final MimeMessage mimeMessage = new MimeMessage(smtpSession, new ByteArrayInputStream(msg.getBytes()));
        Transport.send(mimeMessage);
        greenMail.waitForIncomingEmail(1);
        final MimeMessage receivedMsg = greenMail.getReceivedMessages()[0];
        assertThat(receivedMsg.getSubject()).isEqualTo("subject");
        assertThat(receivedMsg.getContentType()).contains("multipart/alternative");
        MimeMultipart multipart = (MimeMultipart) receivedMsg.getContent();
        assertThat(multipart.getCount()).isEqualTo(2);

        final BodyPart bodyPart0 = multipart.getBodyPart(0);
        assertThat(bodyPart0.getContentType()).isEqualTo("text/plain;charset=UTF-8");
        assertThat(bodyPart0.getContent()).isEqualTo("plain text body");

        final BodyPart bodyPart1 = multipart.getBodyPart(1);
        assertThat(bodyPart1.getContentType()).isEqualTo("text/html;charset=UTF-8");
        assertThat(bodyPart1.getContent()).isEqualTo("<html><body>html body</body></html>");
    }

from greenmail.

pihme avatar pihme commented on June 12, 2024

Verbose output (slightly redacted:

[closed source code log line]
DEBUG: Jakarta Mail version 1.6.7
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers
DEBUG: Providers Listed By Class Name: {com.sun.mail.smtp.SMTPTransport=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], com.sun.mail.imap.IMAPSSLStore=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], com.sun.mail.pop3.POP3Store=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], com.sun.mail.smtp.SMTPSSLTransport=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], com.sun.mail.imap.IMAPStore=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], com.sun.mail.pop3.POP3SSLStore=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]}
DEBUG: Providers Listed By Protocol: {imap=javax.mail.Provider[STORE,imap,com.sun.mail.imap.IMAPStore,Oracle], smtp=javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle], pop3=javax.mail.Provider[STORE,pop3,com.sun.mail.pop3.POP3Store,Oracle], imaps=javax.mail.Provider[STORE,imaps,com.sun.mail.imap.IMAPSSLStore,Oracle], smtps=javax.mail.Provider[TRANSPORT,smtps,com.sun.mail.smtp.SMTPSSLTransport,Oracle], pop3s=javax.mail.Provider[STORE,pop3s,com.sun.mail.pop3.POP3SSLStore,Oracle]}
DEBUG: successfully loaded resource: /META-INF/javamail.default.address.map
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "localhost", port 3025, isSSL false
220 /127.0.0.1 GreenMail SMTP Service v2.0.0 ready
DEBUG SMTP: connected to host "localhost", port: 3025
EHLO blackbox-work
250-/127.0.0.1
250 AUTH PLAIN LOGIN
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
DEBUG SMTP: protocolConnect login, host=localhost, user=user, password=<non-null>
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM XOAUTH2 
DEBUG SMTP: Using mechanism LOGIN
AUTH LOGIN
334 VXNlcm5hbWU6
dXNlcg==
334 UGFzc3dvcmQ6
c2VjcmV0
235 2.7.0  Authentication Succeeded
DEBUG SMTP: use8bit true
MAIL FROM:<[email protected]>
250 OK
RCPT TO:<[email protected]>
250 OK
DEBUG SMTP: Verified Addresses
DEBUG SMTP:   Test User <[email protected]>
DATA
354 Start mail input; end with <CRLF>.<CRLF>
Date: Wed, 8 Mar 2023 17:07:56 +0100 (CET)
From: Cawemo <[email protected]>
Reply-To: Cawemo <[email protected]>
To: Test User <[email protected]>
Message-ID: <2050571201.1.1678291676564@blackbox-work>
Subject: subject
MIME-Version: 1.0
Content-Type: multipart/alternative; 
	boundary="----=_Part_0_447670321.1678291676499"
X-MSYS-API: {"campaign_id":"test-mail"}

------=_Part_0_447670321.1678291676499
Content-Type: text/plain;charset=UTF-8
Content-Transfer-Encoding: 7bit

plain text body
------=_Part_0_447670321.1678291676499
Content-Type: text/html;charset=UTF-8
Content-Transfer-Encoding: 7bit

<html><body>html body</body></html>
------=_Part_0_447670321.1678291676499--
.
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: got response code -1, with response: [EOF]
RSET
DEBUG SMTP: EOF: [EOF]
DEBUG SMTP: MessagingException while sending, THROW: 
com.sun.mail.smtp.SMTPSendFailedException: [EOF]
	at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2374)
	at com.sun.mail.smtp.SMTPTransport.finishData(SMTPTransport.java:2095)
	at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1301)
	at org.springframework.mail.javamail.JavaMailSenderImpl.doSend(JavaMailSenderImpl.java:465)
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:361)
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:378)
	at org.springframework.mail.javamail.JavaMailSenderImpl.send(JavaMailSenderImpl.java:366)
[3 layers of closed source code]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:218)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:793)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
	at org.springframework.retry.interceptor.RetryOperationsInterceptor$1.doWithRetry(RetryOperationsInterceptor.java:97)
	at org.springframework.retry.support.RetryTemplate.doExecute(RetryTemplate.java:329)
	at org.springframework.retry.support.RetryTemplate.execute(RetryTemplate.java:209)
	at org.springframework.retry.interceptor.RetryOperationsInterceptor.invoke(RetryOperationsInterceptor.java:133)
	at org.springframework.retry.annotation.AnnotationAwareRetryOperationsInterceptor.invoke(AnnotationAwareRetryOperationsInterceptor.java:159)
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:186)
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.proceed(CglibAopProxy.java:763)
	at org.springframework.aop.interceptor.AsyncExecutionInterceptor.lambda$invoke$0(AsyncExecutionInterceptor.java:115)
	at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
[1 layer of closed source code]
	at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
	at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
	at java.base/java.lang.Thread.run(Thread.java:833)
QUIT
[closed source code log line]
[repeats from here]
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "localhost", port 3025, isSSL false
220 /127.0.0.1 GreenMail SMTP Service v2.0.0 ready
DEBUG SMTP: connected to host "localhost", port: 3025
EHLO blackbox-work
250-/127.0.0.1
250 AUTH PLAIN LOGIN

[repeats from here]

What showed up as oddity is that we have both com.sun.mail:jakarta.mail:jar:1.6.7:compile and jakarta.mail:jakarta.mail-api:jar:2.0.1:test on the classpath. Even though they are technically not the same artifacts, it looks like they read out providers, so maybe there is a clash there.

from greenmail.

pihme avatar pihme commented on June 12, 2024

Regarding a test case, I was not able to reproduce it. We use Spring to compose the mail. And I simply couldn't get the dependencies right for Spring 5.3.25 and GreenMail 2.0.

I still think it is a good step in figuring this out, so here are the files I used

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.example</groupId>
    <artifactId>GreenMail</artifactId>
    <version>1.0-SNAPSHOT</version>

    <properties>
        <maven.compiler.source>19</maven.compiler.source>
        <maven.compiler.target>19</maven.compiler.target>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>
    <dependencies>
        <dependency>
            <groupId>com.sun.mail</groupId>
            <artifactId>jakarta.mail</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/javax.mail/mail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.7</version>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <version>4.12</version>
            <scope>test</scope>
        </dependency>
        <!-- Needed by junit -->
        <dependency>
            <groupId>org.hamcrest</groupId>
            <artifactId>hamcrest-all</artifactId>
            <version>1.3</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>com.icegreen</groupId>
            <artifactId>greenmail-junit4</artifactId>
            <version>2.0.0</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.assertj</groupId>
            <artifactId>assertj-core</artifactId>
            <version>3.24.2</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.3.25</version>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>5.3.25</version>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <repositories>
        <repository>
            <id>central</id>
            <name>Maven Central</name>
            <layout>default</layout>
            <url>https://repo1.maven.org/maven2</url>
            <snapshots>
                <enabled>false</enabled>
            </snapshots>
        </repository>
    </repositories>
</project>

Test case that contains most of the important moving parts on the sending side:

import com.icegreen.greenmail.junit.GreenMailRule;
import com.icegreen.greenmail.util.ServerSetupTest;
import jakarta.mail.MessagingException;
import jakarta.mail.Session;
import org.junit.Rule;
import org.junit.Test;
import org.springframework.http.MediaType;
import org.springframework.mail.javamail.JavaMailSenderImpl;

import javax.mail.Message;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;


public class AcceptanceTest {

    private static final String TEXT_PLAIN_UTF_8 = new MediaType(MediaType.TEXT_PLAIN, StandardCharsets.UTF_8).toString();
    private static final String TEXT_HTML_UTF_8 = new MediaType(MediaType.TEXT_HTML, StandardCharsets.UTF_8).toString();

    @Rule
    public final GreenMailRule greenMail = new GreenMailRule(ServerSetupTest.SMTP.verbose(true));

    @Test
    public void testSmtpComposedBySpring() throws MessagingException, IOException {
        Session smtpSession = greenMail.getSmtp().createSession();
        smtpSession.getProperties().setProperty("mail.smtp.allow8bitmime", "true");

        JavaMailSenderImpl sender = new JavaMailSenderImpl();
        sender.setPort(greenMail.getSmtp().getPort());

        sender.send(mimeMessage -> {
                    mimeMessage.setReplyTo(new InternetAddress[] { new InternetAddress("[email protected]") });
                    mimeMessage.setFrom(new InternetAddress("[email protected]"));
                    mimeMessage.setRecipient(Message.RecipientType.TO, new InternetAddress("[email protected]"));
                    mimeMessage.setSentDate(new Date());
                    mimeMessage.setSubject("whatever", "UTF-8" );
                    mimeMessage.setContent(createMultipart());
                }
        );
        
        //TODO add asserts
    }

    private javax.mail.internet.MimeMultipart createMultipart() throws javax.mail.MessagingException {
        var multipart = new javax.mail.internet.MimeMultipart("alternative");

        // text/plain
        var textBodyPart = new MimeBodyPart();
        textBodyPart.setContent("plain text body", TEXT_PLAIN_UTF_8);
        multipart.addBodyPart(textBodyPart);

        // text/html
        var htmlBodyPart = new MimeBodyPart();
        htmlBodyPart.setContent("<html><body>html body</body></html>", TEXT_HTML_UTF_8);
        multipart.addBodyPart(htmlBodyPart);

        return multipart;
    }
}

This test fails with a similar output, but probably for a different root cause than the test I mentioned above.

from greenmail.

pihme avatar pihme commented on June 12, 2024

BTW if you already know that it is incompatible with certain versions of Spring, that would be helpful to hear.

I am still figuring out how com.sun.mail:jakarta.mail:jar:1.6.7:compile enters the classpath. We don't configure the version explicitly so my assumption is that it comes as a transitive dependency of Spring.

I won't have much time to investigate this further, as it is not yet urgent for us.

from greenmail.

pihme avatar pihme commented on June 12, 2024

thanks for looking into it and the quick response ❤️

from greenmail.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.