java springboot项目,使用sendgrid提供一个发送邮件的的rest服务
调用一次接口,使用同一个邮箱,给多个地址发送邮件,并且收件人能看到其他收件地址的情况。
tip:如果不想看到其他收件人地址,可根据情况调整后端代码。
一:springboot引入sendgrid依赖
<dependency>
<groupId>com.sendgrid</groupId>
<artifactId>sendgrid-java</artifactId>
<version>4.7.6</version>
</dependency>
二:配置sendgrid参数(如果参数直接在java文件中写死,请忽略)
application.yml文件配置参数
twilio:
apikey: xxxxxxxxxxxx #sendgrid中注册账号是获取的apikey
fromemail: yyyy@qq.com #来源邮箱
然后新建一个配置类,TwilioConfig
@Configuration
@Getter
public class TwilioConfig {
@Value("${twilio.apikey}")
private String apikey;
@Value("${twilio.fromemail}")
private String fromEmail;
}
三:创建rest接口
rest接口中,参数使用是一个类对象,方便接受多个参数或者数组对象。也可以用@RequestParam接受参数,但是比较麻烦。
@RestController
@Slf4j
public class MessageApi {
@Resource
private TwilioConfig twilioConfig;//配置类对象
@RequestMapping(value = "/message/email", method = RequestMethod.POST)
public CommonResult email(@RequestBody EmailParam param) {
return null;
}
}
然后看一下EmailParam类的参数(这个根据实际情况来定义属性)
@Data
@ToString(callSuper = true)
public class EmailParam implements Serializable {
private String mimeType;//类型
private String subject;//主题
private List<EmailAddressBean> to;//发送邮箱
private String content;//内容
private List<EmailAttachmentBean> attachment;//附件
}
因为需要发给多个地址,且邮件地址有别名,所以用List来接收一个数组对象
(attachment附件内容暂时没测试)
@Data
@ToString(callSuper = true)
public class EmailAddressBean implements Serializable {
private String userName;//名称
private String address;//地址
}
@Data
@ToString(callSuper = true)
public class EmailAttachmentBean implements Serializable {
private String fileName;//类型
private String content;//附件的base64编码
}
四:发送邮件的代码
@RequestMapping(value = "/message/email", method = RequestMethod.POST)
public CommonResult email(@RequestBody EmailParam param) {
log.info("request param is {}", param);
//yml文件中配置的from地址
Email from = new Email(twilioConfig.getFromEmail());
Mail mail = new Mail();
//多个地址
List<EmailAddressBean> toAddress = param.getTo();
/*
*如果说不需要在一次发送中,发给多个人
*或者不想要收件人看到其他的收件人地址,可以修改下面的循环,
*让每次循环都执行一次发送请求就好
/
Personalization personalization = new Personalization();
if (toAddress != null && !toAddress.isEmpty()) {
for (EmailAddressBean emailAddress : toAddress) {
Email to = new Email(emailAddress.getAddress());
personalization.addTo(to);
personalization.addSubstitution("-name-", emailAddress.getUserName());
}
}
mail.addPersonalization(personalization);
//邮件支持html格式的内容,默认发送为text
String type = "text/plain";
if("html".equals(param.getMimeType())){
type = "text/html";
}
Content content = new Content(type, param.getContent());
mail.setFrom(from);//发送邮箱
mail.setSubject(param.getSubject());//主题
mail.addContent(content);//内容
//yml文件中配置的apikey
SendGrid sg = new SendGrid(twilioConfig.getApikey());
Request request = new Request();
try {
request.setMethod(Method.POST);
request.setEndpoint("mail/send");
request.setBody(mail.build());
Response response = sg.api(request);
} catch (IOException ex) {
System.out.println("Exception is :" + ex.getMessage());
return CommonResult.failed(ex.getMessage());
}
return CommonResult.success();
}
五:用postman测试一下
tip:postman的下载和使用,网上搜索一下就好,很简单
1 请求方法:POST
2 请求头:Content-Type: application/json
3 请求体:JSON格式
{
"mimeType":"html",# html 或者text,前端参数可自定义类型
"subject":"测试主题666",
"content":"<label style='color: red' >邮件测试内容</label>",
"to":[
{
"userName":"xxx",
"address":"xxx@qq.com"
},
{
"userName":"yyy",
"address":"yyy@163.com"
}
]
}