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

Issue with SMTP via javaxmail api #89

Closed
philiplourandos opened this issue Aug 3, 2015 · 6 comments
Closed

Issue with SMTP via javaxmail api #89

philiplourandos opened this issue Aug 3, 2015 · 6 comments
Assignees
Labels
Milestone

Comments

@philiplourandos
Copy link

Hi there,

I have an application using spring-integration to poll a mail server and store the messages in the designated inbox.

I am able to start the green mail server and send a mail. However the javaxmail api is where thing go wrong. In com.sun.mail.imap.protocol.IMAPProtocol#processGreeting a check is made to see if the IMAP response key is equal to 'PREAUTH' which it is not, the message just contains: '220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025'.

I am perhaps not using javaxmail properties that would forgo this preauth step? Any help would be appreciated.

@marcelmay
Copy link
Member

Can you provide a simple test case or at least the relevant Spring configuration you're using?

@philiplourandos
Copy link
Author

Version info:
java 8 update 51
spring 4.1.7
spring integration 4.1.6
greenmail 1.4.1
javax.mail 1.5.4

package server.service.email;

import entities.EmailInteraction;
import repo.impl.EmailInteractionRepository;
import java.io.IOException;
import javax.mail.Message;
import javax.mail.MessagingException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author plourand
 */
@Service
public class EmailService {

    @Autowired
    private EmailInteractionRepository emailRepo;

    @Transactional
    public void saveEmail(Message mail) throws MessagingException, IOException {
        EmailInteraction interaction = new EmailInteraction();
        interaction.setFromAddress(mail.getFrom()[0].toString());
        interaction.setToAddress(mail.getAllRecipients()[0].toString());
        interaction.setSubject(mail.getSubject());

        emailRepo.save(interaction);
    }
}
package server.service.email;

import com.icegreen.greenmail.spring.GreenMailBean;
import entities.EmailInteraction;
import repo.impl.EmailInteractionRepository;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

import static org.mockito.Mockito.any;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;

/**
 *
 * @author plourand
 */
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = {"classpath*:spring/service-email.xml", "classpath*:spring/test-email.xml"})
public class EmailServiceTest {

    @Autowired
    private GreenMailBean mailBean;

    @Autowired
    private EmailInteractionRepository emailRepo;

    @Test
    public void successfulSave() throws InterruptedException {
        mailBean.sendEmail("philip@theblacklodge", "admin@theblacklodge", "subject", "content");

        Thread.sleep(2000);

        verify(emailRepo, times(1)).save(any(EmailInteraction.class));
    }
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:int="http://www.springframework.org/schema/integration"
       xmlns:int-mail="http://www.springframework.org/schema/integration/mail"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util.xsd
                           http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.springframework.org/schema/integration/mail http://www.springframework.org/schema/integration/mail/spring-integration-mail.xsd
                           http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
                           http://www.springframework.org/schema/integration http://www.springframework.org/schema/integration/spring-integration.xsd">

    <context:property-placeholder location="classpath:config/mail.properties"/>

    <int-mail:inbound-channel-adapter id="emailAdapter"
                                      channel="incoming.mail.channel"
                                      auto-startup="true"
                                      should-delete-messages="true"
                                      should-mark-messages-as-read="true"
                                      store-uri="imap://admin:password@theblacklodge:3025/INBOX" >
        <int:poller max-messages-per-poll="40" fixed-rate="100" />
    </int-mail:inbound-channel-adapter>

    <int:channel id="incoming.mail.channel"/>

    <int:chain input-channel="incoming.mail.channel">
        <int:service-activator ref="emailService" method="saveEmail"/>
    </int:chain>

    <bean id="emailService" class="service.email.EmailService" />
</beans>
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:mockito="http://www.mockito.org/spring/mockito"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
                           http://www.mockito.org/spring/mockito http://www.mockito.org/spring/mockito.xsd">

    <bean id="emailServer" class="com.icegreen.greenmail.spring.GreenMailBean">
        <property name="autostart" value="true"/>
        <property name="smtpProtocol" value="true" />
        <property name="pop3Protocol" value="true" />
        <property name="imapProtocol" value="true" />
        <property name="portOffset" value="3000" />
        <property name="hostname" value="theblacklodge" />
        <property name="users">
            <list>
                <value>philip:password@theblacklodge</value>
                <value>admin:password@theblacklodge</value>
            </list>
        </property>
    </bean>

    <mockito:mock id="emailRepo" class="repo.impl.EmailInteractionRepository" />
</beans>
14:44:49.910 [task-scheduler-6] DEBUG o.s.i.c.PublishSubscribeChannel - postSend (sent=true) on channel 'errorChannel', message: ErrorMessage [payload=org.springframework.messaging.MessagingException: failure occurred while polling for mail; nested exception is javax.mail.MessagingException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025;
  nested exception is:
    com.sun.mail.iap.ConnectionException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025, headers={id=568feca9-010e-f563-c0bd-7d2981eb2c10, timestamp=1438692289909}]
14:44:50.007 [task-scheduler-6] DEBUG o.s.i.mail.ImapMailReceiver - connecting to store [imap://admin:*****@theblacklodge.za.mhgad.com:3025/INBOX]
14:44:50.009 [task-scheduler-6] DEBUG o.s.i.c.PublishSubscribeChannel - preSend on channel 'errorChannel', message: ErrorMessage [payload=org.springframework.messaging.MessagingException: failure occurred while polling for mail; nested exception is javax.mail.MessagingException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025;
  nested exception is:
    com.sun.mail.iap.ConnectionException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025, headers={id=5a8654ca-7831-fa5f-66c7-979c44f191aa, timestamp=1438692290009}]
14:44:50.010 [task-scheduler-6] DEBUG o.s.i.handler.LoggingHandler - (inner bean)#14a50707 received message: ErrorMessage [payload=org.springframework.messaging.MessagingException: failure occurred while polling for mail; nested exception is javax.mail.MessagingException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025;
  nested exception is:
    com.sun.mail.iap.ConnectionException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025, headers={id=5a8654ca-7831-fa5f-66c7-979c44f191aa, timestamp=1438692290009}]
14:44:50.010 [task-scheduler-6] ERROR o.s.i.handler.LoggingHandler - org.springframework.messaging.MessagingException: failure occurred while polling for mail; nested exception is javax.mail.MessagingException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025;
  nested exception is:
    com.sun.mail.iap.ConnectionException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025
    at org.springframework.integration.mail.MailReceivingMessageSource.receive(MailReceivingMessageSource.java:117)
    at org.springframework.integration.endpoint.SourcePollingChannelAdapter.receiveMessage(SourcePollingChannelAdapter.java:144)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.doPoll(AbstractPollingEndpoint.java:192)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint.access$000(AbstractPollingEndpoint.java:55)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:149)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$1.call(AbstractPollingEndpoint.java:146)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller$1.run(AbstractPollingEndpoint.java:298)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor$1.run(ErrorHandlingTaskExecutor.java:52)
    at org.springframework.core.task.SyncTaskExecutor.execute(SyncTaskExecutor.java:50)
    at org.springframework.integration.util.ErrorHandlingTaskExecutor.execute(ErrorHandlingTaskExecutor.java:49)
    at org.springframework.integration.endpoint.AbstractPollingEndpoint$Poller.run(AbstractPollingEndpoint.java:292)
    at org.springframework.scheduling.support.DelegatingErrorHandlingRunnable.run(DelegatingErrorHandlingRunnable.java:54)
    at org.springframework.scheduling.concurrent.ReschedulingRunnable.run(ReschedulingRunnable.java:81)
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
    at java.util.concurrent.FutureTask.run(FutureTask.java:266)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.access$201(ScheduledThreadPoolExecutor.java:180)
    at java.util.concurrent.ScheduledThreadPoolExecutor$ScheduledFutureTask.run(ScheduledThreadPoolExecutor.java:293)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)
Caused by: javax.mail.MessagingException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025;
  nested exception is:
    com.sun.mail.iap.ConnectionException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:722)
    at javax.mail.Service.connect(Service.java:364)
    at javax.mail.Service.connect(Service.java:245)
    at javax.mail.Service.connect(Service.java:194)
    at org.springframework.integration.mail.AbstractMailReceiver.connectStoreIfNecessary(AbstractMailReceiver.java:227)
    at org.springframework.integration.mail.AbstractMailReceiver.openFolder(AbstractMailReceiver.java:234)
    at org.springframework.integration.mail.AbstractMailReceiver.receive(AbstractMailReceiver.java:260)
    at org.springframework.integration.mail.MailReceivingMessageSource.receive(MailReceivingMessageSource.java:103)
    ... 19 more
Caused by: com.sun.mail.iap.ConnectionException: 220 /127.0.0.1 GreenMail SMTP Service Ready at port 3025
    at com.sun.mail.imap.protocol.IMAPProtocol.processGreeting(IMAPProtocol.java:289)
    at com.sun.mail.iap.Protocol.<init>(Protocol.java:124)
    at com.sun.mail.imap.protocol.IMAPProtocol.<init>(IMAPProtocol.java:121)
    at com.sun.mail.imap.IMAPStore.newIMAPProtocol(IMAPStore.java:746)
    at com.sun.mail.imap.IMAPStore.protocolConnect(IMAPStore.java:689)
    ... 26 more

@marcelmay
Copy link
Member

Hi Philip,

thanks for the input. EmailInteractionRepository is missing but I can work around that.
Here are some comments:

  1. You speak IMAP using the SMTP port 3025, which should be 3143 instead when configuring int-mail:inbound-channel-adapter:

    "store-uri="imap://admin:password@theblacklodge:3025/INBOX"

  2. Make sure "theblacklodge" is DNS resolvable (required by the way org.springframework.integration.mail.AbstractMailReceiver creates the JavaMail Store).
    Test this by using:

    ping theblacklodge

  3. There is a GreenMail issue with parentheses around IMAP Search Flags, for which I will open another issue once I got a simple test for reproducing the issue running.

@marcelmay marcelmay self-assigned this Aug 4, 2015
@philiplourandos
Copy link
Author

You speak IMAP using the SMTP port 3025, which should be 3143

facepalm yup that's the main culprit right there.

Make sure "theblacklodge" is DNS resolvable (required by the way

Yup did have entry in /etc/hosts

There is a GreenMail issue with parentheses around IMAP Search Flags

Think I am seeing the same thing now

07:57:35.987 [task-scheduler-4] DEBUG o.s.i.mail.ImapMailReceiver - This email server does not support RECENT or USER flags. System flag 'Flag.FLAGGED' will be used to prevent duplicates during email fetch.
07:57:35.987 [Thread-5] DEBUG c.i.g.imap.ImapRequestHandler - C: tag=A800, command=SEARCH
07:57:35.987 [Thread-5] DEBUG c.i.g.imap.commands.SearchCommand - Search request is 'NOT'
07:57:35.987 [Thread-5] DEBUG c.i.g.imap.commands.SearchCommand - Search request is '(ANSWERED)'
07:57:35.987 [Thread-5] WARN  c.i.g.imap.commands.SearchCommand - Ignoring not yet implemented search command '(ANSWERED)'
java.lang.IllegalArgumentException: No enum constant com.icegreen.greenmail.imap.commands.SearchKey.(ANSWERED)
    at java.lang.Enum.valueOf(Enum.java:238) ~[na:1.8.0_51]
    at com.icegreen.greenmail.imap.commands.SearchKey.valueOf(SearchKey.java:92) ~[greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.imap.commands.SearchCommand$SearchCommandParser.searchTerm(SearchCommand.java:121) ~[greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.imap.commands.SearchCommand.doProcess(SearchCommand.java:51) [greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.imap.commands.SearchCommand.doProcess(SearchCommand.java:42) [greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.imap.commands.CommandTemplate.process(CommandTemplate.java:48) [greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.imap.ImapRequestHandler.doProcessRequest(ImapRequestHandler.java:98) [greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.imap.ImapRequestHandler.handleRequest(ImapRequestHandler.java:51) [greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.imap.ImapHandler.run(ImapHandler.java:94) [greenmail-1.4.1.jar:na]
    at com.icegreen.greenmail.server.AbstractServer$1.run(AbstractServer.java:103) [greenmail-1.4.1.jar:na]
    at java.lang.Thread.run(Thread.java:745) [na:1.8.0_51]

marcelmay added a commit that referenced this issue Aug 5, 2015
Note: Search support is still limited to simple terms - see issue #20
marcelmay added a commit to marcelmay/greenmail that referenced this issue Aug 21, 2015
Note: Search support is still limited to simple terms - see issue greenmail-mail-test#20
@buildscientist
Copy link
Member

@marcelmay Can I close this issue? Looks like it's been resolved with your commit in 1acb351

@marcelmay
Copy link
Member

@philiplourandos :
Just updated snapshots deployment to https://oss.sonatype.org/content/repositories/snapshots with version 1.4.2-SNAPSHOT. Please reopen if problem persists.

@marcelmay marcelmay added the bug label Oct 5, 2015
@marcelmay marcelmay added this to the 1.5 milestone Oct 5, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants