2017-11-27 23 views
2

私は自分のクラスの1つの最終プロジェクトに取り組んでいます。このプログラムはコード内のアドレスに電子メールを送信するためのものです。ほとんどのコードがどのように動作しているかを知っています。パスワード認証の理解に問題があり、SMTPサーバーに接続して特定のポートを使用する方法がわかりません。コードの問題は、実行時に電子メールを送信せず、エラーメッセージを表示しないことです。どんな助けでも大歓迎です。ここにコードがあります。Java電子メールプログラムが電子メールを送信しない

package application; 

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 SendEmail { 
public static void main (String [] args) { 

    String host="smtp.gmail.com"; 
    final String user="[email protected]"; 
    final String password="password"; 

    String to="targetemail.com"; 

    //imported code 
    Properties props = new Properties(); 
    props.put("mail.smtp.socketfactory.port", "465"); 
    props.put("mail.smtp.port", "465"); 
    props.put("mail.smtp.host",host); 
    props.put("mail.smtp.auth", "true"); 

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

//imported code 
     try { 
     MimeMessage message = new MimeMessage(session); 
     message.setFrom(new InternetAddress(user)); 
     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
     message.setSubject("Dwight from the future"); 
     message.setText("At 8:00, someone poisons the coffee. Do NOT drink 
    it."); 


     Transport.send(message); 

     System.out.println("message sent!"); 

     } 
     catch (MessagingException mex) 
     { 
      System.out.println("Error: unable to send message...."); 
      mex.printStackTrace(); 
     } 
    } 
    } 
+0

HTTPSを確認してください。セキュリティを..ここでは、SSLプロトコル –

+0

を使用して、ほぼすべてのSMTPサーバは完全な作業例http://javabycode.com/spring-framework-tutorial/spring-boot-tutorial/springであるため、 -boot-freemarker-email-template.html –

+0

@VedPrakash彼はポート465、すなわちsmtpsを使用しています。 – Robert

答えて

1

私は、ポート値は、以下のコード(ポート値を変更)を試してみてください

props.put("mail.smtp.port", "587"); 

以下の設定例

props.put("mail.smtp.host", "smtp.gmail.com"); 
props.put("mail.smtp.auth", "true"); 
props.put("mail.smtp.port", "587"); 
props.put("mail.smtp.starttls.enable", "true"); 
props.put("mail.smtp.ssl.trust", "*"); 
+0

ありがとう! props.put( "mail.smtp.starttls.enable"、 "true");というアイデアがありますか? props.put( "mail.smtp.ssl.trust"、 "*");コードは? @praveen – Rinzler

+0

https://stackoverflow.com/questions/18333594/is-starttls-enabled-true-is-safe-for-mail-sending-from-java-codeこのリンクはあなたの質問にお答えします – praveen

+0

それは働いてくれてありがとう!今私は機能しているAPIから始めなければなりません... – Rinzler

0

とされるべきだと思います。

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 Main { 

    public static void main(String[] args) { 

     String host="smtp.gmail.com"; 
     final String user="[email protected]"; 
     final String password="*********"; 

     String to="[email protected]"; 

     //imported code 
     Properties props = new Properties(); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", host); 
     props.put("mail.smtp.user", user); 
     props.put("mail.smtp.password", password); 
     props.put("mail.smtp.port", "587"); 
     props.put("mail.smtp.auth", "true"); 


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

     //imported code 
     try { 
      MimeMessage message = new MimeMessage(session); 
      message.setFrom(new InternetAddress(user)); 
      message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
      message.setSubject("Dwight from the future"); 
      message.setText("At 8:00, someone poisons the coffee. Do NOT drinkit."); 


        Transport.send(message); 

      System.out.println("message sent!"); 

     } 
     catch (MessagingException mex) 
     { 
      System.out.println("Error: unable to send message...."); 
      mex.printStackTrace(); 
     } 

    } 
} 
関連する問題