发送邮件&短信(网易云信)

发送邮件&短信(网易云信)


发送邮件

  • CURL发送邮件
    1.测试所安装的curl是否支持smtp(若是树莓派,安装一个curl的open-ssl就可以)

    curl-config --protocols |grep SMTP
        SMTP
        SMTPS

    2.确认邮箱的第三方客户端密码(163、qq都有设置)
    3.curl发送代码(没有尝试发送附件,后加)

    curl -s   --url "smtp://smtp.163.com" \
          --mail-from "[email protected]" \
          --mail-rcpt "[email protected]" \ 
          --upload-file mail.txt --user "[email protected]:xxxxxx"
    
    参数说明: --url 邮箱服务器地址 可在163、qq等邮箱上找到
          --mail-from  发件人
          --mail-rcpt  收件人
          --upload-file 正文
          --user  发送人的邮箱账号:第三方客户端密码
    mail.txt文件格式说明
    
        From:[email protected]
        To:[email protected]
        Subject: 邮件测试
    
        你好!这是一封测试邮件
  • 163设置第三方客户端密码
    163

  • qq邮箱设置第三方客户端密码
    qq
  • 163发送邮件到qq邮箱
    163toqq

发送短信


本想用飞信的,但是不提供API,网友提供的又不能用,我也没去分析飞信的url。就换成了网易云信,
功能挺强,发送也挺简单,但是太贵,最少40000(2000块钱!)。

  • 注册云信账号,创建应用
  • 添加短信功能,购买短信(最少40000条)或者免费试用(需要等待审核,太慢)
  • curl发送短信功能代码

    curl -X POST  -H "AppKey:你的应用appkey" \
                         -H "CurTime: 当前时间戳,最多使用五分钟" \
                         -H "CheckSum: 校验和(最开始坑在这)" \
                         -H "Nonce: (随机数最多12位)" \ 
                         -H "charset: utf-8" \
                         -H "Content-Type: application/x-www-form-urlencoded" \
                                  -d 'templateid=模板号(最后坑这,放弃了使用)&mobiles=["手机号"]&params=["hello"]' \ 
                                  'https://api.netease.im/sms/sendtemplate.action'
  • 参数:CheckSum&CurTime

    • CheckSum
      CheckSum=SHA1(AppSecret + Nonce + CurTime)
      三个参数(应用密码+随机数+当前时间戳)拼接的字符串,进行SHA1哈希计算,转化成16进制字符(String,小写)

    • CurTime:获取当前时间戳(最多使用五分钟)
      shell命令:date ‘+%s’    结果: 1489329510

      • templateid短信模板:自己添加,之后等待审核。很慢。
  • 参考:

  • 附录:CheckSum java代码

import java.security.MessageDigest;
public class CheckSumBuilder {
    public static void main(String[] args)
    {
     //我在这写了个固定输出,把你的APP_Secret 随机数 CurTime传进去生成校验和填到上述代码的
     //CheckSum处       
     System.out.println(getCheckSum("a98cf236e6b",\
                                    "4tgggergig6w323t23t", \
                                    "1489323670"));
    }
    // 计算并获取CheckSum
    public static String getCheckSum(String appSecret, String nonce, String curTime) {
        return encode("sha1", appSecret + nonce + curTime);
    }
    // 计算并获取md5值
    public static String getMD5(String requestBody) {
        return encode("md5", requestBody);
    }

    private static String encode(String algorithm, String value) {
        if (value == null) {
            return null;
        }
        try {
            MessageDigest messageDigest
                    = MessageDigest.getInstance(algorithm);
            messageDigest.update(value.getBytes());
            return getFormattedText(messageDigest.digest());
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
    private static String getFormattedText(byte[] bytes) {
        int len = bytes.length;
        StringBuilder buf = new StringBuilder(len * 2);
        for (int j = 0; j < len; j++) {
            buf.append(HEX_DIGITS[(bytes[j] >> 4) & 0x0f]);
            buf.append(HEX_DIGITS[bytes[j] & 0x0f]);
        }
        return buf.toString();
    }
    private static final char[] HEX_DIGITS = { '0', '1', '2', '3', '4', '5',
            '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
}

猜你喜欢

转载自blog.csdn.net/scylhy/article/details/61665527