0

私は以下のコードのようなコードを使用していますuseridusernameusername and passwordは、メールを送信する場所からのメールのパスワードを示します

Properties props = System.getProperties(); 
props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.host", host); 
props.setProperty("mail.transport.protocol", "smtps");
props.put("mail.smtp.user", userid); 
props.put("mail.smtp.password", password); 
props.put("mail.smtp.port", "465"); 
props.put("mail.smtps.auth", "true"); 
Session session = Session.getDefaultInstance(props, null); 
MimeMessage message = new MimeMessage(session); 
InternetAddress fromAddress = null;
InternetAddress toAddress = null;

try {
fromAddress = new InternetAddress(from);
toAddress = new InternetAddress(to);
} catch (AddressException e) {

e.printStackTrace();
}

message.setFrom(fromAddress);
message.setRecipient(RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(text); 

//SMTPSSLTransport transport =(SMTPSSLTransport)session.getTransport("smtps");

Transport transport = session.getTransport("smtps"); 
transport.connect(host, userid, password); 
transport.sendMessage(message, message.getAllRecipients()); 
transport.close(); 

しかし、それは私に次のようなエラーを与えています

 javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465;
  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.openServer(SMTPTransport.java:1972)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642)

誰かが私のコードの問題を私に提案できますか.....事前にuに感謝します.....

4

1 に答える 1

2
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");

これは、Gmailアカウントを使用してメールを正常に送信するために使用したプロパティ設定です。次に、以下を使用してセッションを作成できます。

Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
                protected PasswordAuthentication getPasswordAuthentication() {
                    return new PasswordAuthentication(emailAddress, password);
                }
              });

これは少し異なるアプローチですが、私が使用しているコードで機能します。

于 2013-02-12T13:36:01.017 に答える