2016-03-26 18 views
0

初めての質問です。AWSのResin 3.1でSSLを使用したGoogleメールサービスを使用

Google &樹脂を設定すると、アプリ内からhttps URL接続を開くことができます。 Resinのドキュメントでは、私はJavaで正しいことをしているとは思わないが、 "java.lang.NoClassDefFoundError:クラスsun.net.www.protocol.https.DelegateHttpsUrlConnectionを初期化できませんでした" 。私は修正が、設定ファイル内の1行か見つからないjar ...のいずれかだと思っていますが、どれが1つですか?

詳細:

のOracleのLinux JDK 1.7 樹脂3.1(以下Mavenの依存関係を参照してください)

私は電子メールを送信するGmailSenderクラスを実装しました GoogleのメールAPI v1の(はい、それは古いです)私のアプリから:

httpTransport = GoogleNetHttpTransport.newTrustedTransport(); 
... 

private Credential authorizeServiceAccount() throws IOException, GeneralSecurityException { 
     Credential credential = new GoogleCredential.Builder() 
       .setTransport(httpTransport) 
       .setJsonFactory(JSON_FACTORY) 
       .setServiceAccountId(serviceAccountId) 
       .setServiceAccountPrivateKeyFromP12File(new File(privateKeyFileName)) 
       .setServiceAccountScopes(SCOPES) 
       .setServiceAccountUser(serviceAccountUser) 
       .build(); 
     return credential; 
} 

Gmail getGmailService() throws IOException, GeneralSecurityException { 
    Credential credential = authorizeServiceAccount(); 
    return new Gmail.Builder(httpTransport, JSON_FACTORY, credential) 
      .setApplicationName(applicationName) 
      .build(); 
} 

public void send(SimpleMailMessage simpleMailMessage) throws MailException { 
    Message m; 
    try { 
     MimeMessage mm = asMimeMessage(simpleMailMessage); 
     m = asMessage(mm); 
    } catch (IOException e) { 
     throw new MailPreparationException("Unable to create email", e); 
    } catch (MessagingException e) { 
     throw new MailPreparationException("Unable to create email", e); 
    } 

    try { 
     Gmail gmail = getGmailService(); 
     m = gmail.users().messages().send("me", m).execute(); 
    } catch (IOException e) { 
     throw new MailSendException("Unable to send mail", e); 
    } catch (GeneralSecurityException e) { 
     throw new MailSendException("Could not send email", e); 
    } catch (Throwable t) { 
     throw new MailSendException("Unexpected failure sending email", t); 
    } 
    String id = m.getId(); 
    //System.out.println("Mail sent. Id is: " + id); 
} 

とMavenの設定:

<dependency> 
     <groupId>com.google.apis</groupId> 
     <artifactId>google-api-services-gmail</artifactId> 
     <version>v1-rev35-1.21.0</version> 
    </dependency> 
    <dependency> 
     <groupId>com.google.oauth-client</groupId> 
     <artifactId>google-oauth-client-jetty</artifactId> 
     <version>1.21.0</version> 
    </dependency> 

と例外 - クラスDEFの問題を除いてほとんどがよさそうだ:

20160326-15:15:54.481org.springframework.mail.MailSendException; nested exceptions (0) are: 20160326-15:15:54.481Caused by: java.lang.NoClassDefFoundError: Could not initialize class sun.net.www.protocol.https.DelegateHttpsURLConnection 20160326-15:15:54.481 at sun.net.www.protocol.https.HttpsURLConnectionImpl.(HttpsURLConnectionImpl.java:86) 20160326-15:15:54.481 at sun.net.www.protocol.https.Handler.openConnection(Handler.java:62) 20160326-15:15:54.481 at sun.net.www.protocol.https.Handler.openConnection(Handler.java:57) 20160326-15:15:54.481 at java.net.URL.openConnection(URL.java:971) 20160326-15:15:54.481 at com.google.api.client.http.javanet.DefaultConnectionFactory.openConnection(DefaultConnectionFactory.java:31) 20160326-15:15:54.481 at com.google.api.client.http.javanet.NetHttpTransport.buildRequest(NetHttpTransport.java:136) 20160326-15:15:54.481 at com.google.api.client.http.javanet.NetHttpTransport.buildRequest(NetHttpTransport.java:62) 20160326-15:15:54.481 at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:863) 20160326-15:15:54.481 at com.google.api.client.auth.oauth2.TokenRequest.executeUnparsed(TokenRequest.java:283) 20160326-15:15:54.481 at com.google.api.client.auth.oauth2.TokenRequest.execute(TokenRequest.java:307) 20160326-15:15:54.481 at com.google.api.client.googleapis.auth.oauth2.GoogleCredential.executeRefreshToken(GoogleCredential.java:384) 20160326-15:15:54.481 at com.google.api.client.auth.oauth2.Credential.refreshToken(Credential.java:489) 20160326-15:15:54.481 at com.google.api.client.auth.oauth2.Credential.intercept(Credential.java:217) 20160326-15:15:54.481 at com.google.api.client.http.HttpRequest.execute(HttpRequest.java:859) 20160326-15:15:54.481 at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:419) 20160326-15:15:54.481 at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.executeUnparsed(AbstractGoogleClientRequest.java:352) 20160326-15:15:54.481 at com.google.api.client.googleapis.services.AbstractGoogleClientRequest.execute(AbstractGoogleClientRequest.java:469) 20160326-15:15:54.481 at MyPackage.GmailSender.send(GmailSender.java:155)

私は私の樹脂起動スクリプトでプロトコルを設定することで実験しました:

args="-jar $RESIN_HOME/lib/resin.jar -server-root $SERVER_ROOT -conf $config -server $SERVER_NAME" 
#args="-Dhttps.protocols=TLSv1.2,TLSv1.1,TLSv1 -jar $RESIN_HOME/lib/resin.jar -server-root $SERVER_ROOT -conf $config -server $SERVER_NAME" 

しかし、その変化はそう影響を与えない。私は何が欠けているのですか?

ありがとうございます!

答えて

0

あなたがjavax.mail.AuthenticationFailedException を取得した場合のjava

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 SendEmailUsingGMailSMTP { 
    public static void main(String[] args) { 
     // Recipient's email ID needs to be mentioned. 
     String to = "[email protected]";//change accordingly 

     // Sender's email ID needs to be mentioned 
     String from = "[email protected]";//change accordingly 
     final String username = "testemail";//change accordingly 
     final String password = "yourPassword";//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", "587"); 

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

     // Now set the actual message 
     message.setText("Hello, this is sample for to check send " 
      + "email using JavaMailAPI "); 

     // Send message 
     Transport.send(message); 

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

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

を使用して電子メールを送信する別の方法は、Gmailの設定に
https://www.google.com/settings/security/lesssecureapps

+0

私は、より速くて安全なGoogle Mail APIを使用するというアプローチから離れています。私のJunitテストは正常に動作します。Webコンテナで実行されません。 – tfield

0

を行ってくださいがNoClassDefFoundエラーがあることExceptionInInitializerによるものであったように見えますロガーが正しく初期化されていないため、URL接続クラスでスローされました。

ロギングを樹脂3.1.5でオーバーホールし、Resinのリリースノートを確認しましたが、この問題は報告されていませんでした。しかし、その場合には、私はResin 3.1.15にアップグレードして、問題を解決しました。

ボトムライン - 樹脂3.1.5のロギングに厄介なバグがあり、Java 7のhttps URL接続を吹き飛ばします。誰が知っていた....

関連する問題