簡體   English   中英

Javamail傳輸錯誤

[英]Javamail transport error

我以前在幾個項目中都使用過javamail,直到最近,它始終運行良好(可能與升級到Java 8有關?),現在它總是返回以下異常:

Exception in thread "main" javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  nested exception is:
    javax.net.ssl.SSLKeyException: RSA premaster secret error
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1963)
    at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:654)
    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 one.Demo.main(Demo.java:39)
Caused by: javax.net.ssl.SSLKeyException: RSA premaster secret error
    at sun.security.ssl.RSAClientKeyExchange.<init>(Unknown Source)
    at sun.security.ssl.ClientHandshaker.serverHelloDone(Unknown Source)
    at sun.security.ssl.ClientHandshaker.processMessage(Unknown Source)
    at sun.security.ssl.Handshaker.processLoop(Unknown Source)
    at sun.security.ssl.Handshaker.process_record(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.readRecord(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.performInitialHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at sun.security.ssl.SSLSocketImpl.startHandshake(Unknown Source)
    at com.sun.mail.util.SocketFetcher.configureSSLSocket(SocketFetcher.java:528)
    at com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:333)
    at com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:229)
    at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1927)
    ... 7 more
Caused by: java.security.NoSuchAlgorithmException: SunTlsRsaPremasterSecret KeyGenerator not available
    at javax.crypto.KeyGenerator.<init>(KeyGenerator.java:159)
    at javax.crypto.KeyGenerator.getInstance(KeyGenerator.java:208)
    at sun.security.ssl.JsseJce.getKeyGenerator(Unknown Source)
    ... 20 more

根據我的理解,該錯誤似乎是由傳輸類的send方法派生的。 這是我遇到的問題的MCVE。 它使用Gmail stmp服務器。

import java.util.Date;
import java.util.Properties;

import javax.mail.*;
import javax.mail.internet.*;

public class Demo {

  public static void main(String[] args) throws MessagingException {
    Properties props = new Properties();
    props.put("mail.smtp.host", "smtp.gmail.com");
    props.put("mail.smtp.port", 465);
    props.put("mail.smtp.ssl.enable", true);
    props.put("mail.smtp.auth", true);
    Authenticator auth = new Authenticator() {
      private PasswordAuthentication pa = new PasswordAuthentication(
        "my_email",
        "my_password");
      public PasswordAuthentication getPasswordAuthentication(){
        return pa;
      }
    };
    Session session = Session.getInstance(props, auth);
    session.setDebug(false);

    MimeMessage message = new MimeMessage(session);

    Address address = new InternetAddress("other_email");
    message.addRecipient(Message.RecipientType.TO, address);

    message.setFrom(new InternetAddress("my_email"));
    message.setSubject("Test");
    message.setText("This is a test of the Javamail API.");
    message.setSentDate(new Date());

    Transport.send(message);
  }

}

查看這篇文章是否有幫助。 看起來有些與JDK的安裝或配置有關的問題。 另外,您要通過代理服務器還是防火牆?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM