简体   繁体   中英

Java smtp error when sending email through gmail

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

}

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

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!

下载 smtp.jar https://mvnrepository.com/artifact/com.sun.mail/smtp/1.4.5专门解决您的问题并将其添加到您的项目中。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM