SendGrid安装步骤与命令
- $heroku addons:create sendgrid
- $heroku config:get SENDGRID_USERNAME and $heroku config:get SENDGRID_PASSWORD获取用户名与密码
- 到达该网址https://app.sendgrid.com/settings/api_keys ,点击Create API Key,默认选中Full Access, 利用"SENDGRID_USERNAME"与"SENDGRID_PASSWORD"获取"SENDGRID_API_Key".
- 命令"heroku config:set SENDGRID_API_KEY=xxxx_api_key_xxxx"更改SENDGRID_API_KEY为刚才获取的KEY
SendGrid邮件发送功能于java代码实现
- 修改配置文件pom.xml, 添加保存:
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.1.0</version>
</dependency>
- 命令$mvn intsall下载sendgrid-java.jar,确认该jar包已存在
- 参照添加代码:
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;
}