Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在AWS的Resin 3.1上使用带SSL的Google电子邮件服务_Java_Https_Configuration_Gmail Api_Resin - Fatal编程技术网

Java 在AWS的Resin 3.1上使用带SSL的Google电子邮件服务

Java 在AWS的Resin 3.1上使用带SSL的Google电子邮件服务,java,https,configuration,gmail-api,resin,Java,Https,Configuration,Gmail Api,Resin,第一个问题 我正在寻找有关配置java&resin以允许我从应用程序中打开https URL连接的帮助。树脂文档没有说,我认为我在Java中做了正确的事情,但我遇到了“Java.lang.NoClassDefFoundError:无法初始化类sun.net.www.protocol.https.DelegateHttpsUrlConnection”。我希望修复要么是配置文件中的一行,要么是缺少jar。。。但是哪一个呢 更多详情: 甲骨文linux JDK1.7 树脂3.1(是的,它是旧的) 谷歌

第一个问题

我正在寻找有关配置java&resin以允许我从应用程序中打开https URL连接的帮助。树脂文档没有说,我认为我在Java中做了正确的事情,但我遇到了“Java.lang.NoClassDefFoundError:无法初始化类sun.net.www.protocol.https.DelegateHttpsUrlConnection”。我希望修复要么是配置文件中的一行,要么是缺少jar。。。但是哪一个呢

更多详情:

甲骨文linux JDK1.7 树脂3.1(是的,它是旧的) 谷歌邮件api v1(见下面的maven依赖项)

我已经实现了一个Gmail Sender类,可以从我的应用程序发送电子邮件:

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>
但这一变化似乎没有影响。我错过了什么


谢谢

使用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 = "reciverEmail@gmail.com";//change accordingly

      // Sender's email ID needs to be mentioned
      String from = "testemail@gmail.com";//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);
      }
   }
}
如果您获得javax.mail.AuthenticationFailedException 请转到gmail设置

看来NoClassDefFound错误是由于URL连接类中抛出了ExceptionInInitializer,因为记录器未正确初始化

在resin 3.1.5中对日志记录进行了彻底检查,我检查了resin的发行说明,没有看到这个问题的报告。但以防万一,我升级到了树脂3.1.15,瞧,问题已经解决了


一句话-resin 3.1.5日志记录中有一个严重的错误,它破坏了Java7HTTPS URL连接。谁知道……

我正在放弃这种方法,转而使用更快、更安全的Google Mail API。我的Junit测试运行得很好——它们只是不在web容器中运行。
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 = "reciverEmail@gmail.com";//change accordingly

      // Sender's email ID needs to be mentioned
      String from = "testemail@gmail.com";//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);
      }
   }
}