-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAmazonSES.java
388 lines (325 loc) · 16.4 KB
/
AmazonSES.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
package hello;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.faces.context.FacesContext;
import com.amazonaws.AmazonClientException;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSCredentials;
import com.amazonaws.auth.DefaultAWSCredentialsProviderChain;
import com.amazonaws.regions.Region;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.dynamodbv2.model.AttributeValue;
import com.amazonaws.services.dynamodbv2.model.ComparisonOperator;
import com.amazonaws.services.dynamodbv2.model.Condition;
import com.amazonaws.services.dynamodbv2.model.ScanRequest;
import com.amazonaws.services.dynamodbv2.model.ScanResult;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClient;
import com.amazonaws.services.simpleemail.model.Body;
import com.amazonaws.services.simpleemail.model.Content;
import com.amazonaws.services.simpleemail.model.Destination;
import com.amazonaws.services.simpleemail.model.Message;
import com.amazonaws.services.simpleemail.model.SendEmailRequest;
import com.amazonaws.services.sqs.AmazonSQS;
import com.amazonaws.services.sqs.AmazonSQSClient;
import com.amazonaws.services.sqs.model.GetQueueUrlResult;
import com.amazonaws.services.sqs.model.ReceiveMessageRequest;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.core.JsonGenerationException;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.JsonMappingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class AmazonSES {
public static void sendCompletionEmail(String TO, String MCName, String Link) {
if(TO != ""){
final String FROM = "[email protected]"; // Replace with
// your "From"
// address. This
// address must
// be verified.
// Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
final String BODY = "This is an automated Email to notify you that the request for a Motion Capture recording "
+ MCName
+ " has been completed. You may find it in the database,searching by the name you specified or in your account tab.";
final String SUBJECT = "Your request has been completed!";
// Construct an object to contain the recipient address.
Destination destination = new Destination().withToAddresses(new String[] { TO });
// Create the subject and body of the message.
Content subject = new Content().withData(SUBJECT);
Content textBody = new Content().withData(BODY);
Body body = new Body().withText(textBody);
// Create a message with the specified subject and body.
Message message = new Message().withSubject(subject).withBody(body);
// Assemble the email.
SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination)
.withMessage(message);
try {
System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");
// Instantiate an Amazon SES client, which will make the service
// call. The service call requires your AWS credentials.
// Because we're not providing an argument when instantiating the
// client, the SDK will attempt to find your AWS credentials
// using the default credential provider chain. The first place the
// chain looks for the credentials is in environment variables
// AWS_ACCESS_KEY_ID and AWS_SECRET_KEY.
// For more information, see
// http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient();
Region REGION = Region.getRegion(Regions.EU_WEST_1);
client.setRegion(REGION);
// Send the email.
client.sendEmail(request);
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
System.out.println("Error message: " + ex.getMessage());
}}
}
public static void sendConfirmationEmail(String name, String TO, String bs) {
if(TO != ""){
final String FROM = "[email protected]"; // Replace with
// your "From"
// address. This
// address must
// be verified.
// Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
final String BODY = "Your account has been created. Please activate it by following the link below:\n"
+ "http://mcdatabase.cgrepository.co.uk/activate.xhtml?key=" + bs + "&&name=" + name;
final String SUBJECT = "Verification required";
// Construct an object to contain the recipient address.
Destination destination = new Destination().withToAddresses(new String[] { TO });
// Create the subject and body of the message.
Content subject = new Content().withData(SUBJECT);
Content textBody = new Content().withData(BODY);
Body body = new Body().withText(textBody);
// Create a message with the specified subject and body.
Message message = new Message().withSubject(subject).withBody(body);
// Assemble the email.
SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination)
.withMessage(message);
try {
System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");
// Instantiate an Amazon SES client, which will make the service
// call. The service call requires your AWS credentials.
// Because we're not providing an argument when instantiating the
// client, the SDK will attempt to find your AWS credentials
// using the default credential provider chain. The first place the
// chain looks for the credentials is in environment variables
// AWS_ACCESS_KEY_ID and AWS_SECRET_KEY.
// For more information, see
// http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient();
Region REGION = Region.getRegion(Regions.EU_WEST_1);
client.setRegion(REGION);
// Send the email.
client.sendEmail(request);
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
System.out.println("Error message: " + ex.getMessage());
}
}
}
public static void sendUpdateEmail(String name, String TO, String bs) {
if(TO != ""){
final String FROM = "[email protected]"; // Replace with
// your "From"
// address. This
// address must
// be verified.
// Replace with a "To" address. If your account is still in the
// sandbox, this address must be verified.
final String BODY = "This email is to confirm your new email address. Please activate it by following the link below:\n"
+ "http://mcdatabase.cgrepository.co.uk/emailchange.xhtml?key=" + bs + "&&name=" + name;
final String SUBJECT = "Email update request";
// Construct an object to contain the recipient address.
Destination destination = new Destination().withToAddresses(new String[] { TO });
// Create the subject and body of the message.
Content subject = new Content().withData(SUBJECT);
Content textBody = new Content().withData(BODY);
Body body = new Body().withText(textBody);
// Create a message with the specified subject and body.
Message message = new Message().withSubject(subject).withBody(body);
// Assemble the email.
SendEmailRequest request = new SendEmailRequest().withSource(FROM).withDestination(destination)
.withMessage(message);
try {
System.out.println("Attempting to send an email through Amazon SES by using the AWS SDK for Java...");
// Instantiate an Amazon SES client, which will make the service
// call. The service call requires your AWS credentials.
// Because we're not providing an argument when instantiating the
// client, the SDK will attempt to find your AWS credentials
// using the default credential provider chain. The first place the
// chain looks for the credentials is in environment variables
// AWS_ACCESS_KEY_ID and AWS_SECRET_KEY.
// For more information, see
// http://docs.aws.amazon.com/AWSSdkDocsJava/latest/DeveloperGuide/credentials.html
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient();
Region REGION = Region.getRegion(Regions.EU_WEST_1);
client.setRegion(REGION);
// Send the email.
client.sendEmail(request);
System.out.println("Email sent!");
} catch (Exception ex) {
System.out.println("The email was not sent.");
System.out.println("Error message: " + ex.getMessage());
}
}
}
public static void BounceHandleRequest() {
AWSCredentials credentials = null;
try {
credentials = new DefaultAWSCredentialsProviderChain().getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. " +
"Please make sure that your credentials file is at the correct " +
"location (C:\\Users\\nas\\.aws\\credentials), and is in valid format.",
e);
}
AmazonSQS sqs = new AmazonSQSClient(credentials);
Region eu = Region.getRegion(Regions.EU_WEST_1);
sqs.setRegion(eu);
System.out.println("===========================================");
System.out.println("Getting Started with Amazon SQS");
System.out.println("===========================================\n");
try {
// Create a queue
// List queues
System.out.println("Listing all queues in your account.\n");
for (String queueUrl : sqs.listQueues().getQueueUrls()) {
System.out.println(" QueueUrl: " + queueUrl);
}
System.out.println();
GetQueueUrlResult QueueUrl = sqs.getQueueUrl("ses-bounces-queue");
// Send a message
System.out.println("Receiving messages from MyQueue.\n");
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(QueueUrl.getQueueUrl());
List<com.amazonaws.services.sqs.model.Message> messages = sqs.receiveMessage(receiveMessageRequest)
.getMessages();
for (com.amazonaws.services.sqs.model.Message message : messages) {
System.out.println(" Body: " + message.getBody());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
AmazonSesBounceNotification obj = mapper.readValue(message.getBody(), AmazonSesBounceNotification.class);
System.out.println(obj.getMessage().getBounce().getBouncedRecipients().get(0).getEmailAddress());
System.out.println(obj.getMessage().getBounce().getBounceSubType());
List<AmazonSesBouncedRecipient> recipient = obj.Message.getBounce().getBouncedRecipients();
switch (obj.getMessage().getBounce().getBounceType()) {
case "Transient":
// Per our sample organizational policy, we will remove all
// recipients
// that generate an AttachmentRejected bounce from our
// mailing list.
// Other bounces will be reviewed manually.
switch (obj.getMessage().getBounce().getBounceSubType()) {
case "AttachmentRejected":
for (AmazonSesBouncedRecipient rec : recipient) {
DbConnect.RemoveFromMailingList(rec.getEmailAddress());
}
break;
default:
System.out.println("manual");
break;
}
break;
default:
// Remove all recipients that generated a permanent bounce
// or an unknown bounce.
for (AmazonSesBouncedRecipient rec : recipient) {
System.out.println("Permanent,removing...");
DbConnect.RemoveFromMailingList(rec.getEmailAddress());
}
break;
}
}
System.out.println();
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it "
+ "to Amazon SQS, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with SQS, such as not "
+ "being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static void ComplaintHandleRequest()
{
AWSCredentials credentials = null;
try {
credentials = new DefaultAWSCredentialsProviderChain().getCredentials();
} catch (Exception e) {
throw new AmazonClientException(
"Cannot load the credentials from the credential profiles file. " +
"Please make sure that your credentials file is at the correct " +
"location (C:\\Users\\nas\\.aws\\credentials), and is in valid format.",
e);
}
AmazonSQS sqs = new AmazonSQSClient(credentials);
Region eu = Region.getRegion(Regions.EU_WEST_1);
sqs.setRegion(eu);
try {
GetQueueUrlResult ComplaintUrl = sqs.getQueueUrl("ses-complaints-queue");
System.out.println("Receiving messages from MyQueue.\n");
ReceiveMessageRequest receiveMessageRequest = new ReceiveMessageRequest(ComplaintUrl.getQueueUrl());
List<com.amazonaws.services.sqs.model.Message> messages = sqs.receiveMessage(receiveMessageRequest).getMessages();
for (com.amazonaws.services.sqs.model.Message message : messages) {
System.out.println(" Body: " + message.getBody());
ObjectMapper mapper = new ObjectMapper();
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
AmazonSesComplaintNotification obj = mapper.readValue(message.getBody(), AmazonSesComplaintNotification.class);
System.out.println(obj.getMessage().getNotificationType());
System.out.println(obj.getMessage().getComplaint().getComplainedRecipients());
List<AmazonSesComplainedRecipient> recipient = obj.getMessage().getComplaint().getComplainedRecipients();
for (AmazonSesComplainedRecipient rec : recipient)
{
System.out.println("Email" + rec.getEmailAddress());
DbConnect.RemoveFromMailingList(rec.getEmailAddress());
}
}
System.out.println();
} catch (AmazonServiceException ase) {
System.out.println("Caught an AmazonServiceException, which means your request made it " +
"to Amazon SQS, but was rejected with an error response for some reason.");
System.out.println("Error Message: " + ase.getMessage());
System.out.println("HTTP Status Code: " + ase.getStatusCode());
System.out.println("AWS Error Code: " + ase.getErrorCode());
System.out.println("Error Type: " + ase.getErrorType());
System.out.println("Request ID: " + ase.getRequestId());
} catch (AmazonClientException ace) {
System.out.println("Caught an AmazonClientException, which means the client encountered " +
"a serious internal problem while trying to communicate with SQS, such as not " +
"being able to access the network.");
System.out.println("Error Message: " + ace.getMessage());
} catch (JsonParseException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (JsonMappingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}