我的Java程序停止使用我的Gmail帐户发送电子邮件 [英] My Java program stopped sending emails using my gmail Account
问题描述
我在这里和其他地方搜索了几个相关的帖子,但是他们没有一个能解决我的问题。我有一个使用javamail API将电子邮件发送给一组人的程序。它运行良好一次。今天我再次需要,但我不能发送任何电子邮件...我的sendEmail方法如下:
$ b $ pre $ public void sendEmail(String userName,String password,String toAddress,
String subject,String message,String [] attachFiles)
throws AddressException,MessagingException {
//设置SMTP属性
属性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);
//使用认证器创建一个新的会话
Authenticator auth = new SMTPAuthenticator(userName,password);
Session session = Session.getInstance(properties,auth);
//创建新的电子邮件消息
MimeMessage msg = new MimeMessage(session);
尝试{
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());
//创建消息部分
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(message,text / html);
//创建多部分
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
//添加附件
if(attachFiles!= null&& attachFiles.length> 0){
for(String filePath:attachFiles){
addAttachment(multipart,filePath);
}
}
//将多部分设置为电子邮件的内容
msg.setContent(multipart);
//发送电子邮件
Transport.send(msg);
$ b因此,现在试图调用这个方法我得到了以下错误
(我正在使用jdk 1.7.0_21):
发送电子邮件失败...
javax.mail.MessagingException:无法将套接字转换为TLS;
嵌套异常是:
javax.net.ssl.SSLHandshakeException:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效的证书路径请求目标
,位于com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)$ b $ javax.mail.Service.connect(Service.java:367)$ b在javax.mail.Service.connect(Service.java:226)
在javax.mail.Service.connect(Service .java:175)
在javax.mail.Transport.send0(Transport.java:253)
在javax.mail.Transport.send(Transport.java:124)
在EmailSender。 (CFP_LaWasp_EmailSender.java:220)
引起:javax.net.ssl .SSLHandshakeException:sun.security.validator。 ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:在sun.security.ssl.Alerts.getSSLException(Alerts.java:192)$ b $无法找到所请求目标的有效证书路径
b在sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
在sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
在sun.security.ssl。 Handshaker.fatalSE(Handshaker.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java: 153)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
at sun .security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHand (SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java: 528)
在com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:465)
在com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
... 9 more
导致:sun.security.validator.ValidatorException:PKIX路径构建失败:sun.security.provider.certpath.SunCertPathBuilderException:无法找到有效的证书路径到所请求的目标
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator .validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.j ava:231)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
... 19 more
导致:sun.security.provider.certpath.SunCertPathBuilderException:在sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder。)无法找到有效的证书路径到所请求的目标
。
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
... 25更多
我不知道如何解决这个问题,因为它曾经工作之前...另外,我不知道我的Gmail是否与此有关(如果它阻止了此访问...)
感谢任何帮助。
关键错误是这样的:
。安全。 provider.certpath.SunCertPathBuilderException:无法找到要求的目标的有效证书路径
请参阅此JavaMail FAQ条目。
由于您连接到Gmail,因此不会发生这种情况。最可能的原因是:
- 防火墙或防病毒程序拦截您的请求。
- 您的JDK安装中存在错误,导致它无法找到可信的证书颁发机构
- 您正在运行在覆盖JDK的可信证书颁发机构列表的应用程序服务器中
I searched several related posts here and in other places, but none of them solved my problem. I have a program that sends emails to a set of people using "javamail API". It worked fine once. Today I needed again, but I cannot send any email... My sendEmail method is the following:
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);
}
So, now the attempt to call this method I got the following error (I'm using jdk 1.7.0_21):
Sending email Failed...
javax.mail.MessagingException: Could not convert socket to TLS;
nested exception is:
javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1907)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:666)
at javax.mail.Service.connect(Service.java:367)
at javax.mail.Service.connect(Service.java:226)
at javax.mail.Service.connect(Service.java:175)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at EmailSender.sendEmail(EmailSender.java:86)
at CFP_LaWasp_EmailSender.sendCFPLaWasp(CFP_LaWasp_EmailSender.java:178)
at CFP_LaWasp_EmailSender.main(CFP_LaWasp_EmailSender.java:220)
Caused by: javax.net.ssl.SSLHandshakeException: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.ssl.Alerts.getSSLException(Alerts.java:192)
at sun.security.ssl.SSLSocketImpl.fatal(SSLSocketImpl.java:1886)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:276)
at sun.security.ssl.Handshaker.fatalSE(Handshaker.java:270)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1341)
at sun.security.ssl.ClientHandshaker.processMessage(ClientHandshaker.java:153)
at sun.security.ssl.Handshaker.processLoop(Handshaker.java:868)
at sun.security.ssl.Handshaker.process_record(Handshaker.java:804)
at sun.security.ssl.SSLSocketImpl.readRecord(SSLSocketImpl.java:1016)
at sun.security.ssl.SSLSocketImpl.performInitialHandshake(SSLSocketImpl.java:1312)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1339)
at sun.security.ssl.SSLSocketImpl.startHandshake(SSLSocketImpl.java:1323)
at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:528)
at com.sun.mail.util.SocketFetcher.startTLS(SocketFetcher.java:465)
at com.sun.mail.smtp.SMTPTransport.startTLS(SMTPTransport.java:1902)
... 9 more
Caused by: sun.security.validator.ValidatorException: PKIX path building failed: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:385)
at sun.security.validator.PKIXValidator.engineValidate(PKIXValidator.java:292)
at sun.security.validator.Validator.validate(Validator.java:260)
at sun.security.ssl.X509TrustManagerImpl.validate(X509TrustManagerImpl.java:326)
at sun.security.ssl.X509TrustManagerImpl.checkTrusted(X509TrustManagerImpl.java:231)
at sun.security.ssl.X509TrustManagerImpl.checkServerTrusted(X509TrustManagerImpl.java:126)
at sun.security.ssl.ClientHandshaker.serverCertificate(ClientHandshaker.java:1323)
... 19 more
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
at sun.security.provider.certpath.SunCertPathBuilder.engineBuild(SunCertPathBuilder.java:196)
at java.security.cert.CertPathBuilder.build(CertPathBuilder.java:268)
at sun.security.validator.PKIXValidator.doBuild(PKIXValidator.java:380)
... 25 more
I don't know how to solve this issue, since it used to work before... Also, I don't know if my Gmail has something to do with this (if it blocked this access...)
I appreciate any help.
The key error is this:
Caused by: sun.security.provider.certpath.SunCertPathBuilderException: unable to find valid certification path to requested target
Since you're connecting to Gmail, this shouldn't happen. The most likely causes are:
- There's a firewall or anti-virus program intercepting your request.
- There's something wrong in your JDK installation preventing it from finding the trusted certificate authorities
- You're running in an application server that has overridden the JDK's list of trusted certificate authorities
这篇关于我的Java程序停止使用我的Gmail帐户发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!