[SOLVED] Most common JavaMail API SMTP Errors

Published on 2019-08-09· Updated on 2021-12-15

The author voluntarily contributed this tutorial as a part of Pepipost Write to Contribute program.


You probably using JavaMail API to compose, write and read emails. The core classes of JavaMail API can be found in the javax.mail and javax.mail.activation packages. While JavaMail API provides a protocol-independent, platform-independent environment for sending and receiving emails but like any other tools, there are few scenarios where the error comes and the developers get stuck.

In this tutorial, we are going to discuss the most commonly occurring JavaMail SMTP errors and a step by step detailed guide on how to solve them.

Before listing possible errors, let's go through one of the most useful JavaMail API property you can use to debug SMTP issues. 

Properties props = new Properties();
props.put("mail.debug", "true");

Or if you are using javax.mail.Authenticator()


Note: At the end of this tutorial, you will find a working code for sending SMTP emails using java mail API and it's corresponding output in debug mode.

List Of Common JavaMail SMTP Errors

You can use the JavaMail API for triggering multiple important events. These events can be a welcome/notification email, forgot password, etc. If you are using JavaMail API for sending SMTP emails, you might end up facing the following errors on your IDE or console.

Error 1: Could not connect to SMTP host: com.sun.mail.util.MailConnectException: Couldn't connect to host🔗

Exception in thread "main" java.lang.RuntimeException: com.sun.mail.util.MailConnectException: Couldn't connect to host, port:

There can be multiple reasons for this but the most common reason for this error is the port, that you are using to send SMTP mails. Few ISPs/hosting providers block SMTP outgoing port 25. If that the case, try changing the port to 587 or 2525.


 telnet <SMTPHOST> portnumber

If this works, then this means that your port is open.

Error 2: A1 BAD Invalid SASL argument🔗

This error mainly occurs when you try to configure SSL Connection with SMTP host in wrong way.

javax.mail.MessagingException: A1 BAD Invalid SASL argument. t21mb170186760ivm;

nested exception is:

com.sun.mail.iap.BadCommandException: A1 BAD Invalid SASL argument. t21mb170186760ivm

Previously Java Mail API (up to JavaMail 1.3) didn’t have built-in support for SSL connection, so it was necessary to set socketFactory properties:

Properties props = new Properties();

From the later version of Java Mail API (greater than 1.3).

The easiest way to enable SSL support in current versions of JavaMail is to set the property "mail.smtp.ssl.enable" to "true".

props.put("mail.smtp.ssl.enable", "true");

Once this is enabled, you are ready to use SSL for authentication.

Error 3: Using Authenticator just to supply username and password 🔗

The above error may occur due to wrong imports or handling of javax.mail.authenticator.

There is nothing wrong in using authenticator, but its unnecessarily complex and hard to debug. Instead to avoid error and for saving time use connect method that takes username password when connecting to a Store. And use the static Transport.send method that takes a username and password while sending a message.

Transport.send(msg, user, password);


Error 4: You may get Empty multipart Error While Sending Attachment 🔗

Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingException: IOException while sending message;
      			nested exception is:
			java.io.IOException: javax.mail.MessagingException: Empty multipart: multipart/mixed; 

This error may occur when you are not properly embedding attachment in a standard defined way.

A simple solution to this is to make sure to add MIMEBODYPART class object into the Multipart Class object using addBodyPart(multipart) method

Here is a code snippet:

Multipart multipart = new MimeMultipart();
MimeBodyPart attachmentPart = new MimeBodyPart();
 try {
 } catch (IOException e) {

If you send an attachment as above it may happen that your mail is not delivered, due to security constraints maintained at public ESP’s Gmail or Yahoo.

Better First Create File object and then pass to MimeBodyPart.

Find below code snippet:

Multipart multipart = new MimeMultipart();
 MimeBodyPart attachmentPart = new MimeBodyPart();
 try {
 File f =new File("H:\7-5-2017statement.pdf");
 } catch (IOException e) {

Error 5: It may happen that you're not using the configuration you think you're using. 🔗

You must create a Session object using Session.getInstance instead of Session.getDefaultInstance. Because Session.getDefaultInstance creates a new session with properties that are passed to it for the first time and subsequent calls return the original session that is created the first time and ignores the properties that are passed in subsequent calls.

If there is another code in JVM or app server that creates a session object, you may end up using that object and your properties will be ignored.

To verify whether you are using the correct properties, verify the output properties on the console using;


To avoid this problem better use;


Here is code snippet for sending SMTP mails using java mail API and corresponding output in debug mode.

Maven Dependencies:

  • Add this in pom.xml
 import java.util.Properties;
 import javax.mail.Message;
 import javax.mail.MessagingException;
 import javax.mail.PasswordAuthentication;
 import javax.mail.Session;
 import javax.mail.Transport;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeMessage;
 public class App
 public static void main( String[] args )
 // Put recipient’s address
 String to = "[email protected]";
 // Put sender’s address
 String from = "[email protected]";
 final String username = "username";//username generated by Pepipost
 final String password = "password";//password generated by Pepipost
 // Paste host address from the SMTP relay tab in Integrations from your Pepipost App
 String host = "smtp.pepipost.com";
 Properties props = new Properties();
 props.put("mail.smtp.auth", "true");
 //props.put("mail.smtp.starttls.enable", "true");
 props.put("mail.smtp.host", host);
 props.put("mail.smtp.port", "587");
 // Get the Session object.
 Session session = Session.getInstance(props,
 new javax.mail.Authenticator() {
 protected PasswordAuthentication getPasswordAuthentication() {
 return new PasswordAuthentication(username, password);
 try {
 // Create a default MimeMessage object.
 Message message = new MimeMessage(session);
 // Set From: header field
 message.setFrom(new InternetAddress(from));
 // Set To: header field
 // Set Subject: header field
 message.setSubject("My first message with JavaMail");
 // Put the content of your message
 message.setText("Hi there, this is my first message sent with JavaMail");
 // Send message
 System.out.println("Sending msg");
 System.out.println("Sent message successfully....");
 } catch (MessagingException e) {
 throw new RuntimeException(e);

Sample Output of using in debug mode:

DEBUG: setDebug: JavaMail version 1.4.4
 Sending msg
 DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Sun Microsystems, Inc]
 DEBUG SMTP: useEhlo true, useAuth true
 DEBUG SMTP: useEhlo true, useAuth true
 DEBUG SMTP: trying to connect to host "smtp.pepipost.com", port 587, isSSL false
 DEBUG SMTP: connected to host "smtp.pepipost.com", port: 587
 250-SIZE 50000000
 250 DSN
 DEBUG SMTP: Found extension "PIPELINING", arg ""
 DEBUG SMTP: Found extension "SIZE", arg "50000000"
 DEBUG SMTP: Found extension "VRFY", arg ""
 DEBUG SMTP: Found extension "ETRN", arg ""
 DEBUG SMTP: Found extension "STARTTLS", arg ""
 DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN"
 DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN"
 DEBUG SMTP: Found extension "ENHANCEDSTATUSCODES", arg ""
 DEBUG SMTP: Found extension "8BITMIME", arg ""
 DEBUG SMTP: Found extension "DSN", arg ""
 DEBUG SMTP: Attempt to authenticate
 334 VXNlcm5hbWU6
 334 UGFzc3dvcmQ6
 235 2.7.0 Authentication successful
 DEBUG SMTP: use8bit false
 MAIL FROM:<[email protected]>
 250 2.1.0 Ok
 RCPT TO:<[email protected]>
 250 2.1.5 Ok
 DEBUG SMTP: Verified Addresses
 DEBUG SMTP: [email protected]
 354 End data with <CR><LF>.<CR><LF>
 From: [email protected]
 To: [email protected]
 Message-ID: <51228289.1.1564230903309.JavaMail.rishabh@DESKTOP-9HOCLLI>
 Subject: Tttachment My first message with JavaMail
 MIME-Version: 1.0
 Content-Type: text/plain; charset=us-ascii
 Content-Transfer-Encoding: 7bit
 Hi there, this is my first message sent with JavaMail
 250 2.0.0 Ok: queued as 042C9D87
 221 2.0.0 Bye
 Sent message successfully....

Grade My Email
Check your spam now?

Netcorecloud's toolkit is the solution to all your email problems.

Rishabh Mishra

Programming Guy! Your coding problem corresponder.

You can also explore

Netcore connects & unifies your data across all sources, connects to your marketing channels and provides you with control over AI Powered automation and personalization.

Deploy emails that are
screenshot worthy!

Stop settling for static emails - use Netcore's platform to create dynamic, interactive emails that convert.
Let's Deploy
Get Started Now