• 国外Twilio 发送sms短信


    1. private static final String userName = "Account SID";
    2. private static final String password = "Auth Token";
    3. private String fromPhone; //你的手机号(平台购买的手机号)
    4. /**
    5. * 发送短信
    6. * */
    7. public static Map sms(String toPhone) {
    8. Map mapTypes = new HashMap();
    9. try {
    10. // 生成安全的HS512密钥
    11. SecretKey secretKey = Keys.secretKeyFor(SignatureAlgorithm.HS512);
    12. // 初始化Twilio客户端
    13. Twilio.init(userName, password);
    14. // 发送短信
    15. Message message = Message.creator(
    16. new PhoneNumber(toPhone), //对方手机号
    17. new PhoneNumber("+14158141829"),//fromPhone
    18. "短信内容"
    19. ).create();
    20. // 打印短信的SID
    21. mapTypes = JSON.parseObject(String.valueOf(message));
    22. System.out.println(JSON.toJSONString(mapTypes));
    23. } catch (Exception e) {
    24. e.printStackTrace();
    25. }
    26. return mapTypes;
    27. }
    28. public static void main(String[] args) {
    29. Map sms = sms("+14159352345");
    30. }

    //发送邮件

    1. /**
    2. * 发送邮件
    3. * */
    4. public static Map sendEmail(String toPhone) {
    5. Map mapTypes = new HashMap();
    6. String apiKey = "后台的appkey,详细查看appKey的设置";
    7. try {
    8. SendGrid sg = new SendGrid(apiKey);
    9. Request request = new Request();
    10. request.setMethod(Method.POST);
    11. request.setEndpoint("/mail/send");//格式不能改
    12. // Create mail
    13. Mail mail = new Mail();
    14. Personalization personalization1 = new Personalization();
    15. personalization1.addTo(new Email("收件人邮件地址", "收件人名称"));
    16. personalization1.addCustomArg("version", "1.0");//注意:自定义参数,会在回调的时候返回
    17. mail.addPersonalization(personalization1);
    18. mail.setFrom(new Email("发件人邮箱", "发件人名称"));
    19. mail.setSubject("Your Example Order Confirmation");
    20. //内容对象
    21. Content content = new Content();
    22. content.setType("text/html");
    23. content.setValue("

      Hello from Twilio SendGrid!

      Sending with the email service trusted by developers and marketers for time-savings, scalability, and delivery expertise.

      %open-track%

      "
      );
    24. mail.addContent(content);
    25. request.setBody(mail.build());
    26. //发送邮件
    27. Response response = sg.api(request);
    28. System.out.println(response.getStatusCode());
    29. System.out.println(response.getBody());
    30. System.out.println(response.getHeaders());
    31. mapTypes = JSON.parseObject(response.getBody(),Map.class);
    32. } catch (Exception e) {
    33. e.printStackTrace();
    34. }
    35. return mapTypes;
    36. }

    注意:短信的pom:  

    1. <dependency>
    2. <groupId>com.twilio.sdkgroupId>
    3. <artifactId>twilioartifactId>
    4. <version>7.55.0version>
    5. dependency>

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

  • 相关阅读:
    【视觉SLAM十四讲学习笔记】第一讲
    多测师肖sir_高级讲师_第二个月python讲解03(索引、切片、字符、列表、元组、字典、集合)
    剑指 Offer 12. 矩阵中的路径
    【算法|虚拟头节点|链表】移除链表元素
    Postgresql pgsql 插件之pg_trgm,rum
    Win32编程串口超时结构体的一般性设置
    联想M7216NWA打印一体机墨粉清零方法
    苹果14手机怎么投屏到mac电脑上面?
    ppo-clip的本质以及它为什么是另一种ppo-KL-penalty
    pandas.read_csv() 处理 CSV 文件的 6 个有用参数
  • 原文地址:https://blog.csdn.net/qq_37272886/article/details/134559196