简体   繁体   中英

How to send contact form data to email address with Angular, using Spring Boot as Backend and the AWS SES service?

I have a website created with Angular with a contact form in 'contact us' and would like to allow users to send messages. The contact data should be sent to my personal email address.

Sending contact form data with HTML and PHP is very straightforward, now using Angular as frontend, Spring Boot as backend and AWS services, in contrast, seems a bit more challenging. Does anyone have an idea where to start besides having an Angular reactive form set up?

Post your Angular form data to a Spring Boot controller. The Angular web site and Spring BOOT backend are decoupled.

Then you can setup your Spring Backend to use the Amazon SES Java V2 API to send email messages. You can use the posted form data to build the email message.

The following Java code is the SES Java V2 code example that sends an email message.

package com.example.ses;

// snippet-start:[ses.java2.sendmessage.request.import]
import software.amazon.awssdk.auth.credentials.ProfileCredentialsProvider;
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]

/**
 * Before running this Java V2 code example, set up your development environment, including your credentials.
 *
 * For more information, see the following 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" +
                "    <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)
                .credentialsProvider(ProfileCredentialsProvider.create())
                .build();

        // 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, 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 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]
}

If you are not familiar with the AWS SDK for Java V2, see https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html .

You can find the SES POM file here: https://github.com/awsdocs/aws-doc-sdk-examples/tree/main/javav2/example_code/ses

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM