Tuesday, October 24, 2017

AWS SES - Amazon Send Email Service

COMMANDS
javac -cp *; MailTest.java
java -cp *;  -Dlog4j.configurationFile=log4j2-console.xml MailTest

DIR
aws-java-sdk-core-1.11.119.jar
aws-java-sdk-ses-1.11.119.jar
httpclient-4.5.3.jar
httpcore-4.4.6.jar
jackson-annotations-2.7.0.jar
jackson-core-2.7.1.jar
jackson-databind-2.7.1.jar
jcl-over-slf4j-1.7.7.jar
joda-time-2.8.1.jar
log4j-api-2.1.jar
log4j-core-2.1.jar
log4j-slf4j-impl-2.1.jar
log4j2-console.xml
MailTest.class
MailTest.java

slf4j-api-1.7.7.jar




MailTest.java
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.amazonaws.AmazonServiceException;
import com.amazonaws.auth.AWSStaticCredentialsProvider;
import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailService;
import com.amazonaws.services.simpleemail.AmazonSimpleEmailServiceClientBuilder;
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;


public class MailTest {

private static final Logger LOGGER = LoggerFactory.getLogger(MailTest.class);

public static void main(String[] args) {

System.out.println("=start=");

String awsAccessKey="change_it";
String awsSecretKey="change_it";
String awsSesRegion="us-west-2";

BasicAWSCredentials credentials = new BasicAWSCredentials(awsAccessKey, awsSecretKey);

AmazonSimpleEmailService amazonSesClient = AmazonSimpleEmailServiceClientBuilder.standard()
.withCredentials(new AWSStaticCredentialsProvider(credentials))
.withRegion(awsSesRegion)
.build();


Content subjectContent = new Content("subject");
Content bodyContent = new Content("body email content");

Body mailBody = new Body().withText(bodyContent);

Message message = new Message().withSubject(subjectContent).withBody(mailBody);

Destination destination = new Destination().withToAddresses("alexey.zavizionov@gmail.com");
SendEmailRequest request = new SendEmailRequest()
.withSource("alexey.zavizionov@gmail.com")
.withDestination(destination)
.withMessage(message);

System.out.println("===============");
System.out.println(request);
System.out.println("---------------");

try {
amazonSesClient.sendEmail(request);
LOGGER.debug("Email with subject '{}' was sent to: {}", "subject", "alexey.zavizionov@gmail.com");
return;
} catch (AmazonServiceException a) {
LOGGER.error("Maximum sending rate exceeded when sending email: {}", message, a);
return;
} catch (Exception e) {
LOGGER.error("Failed to send email: {}", message, e);
return;
} finally {
System.out.println("=THE END=");
}

}

}


log4j2-console.xml
code
   
       
           
                %level{length=1} %date{MMdd-HHmm:ss,SSS} %logger{1.} %message [%thread]%n
           
       
   
   
       
           
       
   

No comments: