Skip to content

JavaMail sends no content with SMTP, sends “QUIT” and hangs

I’m trying to use JavaMail 1.6 to send an email to a local MailHog SMTP server in Java 8. The code is:

// import javax.mail.*;
// import javax.mail.internet.*;
// import javax.mail.internet.MimeMessage.RecipientType;

String from = "";
String to = "";
String subject = "Sample subject";
String body = "Test message, please ignore";

Properties props = System.getProperties();
props.setProperty("", "localhost");
props.setProperty("mail.smtp.port", "1025");

Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);

message.setFrom(new InternetAddress(from));
message.addRecipient(RecipientType.TO, new InternetAddress(to));


When the code is ran, it will start and establish a session with the server, and step through SMTP until it’s supposed to send data. After this, it sends QUITrn and hangs, not sending any content.

MailHog logs:

2022/02/25 16:18:18 [SMTP] Starting session
2022/02/25 16:18:18 [SMTP] [PROTO: INVALID] Started session, switching to ESTABLISH state
2022/02/25 16:18:18 [SMTP] Sent 35 bytes: '220 mailhog.example ESMTP MailHogrn'
2022/02/25 16:18:18 [SMTP] Received 26 bytes: 'EHLO DEVICE.localrn'
2022/02/25 16:18:18 [SMTP] [PROTO: ESTABLISH] Processing line: EHLO DEVICE.local
2022/02/25 16:18:18 [SMTP] [PROTO: ESTABLISH] In state 1, got command 'EHLO', args 'DEVICE.local'
2022/02/25 16:18:18 [SMTP] [PROTO: ESTABLISH] In ESTABLISH state
2022/02/25 16:18:18 [SMTP] [PROTO: ESTABLISH] Got EHLO command, switching to MAIL state
2022/02/25 16:18:18 [SMTP] Sent 31 bytes: '250-Hello DEVICE.localrn'
2022/02/25 16:18:18 [SMTP] Sent 16 bytes: '250-PIPELININGrn'
2022/02/25 16:18:18 [SMTP] Sent 16 bytes: '250 AUTH PLAINrn'
2022/02/25 16:18:18 [SMTP] Received 37 bytes: 'MAIL FROM:<>rn'
2022/02/25 16:18:18 [SMTP] [PROTO: MAIL] Processing line: MAIL FROM:<>
2022/02/25 16:18:18 [SMTP] [PROTO: MAIL] In state 6, got command 'MAIL', args 'FROM:<>'
2022/02/25 16:18:18 [SMTP] [PROTO: MAIL] In MAIL state
2022/02/25 16:18:18 [SMTP] [PROTO: MAIL] Got MAIL command, switching to RCPT state
2022/02/25 16:18:18 [SMTP] Sent 39 bytes: '250 Sender okrn'
2022/02/25 16:18:18 [SMTP] Received 37 bytes: 'RCPT TO:<>rn'
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] Processing line: RCPT TO:<>
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] In state 7, got command 'RCPT', args 'TO:<>'
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] In RCPT state
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] Got RCPT command
2022/02/25 16:18:18 [SMTP] Sent 44 bytes: '250 Recipient okrn'
2022/02/25 16:18:18 [SMTP] Received 6 bytes: 'DATArn'
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] Processing line: DATA
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] In state 7, got command 'DATA', args ''
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] In RCPT state
2022/02/25 16:18:18 [SMTP] [PROTO: RCPT] Got DATA command, switching to DATA state
2022/02/25 16:18:18 [SMTP] Sent 37 bytes: '354 End data with <CR><LF>.<CR><LF>rn'
2022/02/25 16:18:18 [SMTP] Received 6 bytes: 'QUITrn'

Java output:

DEBUG: setDebug: JavaMail version 1.6.2
DEBUG: successfully loaded resource: /META-INF/javamail.default.providers
DEBUG: Tables of loaded providers from javamail.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: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: useEhlo true, useAuth false
DEBUG SMTP: trying to connect to host "localhost", port 1025, isSSL false
220 mailhog.example ESMTP MailHog
DEBUG SMTP: connected to host "localhost", port: 1025

250-Hello DEVICE.local
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "AUTH", arg "PLAIN"
DEBUG SMTP: use8bit false
250 Sender ok
250 Recipient ok
DEBUG SMTP: Verified Addresses
354 End data with <CR><LF>.<CR><LF>

Looking around, I can find a few references of JavaMail hanging while sending mail, but none of them seem to hang at this part of the session. It seems like it should be a misconfiguration related to message.setText. However, I get the same behavior when using message.setContent(body, "text/plain"), using MimeMultipart and adding a MimeBodyPart with the content, and also when using a DataHandler. Furthermore, if it is a misconfiguration of the content, I find it weird that it’s sending a QUIT immediately without first terminating the DATA. As far as I know, a normal exchange would look something like:

354 End data with <CR><LF>.<CR><LF>
Test message, please ignore


Answering my own question:

JavaMail (Maven package javax.mail/javax.mail-api) was moved to Jakarta Mail (com.sun.mail/jakarta.mail). I’m not really sure what’s wrong with the old package, but once I updated the appropriate maven dependency from




The issue resolved itself with no further code changes needed.