Menu

#17 Calling javax.mail.Transport.isConnected() creates extra msg

1.6
open
nobody
None
5
2014-08-18
2006-12-13
No

import java.util.Properties;

import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

import junit.framework.TestCase;

import com.dumbster.smtp.SimpleSmtpServer;

/**
* Demonstrate possible bug in Dumbster when Transport.isConnected() is called.
*/
public class DumbsterTest extends TestCase {
private static final String HOST = "localhost";
private static final int PORT = 1000;

/**
* Succeeds: one email recorded.
*/
public void testSendWithoutConnectedCheck() throws Exception {
SimpleSmtpServer server = SimpleSmtpServer.start(PORT);
sendEmail(false);
server.stop();
assertEquals("# emails received", 1, server.getReceivedEmailSize());
}

/**
* Fails: two emails recorded. The second is completely blank.
* The javadocs for Transport.isConnected() say it actually pings the
* server:
* {@link http://java.sun.com/products/javamail/javadocs/com/sun/mail/smtp/SMTPTransport.html#isConnected\()}
*/
public void testSendWithConnectedCheck() throws Exception {
SimpleSmtpServer server = SimpleSmtpServer.start(PORT);
sendEmail(true);
server.stop();
assertEquals("# emails received", 1, server.getReceivedEmailSize());
}

private void sendEmail(boolean checkState) throws Exception {
Properties props = new Properties();
props.put("mail.smtp.host", HOST);
props.put("mail.smtp.port", String.valueOf(PORT));
Session session = Session.getDefaultInstance(props, null);
Transport transport = session.getTransport("smtp");
transport.connect(HOST, PORT, null, null);
MimeMessage message = new MimeMessage(session);
message.setContent("test body", "text/plain");
message.setSubject("test subject");
message.setFrom(new InternetAddress("from@example.com"));
message.addRecipient(Message.RecipientType.TO, new InternetAddress("to@example.com"));
transport.sendMessage(message, message.getAllRecipients());
if (checkState) transport.isConnected();
transport.close();
}
}

Discussion

  • joawl

    joawl - 2007-03-15

    Logged In: YES
    user_id=1744395
    Originator: NO

    I have same problem and found a possible way to fix it.

    An empty message is being created when Dumbster receives a NOOP command after the "Dot". Eg in that sequence of commands two messages are created:

    EHLO usertwo
    MAIL FROM:<tst@tst.net>
    RCPT TO:<any@tst.net>
    DATA
    ( some data skipped )
    .
    NOOP
    QUIT

    First message after the dot, and second - empty message - after the NOOP.
    That's because you have that code in SimpleSmtpServer.java:

    if (smtpState == SmtpState.QUIT) {
    msgList.add(msg);
    msg = new SmtpMessage();
    }

    It's not checked was any data added to msg or wasn't the state changed from QUIT to QUIT with NOOP command.

     
  • joawl

    joawl - 2007-03-15

    Logged In: YES
    user_id=1744395
    Originator: NO

    I have fixed bug in that way:

    added a method to SmtpRequest.java to get an action type:

    public SmtpActionType getAction() {
    return action;
    }

    and then added condition to the SimpleSmtpServer.java after the response in being sent to client, like this:

    // Send reponse to client
    sendResponse(out, response);

    // http://www.faqs.org/rfcs/rfc2821.html
    // This command does not affect any parameters or previously entered commands.
    // It specifies no action other than that the receiver send an OK reply.
    if (request.getAction() != SmtpActionType.NOOP) {
    // Store input in message
    String params = request.getParams();
    msg.store(response, params);

    // If message reception is complete save it
    if (smtpState == SmtpState.QUIT ) {
    msgList.add(msg);
    msg = new SmtpMessage();
    }
    }

     
  • joawl

    joawl - 2007-03-15

    Logged In: YES
    user_id=1744395
    Originator: NO

    Btw NOOP is being sent on javax.mail.Transport.isConnected() call, that's why the problem was found.

     

Log in to post a comment.