2011-03-08 20 views
3

からの電子メールを送信する私はEJBをウッシング電子メールを送信したいが、私私はこの例外に復帰してもらうだけの事:JavaのステートレスEJB(のJava EE-6)

java.lang.RuntimeException: javax.mail.AuthenticationFailedException: failed to connect, no password specified? 

これはどのように私のEJBであります私はそれがパスワードが指定されていないことを言って続けて理由を知らない

@Stateless(name = "ejbs/EmailServiceEJB") 
public class EmailServiceEJB extends Authenticator implements IEmailServiceEJB { 

public PasswordAuthentication getPasswordAuthentication() { 
    return new PasswordAuthentication("[email protected]", 
      "xxxxxxx"); 
} 

public void sendAccountActivationLinkToBuyer(String destinationEmail, 
     String name) { 
    // OUR EMAIL SETTINGS 
    String host = "smtp.gmail.com";// Gmail 
    int port = 465; 
    String serviceUsername = "[email protected]"; 
    String servicePassword = "xxxxxxx";// Our Gmail password 

    Properties props = new Properties(); 
    props.put("mail.smtp.user", serviceUsername); 
    props.put("mail.smtp.host", host); 
    props.put("mail.smtp.port", port); 
    props.put("mail.smtp.starttls.enable", "true"); 
    props.put("mail.smtp.debug", "true"); 
    props.put("mail.smtp.auth", "true"); 
    props.put("mail.smtp.socketFactory.port", port); 
    props.put("mail.smtp.socketFactory.class", 
      "javax.net.ssl.SSLSocketFactory"); 
    props.put("mail.smtp.socketFactory.fallback", "false"); 

    // Destination of the email 
    String to = destinationEmail; 
    String from = "[email protected]"; 

    // Creating a javax.Session with the our properties 
    Session session = Session.getInstance(props); 

    try { 
     Message message = new MimeMessage(session); 
     // From: is our service 
     message.setFrom(new InternetAddress(from)); 
     // To: destination given 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse(to)); 
     message.setSubject("Comfirm your account"); 
     // Instead of simple text, a .html template should be added here! 
     message.setText("Welcome....... "); 

     Transport transport = session.getTransport("smtp"); 
     transport.connect(host, port, serviceUsername, servicePassword); 
     Transport.send(message, message.getAllRecipients()); 
     transport.close(); 

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

} 

}

:のように見えますか? SSLSocketFactoryには何か関係がありますか?どこかでgetPasswordAuthenticion()メソッドを呼び出す必要がありますか、マネージドBeanから2番目のメソッドを呼び出す必要がありますか?

+0

GlassFishのようなアプリケーションサーバーを使用していますか? – musiKk

+0

はいグラスフィッシュ3.0 – sfrj

答えて

8

あなたのコードが動作しない理由を私は知らないが、あなたはこれを見てとることをお勧めします:

http://spitballer.blogspot.com/2010/02/sending-email-via-glassfish-v3.html

これは、Java EEに電子メールを設定する別の方法を示し、その私は仕事がはるかに良いと思う。接続の詳細は、コンテナ管理のデータベース接続と同様に、コンテナレベルで構成されます。

+1

Seconded。これはアプリケーションサーバーについて尋ねたときに私が気にしていたことです。これは確かに行く方法です。 – musiKk

+0

これはすばらしい答えでした:)私はちょうど10分でそれをやりました。私のアプリはメールをGmailのアカウントを使って送信しています。私は驚いていました。 – sfrj

1

SPFとDKIMを読んでください。

あなたのコードは必要な情報の1/2しかないので、ここで何がうまくいかないのか分かりにくいです。実際の問題はおそらくあなたのコードがあなたの環境に合わないことで、SPFとDKIMはしばしばパブリックSMTPサーバーに接続して電子メールを送信することができない理由です。

少なくとも、SPFとDKIMについて少し読んだ後は、それがあなたのケースで問題であるかどうか、またはあなたのケースでは問題ではないかどうかを知ることができます。

関連する問題