I am creating a larger program that will have emails sent to the users account whenever an event happens, and right now I am just focusing on making the email sending work.
Right now, I have it functioning perfectly in the IDE
(IntelliJ) with no errors or warnings, but after I jar
the file and run it in the terminal
I get an error every time the program tries to send an email.
I am assuming I jarred the file wrong since it works in the IDE
perfectly fine, but I am not too sure. I have looked up similar issues to mine but have not found a working solution.
This is the file that has issues in the terminal
package handler;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMessage.RecipientType;
import java.util.Properties;
public class Sender {
private Sender(){}
private static final String SENDERS_GMAIL = "myemail@email.com";
private static final String SENDERS_PASSWORD = "mypassword";
private static final String RECEIEVES_EMAIL = "myemail@email.com";
private static Properties mailServerProperties;
private static Session mailSession;
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");
mailSession = Session.getDefaultInstance(mailServerProperties);
mailMessage = new MimeMessage(mailSession);
mailMessage.addRecipient(RecipientType.BCC, new InternetAddress(RECEIEVES_EMAIL));
mailMessage.setSubject("Test Email");
mailMessage.setContent(emailBody, "text/html");
Transport transport = mailSession.getTransport("smtp");
transport.connect("smtp.gmail.com", SENDERS_GMAIL, SENDERS_PASSWORD);
transport.sendMessage(mailMessage, mailMessage.getAllRecipients());
transport.close();
}
}
And whenever I run the .jar
in the terminal
, this is the error I get:
C:\Users\genlap\EmailSender>java -jar SendEmail.jar
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 handler.Sender.sendMail(Sender.java:37)
at handler.ManageService.run(ManageService.java:32)
at java.lang.Thread.run(Unknown Source)
Message failed to be sent.
The line in the Sender
file that is being called out in the error is
Transport transport = mailSession.getTransport("smtp");
Does anybody know how I can solve this?