简体   繁体   English

我一直在尝试在 AWS Lambda 中使用 smtp 协议实现电子邮件的安全传输,但它不起作用

[英]I have been trying to implement a secure transmission of emailing using smtp protocol in AWS Lambda and it doesn't work

It's a Spring Boot Java Lambda for secure smtp mail transmission and it does not work while it does work in another api code of Spring Boot.这是一个 Spring Boot Java Lambda 用于安全的 smtp 邮件传输,它在另一个 Spring Boot 的 api 代码中工作时不起作用。 Whenever I run this code it gives either SecurityException on one occassion or AuthenticationException on another and when I use getDefaultInstance() method of Session instead of getInstance() it gives "cannot create a default session".每当我运行此代码时,它都会在一个场合给出 SecurityException,在另一个场合给出 AuthenticationException,当我使用 Session 的 getDefaultInstance() 方法而不是 getInstance() 时,它给出“无法创建默认会话”。 It does not even creates an object for Authenticator and gives Authentication Exception on Lambda.它甚至不为 Authenticator 创建 object,并在 Lambda 上给出身份验证异常。

---code below-- ---代码如下--

import java.util.Properties;

import javax.mail.PasswordAuthentication;
import javax.mail.Session;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;

@Configuration
public class EmailConfig {
    
    private String username = "";
    private String password = "";
    @Bean
    public Session getEmailSession(Environment env) {
        Properties props = System.getProperties();
        props.setProperty("mail.smtp.host", "smtp.gmail.com");
        props.setProperty("mail.smtp.port", "587");
        props.setProperty("mail.smtp.auth", "true");
        props.setProperty("mail.smtp.starttls.enable", "true");
        
        Session session = Session.getInstance(props,
                new javax.mail.Authenticator() {
                    protected PasswordAuthentication getPasswordAuthentication() {
                        return new PasswordAuthentication(username, password); // username & password coming from vault
                    }
                });
        return session;
    }
}

It's Throwing error in Lambda: 2022-02-27T04:49:42.216+05:30它在 Lambda 中抛出错误:2022-02-27T04:49:42.216+05:30

Copy software/amazon/awssdk/regions/Region: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: software/amazon/awssdk/regions/Region at aws.smtp.lambda.demo.SendMessageEmailRequest.main_meth(SendMessageEmailRequest.java:47) at aws.smtp.lambda.demo.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:42) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) Caused by: java.lang.ClassNotFoundException: software.amazon.awssdk.regions.Region. Copy software/amazon/awssdk/regions/Region: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: software/amazon/awssdk/regions/Region at aws.smtp.lambda.demo.SendMessageEmailRequest.main_meth(SendMessageEmailRequest.java:47) at aws .smtp.lambda.demo.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:42) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source ) 在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在 java.base/java.lang.reflect.Method.invoke(未知来源) 引起:java.lang.ClassNotFoundException:software.amazon。 awssdk.regions.区域。 Current classpath: file:/var/task/当前类路径:file:/var/task/

software/amazon/awssdk/regions/Region: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: software/amazon/awssdk/regions/Region at aws.smtp.lambda.demo.SendMessageEmailRequest.main_meth(SendMessageEmailRequest.java:47) at aws.smtp.lambda.demo.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:42) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) Caused by: java.lang.ClassNotFoundException: software.amazon.awssdk.regions.Region. software/amazon/awssdk/regions/Region: java.lang.NoClassDefFoundError java.lang.NoClassDefFoundError: software/amazon/awssdk/regions/Region at aws.smtp.lambda.demo.SendMessageEmailRequest.main_meth(SendMessageEmailRequest.java:47) at aws. smtp.lambda.demo.LambdaFunctionHandler.handleRequest(LambdaFunctionHandler.java:42) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)在 java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(未知来源) 在 java.base/java.lang.reflect.Method.invoke(未知来源) Caused by: java.lang.ClassNotFoundException: software.amazon.awssdk .regions.地区。 Current classpath: file:/var/task/当前类路径:file:/var/task/

Code below:代码如下:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.6.4</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
    <groupId>aws.smtp.lambda</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot Lambda smtp</description>
    <properties>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>software.amazon.awssdk</groupId>
            <artifactId>bundle</artifactId>
            <version>2.9.3</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>

    <!--    <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>-->
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-core</artifactId>
            <version>1.0.0</version>
        </dependency>
        <!-- <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-lambda-java-tests</artifactId>
            <version>1.0.0</version>
            <scope>test</scope>
        </dependency>-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-mail</artifactId>
        </dependency>
        <!-- https://mvnrepository.com/artifact/com.amazonaws/aws-java-sdk-ses -->
        <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-ses</artifactId> 
            <version>1.9.16</version> </dependency>
        <dependency>
            <groupId>com.amazonaws</groupId>
            <artifactId>aws-java-sdk-ses</artifactId>
            <version>1.11.561</version>
        </dependency>
        <!-- Thanks for using https://jar-download.com -->

    </dependencies>

    <!-- <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-shade-plugin</artifactId>
                <configuration>
                    <createDependencyReducedPom>false</createDependencyReducedPom>
                </configuration>
                <executions>
                    <execution>
                        <phase>package</phase>
                        <goals>
                            <goal>shade</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build> -->

</project>
package aws.smtp.lambda.demo;

import org.springframework.boot.autoconfigure.SpringBootApplication;

import com.amazonaws.services.lambda.runtime.Context;
//import com.amazonaws.services.lambda.runtime.RequestHandler;
//import com.amazonaws.services.lambda.runtime.events.ScheduledEvent;

@SpringBootApplication
public class LambdaFunctionHandler /*implements RequestHandler<ScheduledEvent, String>*/ {

    //private Context applicationContext;
//  @Autowired
    private SendMessageEmailRequest reqSES = new SendMessageEmailRequest();
    
    /*@Autowired
    private AmazonSESSample sesSample = new AmazonSESSample();*/
    /*@Autowired
    private SimpleEmail simpleEmail = new SimpleEmail();
    */
    public LambdaFunctionHandler() {
        
    }
/*  public LambdaFunctionHandler(Context context) {
        applicationContext = context;
    }*/

    /*public void initialize() {
        applicationContext = new SpringApplicationBuilder(LambdaFunctionHandler.class).web(WebApplicationType.NONE)
                .run();
    }*/

    public String handleRequest(/*ScheduledEvent input,*/ Context context) throws Exception {
    /*  try {
            if(Objects.isNull(applicationContext)) {
                initialize();
            }*/
            context.getLogger().log("Input: " );
//          simpleEmail.sendMail();
//          sesSample.sendMail();
            System.out.println("Going Inside SES Class ");
            reqSES.main_meth();
            return "Hello World - " ;//+ input;
        /*}
        catch(Exception ex) {
            ex.printStackTrace();
            return "Failure";
        }*/
    }
    
    public static void main(String args[]) {
        
    }
}
package aws.smtp.lambda.demo;

import javax.mail.MessagingException;

//snippet-end:[ses.java2.sendmessage.request.import]
import org.springframework.stereotype.Service;

//snippet-start:[ses.java2.sendmessage.request.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.Body;
import software.amazon.awssdk.services.ses.model.Content;
import software.amazon.awssdk.services.ses.model.Destination;
import software.amazon.awssdk.services.ses.model.Message;
import software.amazon.awssdk.services.ses.model.SendEmailRequest;
//import software.amazon.awssdk.services.ses.model.SesException;

/**
* To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
*
* For information, see this documentation topic:
*
* https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
*/
@Service
public class SendMessageEmailRequest {

 public void main_meth(/*String[] args*/) {
     System.out.println(" Inside SES Class : main_meth");
     final String USAGE = "\n" +
             "Usage:\n" +
             "    SendMessage <sender> <recipient> <subject> \n\n" +
             "Where:\n" +
             "    sender - an email address that represents the sender. \n"+
             "    recipient -  an email address that represents the recipient. \n"+
             "    subject - the  subject line. \n" ;
     System.out.println("Inside SES Class ");
      /* if (args.length != 3) {
         System.out.println(USAGE);
          System.exit(1);
        }*/

     String sender = ""; // written correctly in the original code 
     String recipient = ""; // written correctly in the original code 
     String subject = "Amazon SES test (AWS SDK for Java)";

     Region region = Region.AP_SOUTH_1; // failing in this line
     /*SesClient client = SesClient.builder()
             .region(region)
             .build();*/
     
     // The email body for non-HTML email clients
     String bodyText = "Hello,\r\n" + "See the list of customers. ";

     // The HTML body of the email
     String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>"
             + "<p> See the list of customers.</p>" + "</body>" + "</html>";

     try {
         System.out.println(" Inside SES Class : main_meth : in try block");
        // send(client, sender, recipient, subject, bodyText, bodyHTML);
         //client.close();
         System.out.println("Done");

     } catch (/*Messaging*/Exception e) {
         e.getStackTrace();
     }
 }

 // snippet-start:[ses.java2.sendmessage.request.main]
 /*public void send(SesClient client,
                         String sender,
                         String recipient,
                         String subject,
                         String bodyText,
                         String bodyHTML
 ) throws MessagingException {

     Destination destination = Destination.builder()
             .toAddresses(recipient)
             .build();

     Content content = Content.builder()
             .data(bodyHTML)
             .build();

     Content sub = Content.builder()
             .data(subject)
             .build();

     Body body = Body.builder()
             .html(content)
             .build();

     Message msg = Message.builder()
             .subject(sub)
             .body(body)
             .build();

     SendEmailRequest emailRequest = SendEmailRequest.builder()
             .destination(destination)
             .message(msg)
             .source(sender)
             .build();

     try {
         System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
         client.sendEmail(emailRequest);

     } catch (SesException e) {
         System.err.println(e.awsErrorDetails().errorMessage());
         System.exit(1);
     }
     // snippet-end:[ses.java2.sendmessage.request.main]
 }*/
}

Look at using the Amazon Simple Email Service if you need email functionality from either an AWS Lambda function or even a Spring Boot app.如果您需要来自 AWS Lambda function 甚至 Spring 启动应用程序的 email 功能,请考虑使用 Amazon Simple Email 服务。 There are no issues when doing so.这样做没有问题。 You can easily send an email message using this SES Java Code:您可以使用此 SES Java 代码轻松发送 email 消息:

// snippet-start:[ses.java2.sendmessage.request.import]
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.ses.SesClient;
import software.amazon.awssdk.services.ses.model.*;
import software.amazon.awssdk.services.ses.model.Message;
import software.amazon.awssdk.services.ses.model.Body;
import javax.mail.MessagingException;
// snippet-end:[ses.java2.sendmessage.request.import]

/**
 * To run this Java V2 code example, ensure that you have setup your development environment, including your credentials.
 *
 * For information, see this documentation topic:
 *
 * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html
 */
public class SendMessageEmailRequest {

    public static void main(String[] args) {

        final String USAGE = "\n" +
                "Usage:\n" +
                "    SendMessage <sender> <recipient> <subject> \n\n" +
                "Where:\n" +
                "    sender - an email address that represents the sender. \n"+
                "    recipient -  an email address that represents the recipient. \n"+
                "    subject - the  subject line. \n" ;

          if (args.length != 3) {
            System.out.println(USAGE);
             System.exit(1);
           }

        String sender = args[0];
        String recipient = args[1];
        String subject = args[2];

        Region region = Region.US_EAST_1;
        SesClient client = SesClient.builder()
                .region(region)
                .build();

        // The email body for non-HTML email clients
        String bodyText = "Hello,\r\n" + "See the list of customers. ";

        // The HTML body of the email
        String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>"
                + "<p> See the list of customers.</p>" + "</body>" + "</html>";

        try {
            send(client, sender, recipient, subject, bodyText, bodyHTML);
            client.close();
            System.out.println("Done");

        } catch (MessagingException e) {
            e.getStackTrace();
        }
    }

    // snippet-start:[ses.java2.sendmessage.request.main]
    public static void send(SesClient client,
                            String sender,
                            String recipient,
                            String subject,
                            String bodyText,
                            String bodyHTML
    ) throws MessagingException {

        Destination destination = Destination.builder()
                .toAddresses(recipient)
                .build();

        Content content = Content.builder()
                .data(bodyHTML)
                .build();

        Content sub = Content.builder()
                .data(subject)
                .build();

        Body body = Body.builder()
                .html(content)
                .build();

        Message msg = Message.builder()
                .subject(sub)
                .body(body)
                .build();

        SendEmailRequest emailRequest = SendEmailRequest.builder()
                .destination(destination)
                .message(msg)
                .source(sender)
                .build();

        try {
            System.out.println("Attempting to send an email through Amazon SES " + "using the AWS SDK for Java...");
            client.sendEmail(emailRequest);

        } catch (SesException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
            System.exit(1);
        }
        // snippet-end:[ses.java2.sendmessage.request.main]
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Typeorm 不适用于无服务器框架 AWS Lambda - Typeorm doesn't work with Serverless Framework AWS Lambda AWS Lambda 通过 AWS-PHP-SDK 发送短信不起作用 - AWS Lambda send SMS via AWS-PHP-SDK doesn't work AWS lambda 不返回响应 - AWS lambda doesn't return the response 在使用 Node 的 AWS Lambda 中,无法让 Express 与 aws-serverless-express 一起使用 - In AWS Lambda using Node, can't get Express to work with aws-serverless-express AWS IAM Policy Assistance with Lambda Permissions for SMTP - AWS IAM Policy Assistance with Lambda Permissions for SMTP 使用 AWS 保护无服务器后端 Lambda - Secure a serverless backend with AWS Lambda AWS Lambda - 上传的依赖项不加载依赖项的依赖项 - AWS Lambda - Uploaded dependency doesn't load the dependency's dependency 我正在尝试使用 Firebase 在 Flutter 中使用 Google 注销,但它不起作用 - I'm trying to signOut with google in Flutter with Firebase and it doesn't work 如何使用 AWS CLI 创建 AWS Lambda 函数? - How can I create an AWS Lambda function using the AWS CLI? 在 lambda 函数的 IAM 策略条件下使用 aws:ResourceTag 不起作用 - Using aws:ResourceTag in conditions on an IAM policy for lambda functions does not work
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM