2011-12-23 20 views
2

同じ問題が発生した場合は、次のコードを書いています。しかし、次の例外が発生します。Javaで電子メールを送信できません

javax.mail.MessagingException: SMTPホストに接続できませんでした:smtp.gmail.com、port:587; ネストされた例外はある:java.net.ConnectException:接続がタイムアウトしました:誰かが私がやっている間違いを伝えることができ

public static void main(String[] args) { 

    String to = "[email protected]" // valid gmail address.  
    String from = "[email protected]"; // valid gmail address 

    String host = "smtp.gmail.com"; 
    String password = "****"; // password of the gmaill acc used in from 

    int port = 587; 


    Properties properties = System.getProperties(); 
    properties.put("mail.smtp.starttls.enable", "true"); 
    properties.setProperty("mail.smtp.host",host); 
    properties.setProperty("mail.smtp.user", from); 
    properties.setProperty("mail.smtp.password", password); 
    properties.setProperty("mail.smtp.port", "587"); 
    properties.setProperty("mail.smtp.auth", "true"); 
    Session session = Session.getDefaultInstance(properties,null); 

    try { 

     MimeMessage message = new MimeMessage(session); 

     message.setFrom(new InternetAddress(from)); 

     message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); 

     message.setSubject("Test Mail"); 

     message.setText("This is just a test mail generated"); 

     Transport transport = session.getTransport("smtp"); 
     transport.connect(host,from,password); 
     InternetAddress[] addresses = new InternetAddress[1]; 
     addresses[0] = new InternetAddress(to); 
     transport.sendMessage(message,addresses); 


     System.out.println("Message Sent Successfully"); 
    }catch(MessagingException excp){ 
     System.out.println(excp); 
    } 

} 

を接続します。私のGmailアカウントには、Gmailのsmtpサーバーを使用するように設定する必要のある設定はありますか?

+1

プロキシ/ファイアウォールの背後にいますか? – Santosh

+0

いいえ、「smtp.gmail.com」にpingを実行しようとしましたが、パケットロスなしで応答を返します。だから私はそれがファイアウォールの問題だと思っていない。 – user1043422

答えて

1

次のコードを試してください。 javax.mailパッケージ(Ajarファイル)をダウンロードする必要があります。このコードを試したので、そのJARファイルをダウンロードしたリンクを提供していないため、すでにjarファイルがあると仮定しました。クラスパスを適切に設定し、必要なパッケージをインポートします。ファイアウォールと選択したホストポートに注意してください。

import java.util.logging.Level; 
import java.util.logging.Logger; 
import javax.mail.*; 
import javax.mail.internet.*; 
import java.util.*; 

final class MailClient 
{ 
    private class SMTPAuthenticator extends Authenticator 
    { 
     private PasswordAuthentication authentication; 

     public SMTPAuthenticator(String login, String password) 
     { 
      authentication = new PasswordAuthentication(login, password); 
     } 

     @Override 
     protected PasswordAuthentication getPasswordAuthentication() 
     { 
      return authentication; 
     } 
    } 

    public void mail() 
    { 
     try 
     { 
      String from = "xyz.com"; 
      String to = "abc.com"; 
      String subject = "Your Subject."; 
      String message = "Message Text."; 
      String login = "xyz.com"; 
      String password = "password"; 

      Properties props = new Properties(); 
      props.setProperty("mail.host", "smtp.gmail.com"); 
      props.setProperty("mail.smtp.port", "587"); 
      props.setProperty("mail.smtp.auth", "true"); 
      props.setProperty("mail.smtp.starttls.enable", "true"); 

      Authenticator auth = new SMTPAuthenticator(login, password); 

      Session session = Session.getInstance(props, auth); 

      MimeMessage msg = new MimeMessage(session); 

      try 
      { 
       msg.setText(message); 
       msg.setSubject(subject); 
       msg.setFrom(new InternetAddress(from)); 
       msg.addRecipient(Message.RecipientType.TO, 
       new InternetAddress(to)); 
       Transport.send(msg); 
      } 
      catch (MessagingException ex) 
      { 
       Logger.getLogger(MailClient.class.getName()). 
       log(Level.SEVERE, null, ex); 
      } 
     } 
    } 
} 

final public class Main 
{ 
    public static void main(String...args) 
    { 
     new MailClient().mail(); 
    } 
} 
+2

ありがとうございました。 – user1043422

+1

@ user1043422その場合、この投稿をアップヴォートする必要があります。あなたがこのサイトを少し新しくしているので、あなたの答えなら、それを投票することを忘れないでください。 – Bhavesh

+0

@baveshいいえ、彼はそれが正しいとマークする必要があります。アップグレードは、第三者のためのものです。 – EJP

0

接続タイムアウトは、接続していないことを示します。これは、Gmailアカウントにもアクセスできないことを意味するため、何かを設定すると助けになりません。

あなたはまったく接続できるかどうかを確認するために、そのホスト/ポートに単純なtelnetを試してみてください。できない場合は、接続場所が間違っているか、ファイアウォールの問題が考えられます。

+0

私はsmtp.gmail.comにpingを試みました。私はそれがファイアウォールの問題であるとは思わないので、私は応答を取り戻しています。 – user1043422

+0

@Bill "...あなたのGmailアカウントにもアクセスできませんでした"。いいえ、Gmailのサーバにもアクセスできないことを意味します。* – EJP

+0

さらに、あなたのアカウントにはアクセスできません。 – Bill

1

接続に問題があります。まず「smtp.gmail.com」への接続を確認します。

ジャンプコマンドプロンプトで次のようにpingコマンドを実行します。

ピングsmtp.gmail.com

ファイアウォール問題がある可能性があり、サーバからの応答を取得しない場合。

+0

私はそれを試みました、そして私はパケットロスなしで返信を返します...だから私はここでファイアウォールの問題は見られません。 – user1043422

+1

@ user1043422 pingが成功しても、メールサーバーへの接続が成功したわけではありません。そのポートをブロックするファイアウォールが存在する可能性があります。 – EJP

+0

@EJP:ファイアウォールがそのポートをブロックしているかどうかを確認する方法は? –

関連する問題