Created
November 28, 2013 03:17
-
-
Save goyuninfo/7686796 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class AWSeMailService { | |
public static void send(AmazonSimpleEmailService ses, String from, | |
String to, String subject, String body) throws IOException { | |
/* | |
* Before you can send email via Amazon SES, you need to verify that you | |
* own the email address from which you'll be sending email. This will | |
* trigger a verification email, which will contain a link that you can | |
* click on to complete the verification process. | |
*/ | |
verifyEmailAddress(ses, from); | |
// RETERN_PATH is optional | |
SendEmailRequest sendEmailRequest = new SendEmailRequest() | |
.withSource(from).withReturnPath(RETERN_PATH); | |
List toAddresses = new ArrayList<>(); | |
toAddresses.add(to); | |
Destination destination = new Destination() | |
.withToAddresses(toAddresses); | |
sendEmailRequest.setDestination(destination); | |
com.amazonaws.services.simpleemail.model.Message message | |
= new com.amazonaws.services.simpleemail.model.Message(new Content() | |
.withCharset("UTF-8").withData(subject + " i88.ca"), new Body( | |
new Content().withCharset("UTF-8").withData(body))); | |
sendEmailRequest.setMessage(message); | |
ses.sendEmail(sendEmailRequest); | |
} | |
/* | |
* Important: Be sure to fill in an email address you have access to so that | |
* you can receive the initial confirmation email from Amazon Simple Email | |
* Service. | |
*/ | |
private static final String RETERN_PATH = "noreply@i88.ca"; | |
private static final String TO = "nobody@i88.ca"; | |
private static final String FROM = "somebody@i88.ca"; | |
private static final String BODY = "This is an email by java from Amazon Simple Email Service!"; | |
private static final String SUBJECT = "A trying mail from Amazon SES by Java"; | |
public static void main(String[] args) throws IOException { | |
/* | |
* Important: Be sure to fill in your AWS access credentials in the | |
* AwsCredentials.properties file before you try to run this sample. | |
* http://aws.amazon.com/security-credentials | |
*/ | |
PropertiesCredentials credentials = new PropertiesCredentials( | |
AWSeMailService.class | |
.getResourceAsStream("AwsCredentials.properties")); | |
AmazonSimpleEmailService ses = new AmazonSimpleEmailServiceClient( | |
credentials); | |
AWSeMailService.send(ses, FROM, TO, SUBJECT, BODY); | |
} | |
/** | |
* Sends a request to Amazon Simple Email Service to verify the specified | |
* email address. This triggers a verification email, which will contain a | |
* link that you can click on to complete the verification process. | |
* | |
* @param ses | |
* The Amazon Simple Email Service client to use when making | |
* requests to Amazon SES. | |
* @param address | |
* The email address to verify. | |
*/ | |
private static void verifyEmailAddress(AmazonSimpleEmailService ses, | |
String address) { | |
String domain = address.substring(address.indexOf("@") + 1); | |
List lids = ses.listIdentities().getIdentities(); | |
if (lids.contains(address) || lids.contains(domain)) { | |
return; | |
} | |
ses.verifyEmailAddress(new VerifyEmailAddressRequest() | |
.withEmailAddress(address)); | |
System.out.println("Please check the email address " + address | |
+ " to verify it"); | |
System.exit(0); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment