使用Amazon Web Services的SES发送SMS

时间:2015-10-16 07:57:57

标签: amazon-web-services aws-sdk

是否可以使用AWS的SES服务发送短信。我正在尝试使用Java实现此功能。

3 个答案:

答案 0 :(得分:2)

您可以使用AWS的SNS发送短信。您可以通过发送电子邮件将SES端点连接到SNS来执行此操作。

目前,只有美国电话号码可以接收短信。

请参阅http://aws.amazon.com/sns/

上的交付多个协议标题

答案 1 :(得分:1)

您现在可以全局发送短信。见Introducing Worldwide SMS Messaging

答案 2 :(得分:0)

是的,您可以使用AMazon SNS发送批量短信(简单通知服务)

import java.util.HashMap;
import java.util.Map;

import com.amazonaws.services.sns.AmazonSNSClient;
import com.amazonaws.services.sns.model.CreateTopicRequest;
import com.amazonaws.services.sns.model.CreateTopicResult;
import com.amazonaws.services.sns.model.MessageAttributeValue;
import com.amazonaws.services.sns.model.PublishRequest;
import com.amazonaws.services.sns.model.PublishResult;
import com.amazonaws.services.sns.model.SubscribeRequest;
import com.amazonaws.services.sns.model.SubscribeResult;

public class BulkSMSSender {
    static String topicArn;
    static Map<String, MessageAttributeValue> smsAttributes;
    static AmazonSNSClient snsClient = AwsSNSCredentials.getSnsClient();

    public static void main(String[] args) {
        // AmazonSNSClient snsClient = new AmazonSNSClient();

        if (topicArn == null) {
            topicArn = createSNSTopic(snsClient);

        }
        smsAttributes = initializeMessage();
        String message = "*********";
        String phoneNumber[] = { "+91**********" };
        subscribeToTopic(snsClient, topicArn, "sms", phoneNumber[0]);

        sendSMSMessageToTopic(snsClient, topicArn, message, smsAttributes);
    }

    public static String createSNSTopic(AmazonSNSClient snsClient) {
        CreateTopicRequest createTopic = new CreateTopicRequest("Topic");
        CreateTopicResult result = snsClient.createTopic(createTopic);
        System.out.println("Create topic request: " + snsClient.getCachedResponseMetadata(createTopic));
        return result.getTopicArn();
    }

    public static void subscribeToTopic(AmazonSNSClient snsClient, String topicArn, String protocol, String endpoint) {
        SubscribeRequest subscribe = new SubscribeRequest(topicArn, protocol, endpoint);
        SubscribeResult subscribeResult = snsClient.subscribe(subscribe);
        System.out.println("Subscribe request: " + snsClient.getCachedResponseMetadata(subscribe));
    }

    public static void sendSMSMessageToTopic(AmazonSNSClient snsClient, String topicArn, String message,
            Map<String, MessageAttributeValue> smsAttributes) {
        PublishResult result = snsClient.publish(
                new PublishRequest().withTopicArn(topicArn).withMessage(message).withMessageAttributes(smsAttributes));
        System.out.println(result);
    }

    private static Map<String, MessageAttributeValue> initializeMessage() {
        if (smsAttributes == null) {
            smsAttributes = new HashMap<String, MessageAttributeValue>();
            smsAttributes.put("AWS.SNS.SMS.SenderID",
                    new MessageAttributeValue().withStringValue("MSGGGG").withDataType("String"));
            smsAttributes.put("AWS.SNS.SMS.MaxPrice",
                    new MessageAttributeValue().withStringValue("0.05").withDataType("Number"));
            smsAttributes.put("AWS.SNS.SMS.SMSType",
                    new MessageAttributeValue().withStringValue("Transactional").withDataType("String"));
        }
        return smsAttributes;

    }

}

可以将AWS凭证放在此处

import com.amazonaws.auth.BasicAWSCredentials;
import com.amazonaws.regions.Regions;
import com.amazonaws.services.sns.AmazonSNSClient;

import common.PropertiesFileReader;
import constants.StringConstants;

public class AwsSNSCredentials {
    private static final String CLIENT_SMS_TOKEN = "AWS Secret Id";
    private static final String CLIENT_SMS_SECRET = "AWS Secret Key";
    private static final BasicAWSCredentials CREDENTIALS = new BasicAWSCredentials(CLIENT_SMS_TOKEN, CLIENT_SMS_SECRET);

    private static AmazonSNSClient snsClient = null;
    public static AmazonSNSClient getSnsClient() {
        if(null == snsClient) {
        snsClient = new AmazonSNSClient(CREDENTIALS)
                .withRegion(Regions.US_WEST_2);
        }
        return snsClient;
    }
}