首先取得AWS sdk。 位置:
http://docs.aws.amazon.com/ses/latest/DeveloperGuide/download-aws-sdk.html
接著注意的是需要在你的java project匯入aws-java-sdk-1.7.5.jar跟 3rd party的lib。需要的全部都在AWS sdk的3rd-party的資料夾裡面。
注意:要是原本的project就有用到跟aws sdk裡面相同的jar(例如apache)的話,請跟sdk裡面附的比較版本號碼,留下比較新的。 新舊版混放在一起有可能會導致編譯錯誤。
準備文字檔。檔名:AwsCredentials.properties,放在java project的根目錄。
內容如下:
#這是註解
accessKey=[跟aws註冊後獲得的access key]
secretKey=[跟aws註冊後獲得的secret key]
然後是sample程式碼:
import java.io.IOException;
import com.amazonaws.services.simpleemail.*;
import com.amazonaws.services.simpleemail.model.*;
import com.amazonaws.auth.PropertiesCredentials;
import com.amazonaws.regions.*;
public class AmazonSESSample {
static final String FROM = "SENDER@EXAMPLE.COM"; // Replace with your "From" address. This address must be verified.
static final String TO = "RECIPIENT@EXAMPLE.COM"; // Replace with a "To" address. If you have not yet requested
// production access, this address must be verified.
static final String BODY = "This email was sent through Amazon SES by using the AWS SDK for Java.";
static final String SUBJECT = "Amazon SES test (AWS SDK for Java)";
/*
* 利用AWS的的寄信功能,必須執行寄出信箱認證。應該是為了防止利用垃圾信程式亂寄。
* 動作流程:call這個動作,會在strEmailAdd的指定的email位置收到認證信。在認證信裡面會有認證連結。
* 點選認證連結,就會執行認證。成功之後,FROM這個參數就可以使用這個strEmailAdd所指定的email位置。
* 只要執行一次即可。
*/
public static void VerifyEmailViaAWS(String strEmailAdd)
{
File file = new File("AwsCredentials.properties");
PropertiesCredentials credentials;
try {
credentials = new PropertiesCredentials(file);
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
VerifyEmailAddressRequest verifyEmailAddressRequest = new VerifyEmailAddressRequest();
verifyEmailAddressRequest.withEmailAddress(strEmailAdd);
client.verifyEmailAddress(verifyEmailAddressRequest);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
public static void main(String[] args) throws IOException
{
// Your AWS credentials are stored in the AwsCredentials.properties file within the project.
// You entered these AWS credentials when you created a new AWS Java project in Eclipse.
//File file = new File("AwsCredentials.properties");
//PropertiesCredentials credentials = new PropertiesCredentials(file); //原本Amazon所提供的sourcecode裡面用的方法是如下面寫的「AmazonSESSample.class.getResourceAsStream("AwsCredentials.properties");」。若是發現amazon的方法抓不到AwsCredentials.properties檔案的話,可以用註解裡面寫的這個方法。
PropertiesCredentials credentials = new PropertiesCredentials(
AmazonSESSample.class
.getResourceAsStream("AwsCredentials.properties"));
// Retrieve the AWS Access Key ID and Secret Key from AwsCredentials.properties.
credentials.getAWSAccessKeyId();
credentials.getAWSSecretKey();
// 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 with the supplied AWS credentials.
AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient(credentials);
// Choose the AWS region of the Amazon SES endpoint you want to connect to. Note that your production
// access status, sending limits, and Amazon SES identity-related settings are specific to a given
// AWS region, so be sure to select an AWS region in which you set up Amazon SES. Here, we are using
// the US East (N. Virginia) region. Examples of other regions that Amazon SES supports are US_WEST_2
// and EU_WEST_1. For a complete list, see http://docs.aws.amazon.com/ses/latest/DeveloperGuide/regions.html
Region REGION = Region.getRegion(Regions.US_EAST_1); //指定使用amazon的伺服器的位置。
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());
}
}
}
0 件のコメント:
コメントを投稿