heroku邮件功能——插件安装与java代码实现

SendGrid安装步骤与命令

  1. $heroku addons:create sendgrid
  2. $heroku config:get SENDGRID_USERNAME and $heroku config:get SENDGRID_PASSWORD获取用户名与密码
  3. 到达该网址https://app.sendgrid.com/settings/api_keys ,点击Create API Key,默认选中Full Access, 利用"SENDGRID_USERNAME"与"SENDGRID_PASSWORD"获取"SENDGRID_API_Key".
  4. 命令"heroku config:set SENDGRID_API_KEY=xxxx_api_key_xxxx"更改SENDGRID_API_KEY为刚才获取的KEY

SendGrid邮件发送功能于java代码实现

  1. 修改配置文件pom.xml, 添加保存:
<dependency>
   <groupId>com.sendgrid</groupId>
   <artifactId>sendgrid-java</artifactId>
   <version>4.1.0</version>
</dependency>
  1. 命令$mvn intsall下载sendgrid-java.jar,确认该jar包已存在
  2. 参照添加代码:
try {
      SendGrid sg = new SendGrid(System.getenv("SENDGRID_API_KEY"));
        Request request = new Request();
        request.setMethod(Method.POST);
        request.setEndpoint("mail/send");
        request.setBody("{\"personalizations\":[{\"to\":[{\"email\":\"liuyue_lilu@sina.com\"}],\"subject\":\"Hello World from the SendGrid Java Library!\"}],\"from\":{\"email\":\"test@example.com\"},\"content\":[{\"type\":\"text/plain\",\"value\": \"Hello, Email!\"}]}");
        Response response = sg.api(request);
        System.out.println(response.getStatusCode());
        System.out.println(response.getBody());
        System.out.println(response.getHeaders());
    } catch (IOException ex) {
      throw ex;
    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值