国外Twilio 发送sms短信

private static final String userName = "Account SID";  
private static final String password = "Auth Token"; 
private String fromPhone; //你的手机号(平台购买的手机号)


/**
     * 发送短信
     * */
    public static Map sms(String toPhone) {
        Map mapTypes =  new HashMap();
        try {
            // 生成安全的HS512密钥
            SecretKey secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS512);
            // 初始化Twilio客户端
            Twilio.init(userName, password);
            // 发送短信
            Message message = Message.creator(
                new PhoneNumber(toPhone), //对方手机号
                new PhoneNumber("+14158141829"),//fromPhone
                "短信内容"
            ).create();
            // 打印短信的SID
            mapTypes =  JSON.parseObject(String.valueOf(message));
            System.out.println(JSON.toJSONString(mapTypes));
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapTypes;
    }

    public static void main(String[] args) {
        Map sms = sms("+14159352345");
    }

//发送邮件

 /**
     * 发送邮件
     * */
    public static Map sendEmail(String toPhone) {
        Map mapTypes =  new HashMap();
        String apiKey = "后台的appkey,详细查看appKey的设置";

        try {
            SendGrid sg = new SendGrid(apiKey);
            Request request = new Request();
            request.setMethod(Method.POST);
            request.setEndpoint("/mail/send");//格式不能改

            // Create mail
            Mail mail = new Mail();
            Personalization personalization1 = new Personalization();
            personalization1.addTo(new Email("收件人邮件地址", "收件人名称"));
            personalization1.addCustomArg("version", "1.0");//注意:自定义参数,会在回调的时候返回
            mail.addPersonalization(personalization1);
            mail.setFrom(new Email("发件人邮箱", "发件人名称"));
                mail.setSubject("Your Example Order Confirmation");
            //内容对象
            Content content = new Content();
            content.setType("text/html");
            content.setValue("<p>Hello from Twilio SendGrid!</p><p>Sending with the email service trusted by developers and marketers for <strong>time-savings</strong>, <strong>scalability</strong>, and <strong>delivery expertise</strong>.</p><p>%open-track%</p>");
            mail.addContent(content);
            request.setBody(mail.build());
            //发送邮件
            Response response = sg.api(request);
            System.out.println(response.getStatusCode());
            System.out.println(response.getBody());
            System.out.println(response.getHeaders());
            mapTypes = JSON.parseObject(response.getBody(),Map.class);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return mapTypes;
    }

注意:短信的pom:  

<dependency>
     <groupId>com.twilio.sdk</groupId>
     <artifactId>twilio</artifactId>
     <version>7.55.0</version>
 </dependency>

邮件的我是在官网下载的jar包打到项目里的

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值