|
5 | 5 |
|
6 | 6 | // snippet-start:[ses.java2.sendmessage.sesv2.main] |
7 | 7 | // snippet-start:[ses.java2.sendmessage.sesv2.import] |
| 8 | + |
8 | 9 | import software.amazon.awssdk.regions.Region; |
9 | 10 | import software.amazon.awssdk.services.sesv2.model.Body; |
10 | 11 | import software.amazon.awssdk.services.sesv2.model.Content; |
|
19 | 20 | /** |
20 | 21 | * Before running this AWS SDK for Java (v2) example, set up your development |
21 | 22 | * environment, including your credentials. |
22 | | - * |
| 23 | + * <p> |
23 | 24 | * For more information, see the following documentation topic: |
24 | | - * |
| 25 | + * <p> |
25 | 26 | * https://docs.aws.amazon.com/sdk-for-java/latest/developer-guide/get-started.html |
26 | 27 | */ |
27 | 28 |
|
28 | 29 | public class SendEmail { |
29 | | - public static void main(String[] args) { |
30 | | - final String usage = """ |
31 | | -
|
32 | | - Usage: |
33 | | - <sender> <recipient> <subject>\s |
34 | | -
|
35 | | - Where: |
36 | | - sender - An email address that represents the sender.\s |
37 | | - recipient - An email address that represents the recipient.\s |
38 | | - subject - The subject line.\s |
39 | | - """; |
40 | | - |
41 | | - if (args.length != 3) { |
42 | | - System.out.println(usage); |
43 | | - System.exit(1); |
44 | | - } |
45 | | - |
46 | | - String sender = args[0]; |
47 | | - String recipient = args[1]; |
48 | | - String subject = args[2]; |
49 | | - |
50 | | - Region region = Region.US_EAST_1; |
51 | | - SesV2Client sesv2Client = SesV2Client.builder() |
52 | | - .region(region) |
53 | | - .build(); |
54 | | - |
55 | | - // The HTML body of the email. |
56 | | - String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>" |
57 | | - + "<p> See the list of customers.</p>" + "</body>" + "</html>"; |
58 | | - |
59 | | - send(sesv2Client, sender, recipient, subject, bodyHTML); |
| 30 | + public static void main(String[] args) { |
| 31 | + final String usage = """ |
| 32 | + |
| 33 | + Usage: |
| 34 | + <sender> <recipient> <subject>\s |
| 35 | + |
| 36 | + Where: |
| 37 | + sender - An email address that represents the sender.\s |
| 38 | + recipient - An email address that represents the recipient.\s |
| 39 | + subject - The subject line.\s |
| 40 | + """; |
| 41 | + |
| 42 | + if (args.length != 3) { |
| 43 | + System.out.println(usage); |
| 44 | + System.exit(1); |
60 | 45 | } |
61 | 46 |
|
62 | | - public static void send(SesV2Client client, |
63 | | - String sender, |
64 | | - String recipient, |
65 | | - String subject, |
66 | | - String bodyHTML) { |
67 | | - |
68 | | - Destination destination = Destination.builder() |
69 | | - .toAddresses(recipient) |
70 | | - .build(); |
71 | | - |
72 | | - Content content = Content.builder() |
73 | | - .data(bodyHTML) |
74 | | - .build(); |
75 | | - |
76 | | - Content sub = Content.builder() |
77 | | - .data(subject) |
78 | | - .build(); |
79 | | - |
80 | | - Body body = Body.builder() |
81 | | - .html(content) |
82 | | - .build(); |
83 | | - |
84 | | - Message msg = Message.builder() |
85 | | - .subject(sub) |
86 | | - .body(body) |
87 | | - .build(); |
88 | | - |
89 | | - EmailContent emailContent = EmailContent.builder() |
90 | | - .simple(msg) |
91 | | - .build(); |
92 | | - |
93 | | - SendEmailRequest emailRequest = SendEmailRequest.builder() |
94 | | - .destination(destination) |
95 | | - .content(emailContent) |
96 | | - .fromEmailAddress(sender) |
97 | | - .build(); |
98 | | - |
99 | | - try { |
100 | | - System.out.println("Attempting to send an email through Amazon SES " |
101 | | - + "using the AWS SDK for Java..."); |
102 | | - client.sendEmail(emailRequest); |
103 | | - System.out.println("email was sent"); |
104 | | - |
105 | | - } catch (SesV2Exception e) { |
106 | | - System.err.println(e.awsErrorDetails().errorMessage()); |
107 | | - System.exit(1); |
108 | | - } |
| 47 | + String sender = args[0]; |
| 48 | + String recipient = args[1]; |
| 49 | + String subject = args[2]; |
| 50 | + |
| 51 | + Region region = Region.US_EAST_1; |
| 52 | + SesV2Client sesv2Client = SesV2Client.builder() |
| 53 | + .region(region) |
| 54 | + .build(); |
| 55 | + |
| 56 | + // The HTML body of the email. |
| 57 | + String bodyHTML = "<html>" + "<head></head>" + "<body>" + "<h1>Hello!</h1>" |
| 58 | + + "<p> See the list of customers.</p>" + "</body>" + "</html>"; |
| 59 | + |
| 60 | + send(sesv2Client, sender, recipient, subject, bodyHTML); |
| 61 | + } |
| 62 | + |
| 63 | + public static void send(SesV2Client client, |
| 64 | + String sender, |
| 65 | + String recipient, |
| 66 | + String subject, |
| 67 | + String bodyHTML) { |
| 68 | + |
| 69 | + Destination destination = Destination.builder() |
| 70 | + .toAddresses(recipient) |
| 71 | + .build(); |
| 72 | + |
| 73 | + Content content = Content.builder() |
| 74 | + .data(bodyHTML) |
| 75 | + .build(); |
| 76 | + |
| 77 | + Content sub = Content.builder() |
| 78 | + .data(subject) |
| 79 | + .build(); |
| 80 | + |
| 81 | + Body body = Body.builder() |
| 82 | + .html(content) |
| 83 | + .build(); |
| 84 | + |
| 85 | + Message msg = Message.builder() |
| 86 | + .subject(sub) |
| 87 | + .body(body) |
| 88 | + .build(); |
| 89 | + |
| 90 | + EmailContent emailContent = EmailContent.builder() |
| 91 | + .simple(msg) |
| 92 | + .build(); |
| 93 | + |
| 94 | + SendEmailRequest emailRequest = SendEmailRequest.builder() |
| 95 | + .destination(destination) |
| 96 | + .content(emailContent) |
| 97 | + .fromEmailAddress(sender) |
| 98 | + .build(); |
| 99 | + |
| 100 | + try { |
| 101 | + System.out.println("Attempting to send an email through Amazon SES " |
| 102 | + + "using the AWS SDK for Java..."); |
| 103 | + client.sendEmail(emailRequest); |
| 104 | + System.out.println("email was sent"); |
| 105 | + |
| 106 | + } catch (SesV2Exception e) { |
| 107 | + System.err.println(e.awsErrorDetails().errorMessage()); |
| 108 | + System.exit(1); |
109 | 109 | } |
| 110 | + } |
110 | 111 | } |
111 | 112 | // snippet-end:[ses.java2.sendmessage.sesv2.main] |
0 commit comments