2016-03-26 1 views
0

Première question.Utilisation du service de messagerie Google avec SSL sur Resin 3.1 dans AWS

Je cherche de l'aide pour configurer java & résine pour me permettre d'ouvrir une connexion URL https à partir de mon application. Les documents Resin ne disent pas, je pense que je fais la bonne chose en Java, mais je cours dans un "java.lang.NoClassDefFoundError: Impossible d'initialiser la classe sun.net.www.protocol.https.DelegateHttpsUrlConnection" . Je m'attends à ce que le correctif soit une seule ligne dans un fichier de configuration ou un pot manquant ... mais lequel ??

Plus de détails:

Linux Oracle JDK 1.7 Résine 3.1 (oui il est vieux) Google Mail api v1 (voir la dépendance maven ci-dessous)

J'ai mis en place une classe GmailSender qui envoie un courrier électronique à de mon application:

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); 
} 

Et la configuration 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> 

Et l'exception - semble généralement bon, sauf pour la question de la définition de la classe:

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)

Je l'ai expérimenté avec la définition des protocoles dans ma résine script de démarrage:

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" 

Mais ce changement semble avoir aucun impact. Qu'est-ce que je rate??

Merci!

Répondre

0

une autre façon d'envoyer un courriel en utilisant 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); 
     } 
    } 
} 

si vous obtenir javax.mail.AuthenticationFailedException s'il vous plaît aller aux réglages gmail
https://www.google.com/settings/security/lesssecureapps

+0

Je m'éloigne de cette approche pour utiliser l'API Google Mail plus rapide et plus sécurisée. Mes tests Junit fonctionnent bien - ils ne fonctionnent pas dans le conteneur Web. – tfield

0

On dirait que l'erreur noClassDefFound était due à un être ExceptionInInitializer jeté dans la classe de connexion URL, car l'enregistreur n'était pas correctement initialisé.

L'enregistrement a été révisé dans la résine 3.1.5 et j'ai vérifié les notes de version pour Resin et n'a pas vu ce problème signalé. Mais juste au cas où, je suis passé à la version 3.1.15 et voilà le problème est parti.

Bottom line - il y a un méchant bug dans la résine 3.1.5 journalisation qui explose java 7 connexions URL https. Qui savait ....