5

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", "me@mydomain.com");
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("john@example.com"));
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.

beginner_
  • 7,230
  • 18
  • 70
  • 127
  • You should follow this link. I had successfully used SMTP way of sending mail via same link. https://www.tutorialspoint.com/javamail_api/javamail_api_gmail_smtp_server.htm – Shankar Saran Singh Jan 08 '20 at 08:06
  • Or also via this link, hope it helps. https://www.javatpoint.com/example-of-sending-email-using-java-mail-api – Shankar Saran Singh Jan 08 '20 at 08:08
  • @ShankarSaranSingh Your links are not for `Multipart` messages – Scary Wombat Jan 08 '20 at 08: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) – Mark Rotteveel Jan 08 '20 at 09:33

4 Answers4

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.

beginner_
  • 7,230
  • 18
  • 70
  • 127
0

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

String from = "from@example.com";
String to = "to@example.com";
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.

The_Cute_Hedgehog
  • 1,280
  • 13
  • 22
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>
hadialaoui
  • 71
  • 1
  • 4
0
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.

Mahmoud
  • 9,729
  • 1
  • 36
  • 47