这是使用sendgrid电子邮件提供商的api的简短演示。 **克隆github存储库以获得更多详细信息! ** 我正在通过sendgrid Java客户端和在springboot制成的微服务中使用此api,并且像所有好的代码一样,它是经过测试的代码,让我们使用greenmail库模拟smtp服务器来接收电子邮件。

First of all you need to create an account in Sendgrid and enable an api Key to use in client.

There are some ways to use this sendgrid sdk and in this case will be approached the sending of e-mail that will be transferred by http connection in json format. There is another way to send an email, which would be to register a template in the administrative panel of sendgrid site and send it through the client with the key of the registered template. To do any invocation of sendgrid it is necessary to send the key, which is available in the sendgrid admin area.

要调用sendgrid api,必须构建请求对象,如下所示。

    public class RequestSendGridBuilder {private static final String MIME_TYPE_HTML = "text/html";private static final String ENDPOINT_MAIL_SEND = "mail/send";private Mail mail = new Mail();private Content content = new Content();public static RequestSendGridBuilder of() {return new RequestSendGridBuilder();}public RequestSendGridBuilder from(String from) {mail.setFrom(new Email(from));return this;}public RequestSendGridBuilder key(String key) {mail.addCustomArg("customerAccountNumber", key);return this;}public RequestSendGridBuilder content(String template) {content.setType(MIME_TYPE_HTML);content.setValue(template);return this;}public RequestSendGridBuilder subject(String subject) {mail.setSubject(subject);return this;}public RequestSendGridBuilder to(String to) {Personalization personalization = new Personalization();personalization.addTo(new Email(to));mail.addPersonalization(personalization);return this;}public Request build() throws IOException {mail.addContent(content);Request request = new Request();request.setMethod(Method.POST);request.setEndpoint(ENDPOINT_MAIL_SEND);request.setBody(mail.build());return request;}}

然后致电服务:

    @Overridepublic EmailResponseDTO send(EmailRequestDTO request) throws EmailConnectionException {try {Request sendGridRequest = RequestSendGridBuilder.of().content(request.getContent()).subject(request.getSubject()).to(request.getTo()).from(request.getFrom()).key(sendGridKey).build();Response sendGridResponse = sendGrid.api(sendGridRequest);return getResponse(sendGridResponse, sendGridRequest);} catch (IOException e) {throw new EmailConnectionException(e);}}

是的,这很简单! 但不要忘记设置sendgrid API_KEY,否则它将无法正常工作。

Templates

只要您发送带有html内容的json,就可以使用自己的模板引擎。 在这种情况下,您将在项目中包含模板(例如html),并且需要进行更新以进行更新。

If you don't want to traffic large size of bytes over the network because of a very large email, it is possible, as previously said, to write your template in the administrative template panel, after creating the template, a key will be generated to identify it, and this key will be sent by the client to the service, so you do not have to traffic all the content of the email. In addition you will be able to update your template at runtime directly on the site https://sendgrid.com

    mail.setTemplateId(templateId);Request request = new Request();request.setMethod(Method.POST);request.setEndpoint(ENDPOINT_MAIL_SEND);request.setBody(mail.build());Response sendGridResponse = sendGrid.api(request);

Template parser parameters

在此示例中,我使用了胡子来将参数解析为模板。 它非常易于使用且非常高效。 要更改模板变量的值,只需将字符串映射传递到髭引擎,如下所示:

    public class TemplateService {private MustacheAutoConfiguration mustacheAutoConfiguration;public TemplateService(MustacheAutoConfiguration mustacheAutoConfiguration) {this.mustacheAutoConfiguration = mustacheAutoConfiguration;}public @NonNull String parseTemplateParams(EmailRequestDTO email) throws TemplateException {try {MustacheResourceTemplateLoader templateLoader = mustacheAutoConfiguration.mustacheTemplateLoader();Reader template = templateLoader.getTemplate(email.getTemplateName());return mustacheAutoConfiguration.mustacheCompiler(templateLoader).compile(template).execute(email.getTemplateParams());} catch (FileNotFoundException e) {throw new TemplateException("TEMPLATE_NOT_FOUND");} catch (MustacheException e) {throw new TemplateException("TEMPLATE_PARSE_ERROR", e);} catch (Exception e) {throw new TemplateException("GENERIC_ERROR", e);}}}

Test sending with a fake smtp mail server

要创建电子邮件集成测试,我正在使用greenmail库。 该库允许您创建一个伪造的smtp服务器,该服务器将接收客户端发送的电子邮件,因此可以保证更好的资格和实施效率。

  public static GreenMail getGreenMail() {if (greenMail == null) {ServerSetup serverSetup = new ServerSetup(SocketUtils.findAvailableTcpPort(INIT_RANGE_EMAIL_PORT,END_RANGE_EMAIL_PORT),ServerSetup.getLocalHostAddress(),ServerSetup.PROTOCOL_SMTP);greenMail = new GreenMail(serverSetup);greenMail.setUser(EMAIL_USER_ADDRESS,USER_NAME,USER_PASSWORD);greenMail.start();}return greenMail;}

Source code

To see the client working you can use the complete code here
ENJOY!!!

References

Http://www.icegreen.com/greenmail/
Https://sendgrid.com/docs/api-reference/

from: https://dev.to//georgeoikawa/send-mail-easily-springboot-sendgrid-and-greenmail-9g

轻松发送邮件! springboot,sendgrid和greenmail相关推荐

  1. SpringBoot发送邮件通过SendGrid服务平台(带动态模板)

    SpringBoot发送邮件通过SendGrid服务平台 一.开通API Keys 二.后台实现 一.开通API Keys 二.后台实现 加入maven依赖 <dependency>< ...

  2. 小白轻松学SpringBoot系列

    小白轻松学SpringBoot系列 Spring Boot简介 Spring Boot特点: Spring Boot 学习地址: Spring Boot简介 Spring Boot目前也是一个比较火的 ...

  3. itextpdf api帮助文档_我开源了一个小工具,可以帮你轻松生成 SpringBoot API 文档...

    前言 大家好,我叫叶大侠,一名独立开发者.这个文档工具是我17年的一个想法,当时还是在公司里面上班,负责App客户端的开发工作,当时后端童鞋写文档的意愿比较低,总是要等他们开发完接口,然后才在微信上沟 ...

  4. kafka 可视化工具_两小时带你轻松实战SpringBoot+kafka+ELK分布式日志收集

    一.背景 随着业务复杂度的提升以及微服务的兴起,传统单一项目会被按照业务规则进行垂直拆分,另外为了防止单点故障我们也会将重要的服务模块进行集群部署,通过负载均衡进行服务的调用.那么随着节点的增多,各个 ...

  5. 一篇博客带你轻松应对Springboot面试

    1. SpringBoot简介 SpringBoot是简化Spring应用开发的一个框架.他整合了Spring的技术栈,提供各种标准化的默认配置.使得我们可以快速开发Spring项目,免掉xml配置的 ...

  6. get这3种方式 轻松发送邮件超大附件

    日常工作中,员工之间.企业之间通过邮件形式进行信息交换是很普遍的,这也是一种相对较正式的文件交换方式,一般的小文件发送是没有问题的,但是总会遇到一些大容量的文件需要发送. 邮件附件太大,上传.发送不了 ...

  7. 怎么会不喜欢呢,CICD中轻松发送邮件

    不知道大家在工作中,是不是也和我一样需要经常发送邮件呢,邮件可以事前清晰传达,事后有据可查,同时还有利于保密,一般用于比较正式的交流. 之前已经写过一次用DSL发送邮件的文章了,自从建木CIv2.4. ...

  8. 教你轻松将springboot打包成exe程序

    作者:如漩涡 blog.csdn.net/m0_37701381/article/details/104163877 前言 近期做了一个前后端合并的spring boot项目,但是要求打成exe文件, ...

  9. 轻松实现SpringBoot项目异常全局处理

    1.Restful接口:当出现异常的时候,我们并不希望前端能够看到后台异常栈信息,更希望正常返回一段JSON,效果如下: 有两种方案,一种是intercept的形式,但如果用来处理异常的话,貌似不太适 ...

最新文章

  1. silverlight之How To:设置ComboBox控件的数据源当ComboBox用来作为DataGrid的某列的编辑控件时...
  2. 华为nova 2 Plus魔镜版818欢购热潮凶猛来袭
  3. SpringBoot介绍
  4. 划动浮空岛_划动浮空岛攻略轻松通关要点详解
  5. 【Python基础】在pandas中使用数据透视表
  6. 【机器学习算法专题(蓄力计划)】三、机器学习中的概率论基础精讲
  7. 大一新生开发的小工具火了!不一样的 Python 编程体验,还是可视化的那种
  8. 一键免费下载外文文献的方式
  9. 钳位型过压保护器件压敏电阻MOV的生产工艺你知道吗?
  10. XPI 文件安装方法
  11. VScode底部状态栏不见,手把手教学
  12. [MySQL远程备份策略举例]
  13. 洞见科技解决方案总监薛婧:联邦学习助力数据要素安全流通
  14. SAPGUI 里 F1 功能键的用法专题讲解试读版
  15. Oracle常見問題查詢
  16. 论文浅尝 | DKN: 面向新闻推荐的深度知识感知网络
  17. C# WinForm窗体制作以图片为背景的登陆界面
  18. 《校园墙》小程序可行性分析
  19. markdown导出以及合并pdf
  20. 无法打开数据库‘Data’.恢复操作已将数据库标记为SUSPECT。

热门文章

  1. 女神节送什么比较好?38节送礼推荐
  2. Visual Studio如何打开项目
  3. libreoffice的启动、测试和问题记录
  4. php 教学心得 论文,信息技术教学心得体会
  5. 强大的.NET反编译工具Reflector及插件
  6. 专利号校验码php,专利申请号校验位怎么去理解?
  7. U盘autorun.inf病毒免疫
  8. Java实现 LeetCode 72 编辑距离
  9. 我想要绅博注册邀请码
  10. [BMC][IPMI] 快速理解 FRU 和 VPD