6

First time working with java mail. I'm following this tutorial but I already fail at sending a basic message and I get a very strange error:

java.util.ServiceConfigurationError: javax.mail.Provider: Provider com.sun.mail.imap.IMAPProvider not a subtype

Strange because I'm not using IMAP anywhere in my code:

Properties mailProps = new Properties();
mailProps.put("mail.transport.protocol", "smtp");
mailProps.put("mail.host", "smtp.mydomain.com");
mailProps.put("mail.from", "[email protected]");
mailProps.put("mail.smtp.port", "25");     

Session session = Session.getDefaultInstance(mailProps);
SMTPMessage m = new SMTPMessage(session);
MimeMultipart content = new MimeMultipart();
MimeBodyPart mainPart = new MimeBodyPart();
mainPart.setText("test");
content.addBodyPart(mainPart);  
m.setContent(content);
m.setSubject("Demo message");

m.setRecipient(RecipientType.TO, new InternetAddress("[email protected]"));
Transport.send(m);

The error happens on last line (send). I know the smtp server is correct and working.

Any advice why this happens and how I can solve it?

EDIT: obviously the addresses/hosts are changed here and I'm using real ones that work in the actual code.

4
  • You should follow this link. I had successfully used SMTP way of sending mail via same link. tutorialspoint.com/javamail_api/… Commented Jan 8, 2020 at 8:06
  • Or also via this link, hope it helps. javatpoint.com/example-of-sending-email-using-java-mail-api Commented Jan 8, 2020 at 8:08
  • @ShankarSaranSingh Your links are not for Multipart messages Commented Jan 8, 2020 at 8:27
  • Please post the full exception stacktrace. It is possible that this problem occurs if you have multiple copies of JavaMail/JakartaMail on the classpath, so please also post your dependency list (eg Maven pom.xml or Gradle build.gradle) Commented Jan 8, 2020 at 9:33

4 Answers 4

2

it turns out I was running into multiple issues:

  1. Issue with tutorial

It uses com.sun.mail.smtp.SMTPMessage but in my case that doesn't work but using javax.mail.internet.MimeMessage works fine.

  1. Root cause of error

Above code runs within a 3rd party eclipse based application and they seem to interfere with each other. The solution for this can be found here:

Thread t =  Thread.currentThread();
ClassLoader ccl = t.getContextClassLoader();
t.setContextClassLoader(session.getClass().getClassLoader());
try {
    Transport.send(m);
} finally {
    t.setContextClassLoader(ccl);
}

Adjusting the code accordingly make it work.

Sign up to request clarification or add additional context in comments.

Comments

1
java.util.ServiceConfigurationError: jakarta.mail.Provider: com.sun.mail.imap.IMAPProvider not a subtype

I had a similar problem because the project is using jakarta.mail; and one of the dependencies brought javax.mail as a transitive dependency.

Excluding the the old javax.mail should do the trick.

<dependency>
    <groupId>org.abc</groupId>
    <artifactId>xyz</artifactId>
    <version>0.0.0</version>
    <exclusions>
        <exclusion>
            <groupId>com.sun.mail</groupId>
            <artifactId>javax.mail</artifactId>
        </exclusion>
    </exclusions>
</dependency>

mvn dependency:tree views all the the dependencies and their dependencies.

Comments

0

This is an example for sending a multipart message with attachment:

String from = "[email protected]";
String to = "[email protected]";
File file = new File("/file/to/attach");

Properties mailProps = new Properties();
// put your properties here
Session session = Session.getInstance(mailProps, null);

try {
 MimeMessage message = new MimeMessage(session);
 message.setFrom(new InternetAddress(from) );
 InternetAddress[] toAddress = { new InternetAddress(to) };
 message.setRecipients(Message.RecipientType.TO, toAddress);
 message.setSubject("Demo Message");
 message.setSentDate(new Date());

 MimeBodyPart part1 = new MimeBodyPart();
 part1.setText("Test");

 MimeBodyPart part2 = new MimeBodyPart();
 part2.attachFile(file);

 Multipart multiPart = new MimeMultipart();
 multiPart.addBodyPart(part1);
 multiPart.addBodyPart(part2);

 message.setContent(multiPart);

 Transport.send(message);

} catch( MessagingException e ) {
  // handle the exception properly
  e.printStackTrace();
}

Hope it helps.

Comments

0

I had the same problem with libertyCore server I add this feature in server.xml and it's work for me

<feature>javaMail-1.6</feature>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.