我的Java程序停止使用我的gmail帐户发送电子邮件
我在这里和其他地方搜索了几个相关的帖子,但都没有解决我的问题。我有一个程序,使用“javamail API”向一群人发送电子邮件。有一次效果很好。今天我再次需要,但我不能发送任何电子邮件。。。我的sendEmail方法如下:我的Java程序停止使用我的gmail帐户发送电子邮件,gmail,jakarta-mail,Gmail,Jakarta Mail,我在这里和其他地方搜索了几个相关的帖子,但都没有解决我的问题。我有一个程序,使用“javamail API”向一群人发送电子邮件。有一次效果很好。今天我再次需要,但我不能发送任何电子邮件。。。我的sendEmail方法如下: public void sendEmail(String userName, String password, String toAddress, String subject, String message, String[] attachFiles)
public void sendEmail(String userName, String password, String toAddress,
String subject, String message, String[] attachFiles)
throws AddressException, MessagingException {
// sets SMTP properties
Properties properties = new Properties();
properties.put("mail.smtp.host", "smtp.gmail.com");
properties.put("mail.smtp.port", "587");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.starttls.enable", "true");
properties.put("mail.user", userName);
properties.put("mail.password", password);
// creates a new session with an authenticator
Authenticator auth = new SMTPAuthenticator(userName, password);
Session session = Session.getInstance(properties, auth);
// creates a new e-mail message
MimeMessage msg = new MimeMessage(session);
try {
msg.setFrom(new InternetAddress(userName, "My name"));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
InternetAddress[] toAddresses = {new InternetAddress(toAddress)};
msg.setRecipients(Message.RecipientType.TO, toAddresses);
msg.setSubject(subject);
msg.setSentDate(new Date());
// creates message part
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message, "text/html");
// creates multi-part
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// adds attachments
if (attachFiles != null && attachFiles.length > 0) {
for (String filePath : attachFiles) {
addAttachment(multipart, filePath);
}
}
// sets the multi-part as e-mail's content
msg.setContent(multipart);
// sends the e-mail
Transport.send(msg);
}
因此,现在尝试调用此方法时,我得到了以下错误
(我使用的是JDK1.7.021):
我不知道如何解决这个问题,因为它以前是有效的。。。另外,我不知道我的Gmail是否与此有关(如果它阻止了访问…)
非常感谢您的帮助。关键错误是:
原因:sun.security.provider.certpath.SunCertPathBuilderException:找不到请求目标的有效证书路径
看
既然你正在连接Gmail,这就不应该发生。最可能的原因是:
- 有防火墙或反病毒程序拦截你的请求
- 您的JDK安装中出现了一些错误,使其无法找到受信任的证书颁发机构
- 您运行的应用程序服务器覆盖了JDK的可信证书颁发机构列表
- 我已经为这个错误挣扎了几个小时。我尝试使用Bil Shannon提供的答案安装服务器证书。什么都没用
我的问题是AVAST杀毒软件。一旦我禁用了Avast Mail Shield,我就可以从我的web应用程序通过gmail帐户发送邮件了。我在java 8上遇到了这个问题。更新此属性后,问题得到解决 properties.put(“mail.smtp.ssl.trust”、“smtp.gmail.com”) 如果在application.property中使用spring引导
spring.mail.properties.mail.smtp.ssl.trust=smtp.gmail.com我的问题已通过以下方式得到解决:
properties.put("mail.smtp.ssl.trust", "smtp.gmail.com")
当我用弹簧靴的时候
spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com
我终于发现了问题!是防病毒软件阻止了应用程序。谢谢!你的建议帮了大忙:-)这一条帮了大忙。。。在我的案例中,avast正在这样做。谢谢,我花了2个小时找到解决方案来修复“PKIX路径构建失败”,最后,它是该死的avast。在我的案例中,我也是avast。以下是修复方法:
spring.mail.properties.mail.smtp.ssl.trust = smtp.gmail.com