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, 添加保存:

   com.sendgrid
   sendgrid-java
   4.1.0

  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\":\"[email protected]\"}],\"subject\":\"Hello World from the SendGrid Java Library!\"}],\"from\":{\"email\":\"[email protected]\"},\"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;
    }

你可能感兴趣的:(heroku邮件功能——插件安装与java代码实现)