2017-08-12 11 views
1

私はテンプレートを作成するHTMLコードを追加したいので、電子メールテンプレートを作成します。コードの下にこのport 465の番号が機能していない 誰でも私を助けることができますか?コードが例外実行取得されていないSMTPホストに接続できませんでした:smtp.gmail.com、ポート:465、応答:-1なぜ465が機能していません

package com.indoabus2.mail; 

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendHTMLEmail { 

public static void main(String[] args) { 
     // Recipient's email ID needs to be mentioned. 
     String to = "[email protected]"; 

     // Sender's email ID needs to be mentioned 
     String from = "[email protected]"; 
     final String username = "vpenchalaprasad2";//change accordingly 
     final String password = "100509732041";//change accordingly 

     // Assuming you are sending email through relay.jangosmtp.net 
     String host = "smtp.gmail.com"; 

     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.port", "465"); 

     // Get the Session object. 
     Session session = Session.getInstance(props, 
     new javax.mail.Authenticator() { 
      protected PasswordAuthentication getPasswordAuthentication() { 
       return new PasswordAuthentication(username, password); 
      } 
    }); 

     try { 
      // Create a default MimeMessage object. 
      Message message = new MimeMessage(session); 

     // Set From: header field of the header. 
     message.setFrom(new InternetAddress(from)); 

     // Set To: header field of the header. 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(to)); 

     // Set Subject: header field 
     message.setSubject("Testing Subject"); 

     // Send the actual HTML message, as big as you like 
     message.setContent(
       "<h1>This is actual message embedded in HTML tags</h1>", 
      "text/html"); 

     // Send message 
     Transport.send(message); 

     System.out.println("Sent message successfully...."); 

     } catch (MessagingException e) { 
     e.printStackTrace(); 
     throw new RuntimeException(e); 
     } 
    } 



} 

ナットはいずれかが私に解決策を提案することができ

javax.mail.MessagingException: Could not connect to SMTP host: smtp.gmail.com, port: 465, response: -1 

が、私はそれをトレースすることはできませんよ、なぜ465動作していないで、そしてresponse: -1

答えて

1

Google SMTPではポート465のSTARTTLSの代わりにSSLが必要です。

ただ削除:

props.put("mail.smtp.starttls.enable", "true");

とSSLを使用することで追加します。

props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");

それとも、ちょうど587

https://support.google.com/a/answer/176600?hl=en-EN

+1

代わりのSocketFactoryを指定するポートを変更することができますが、 "より良い方法はsmtpの代わりにJavaMail smtpsプロトコルを使用することです。 –

+0

その行を変更した後に再生していただきありがとうございます。次の例外が発生しています: 'javax.mail.AuthenticationFailedException:535-5.7.8ユーザー名とパスワードは受け入れられません。私のパスワードとメールIDは正しい – bharath

+0

「あなたのアカウントにアクセスするには安全性の低いアプリ」を有効にする必要があります。この[ページ]をご覧ください(https://support.google.com/accounts/answer/6010255?hl= en) –

関連する問題