Skip to content

Amazon SESのマイグレーション

AWS SDK for Java にて開発したメール送信プログラムに大きな修正を加えることなく、本サービスを利用することができます。

ライブラリ

AWS SDK for Java が提供する AmazonSimpleEmailServiceClient クラスの代わりに、本サービスが提供する CMCAmazonClient を利用してメールを送信することができます。

ライブラリは、以下をダウンロードして使用してください。本ライブラリは、Apache License Version 2.0 で提供します。

サンプルコード

AWS SDK for Javaと CMCAmazonClient クラスを使用したサンプルコードです。

import java.io.IOException;

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 jp.co.hde.mail.ses.CMCAmazonClient;

public class CMCAmazonClientSample {

    // Replace with your "From" address. This address must be verified.
    static final String FROM = "SENDER@EXAMPLE.COM";

    // Replace with a "To" address. If your account is still in the
    // sandbox, this address must be verified.
    static final String TO = "RECIPIENT@EXAMPLE.COM";

    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)";

    public static void main(String[] args) throws IOException {     

        // 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...");

            // 以下のソースコードをCMCAmazonClientに置き換えます。
            // AmazonSimpleEmailServiceClient client = new AmazonSimpleEmailServiceClient();
            // Region REGION = Region.getRegion(Regions.US_WEST_2);
            // client.setRegion(REGION);

            // CMCAmazonClient インスタンスを生成します。
            // 引数には、SMTPホスト名を渡します。
            CMCAmazonClient client = new CMCAmazonClient("SUBDOMAIN.smtps.jp");

            // 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());
        }
    }
}