Vert.x Email客户端,用于通过本地邮件服务器(eg. postfix)或外部邮件服务器(eg. googlemail 或 aol)发送 SMTP 电子邮件。
1. maven项目依赖2.YAML文件配置io.vertx vertx-webio.vertx vertx-config-yamlio.vertx vertx-mail-clientcom.fasterxml.jackson.core jackson-databindcom.lance.common vertx-common-core0.0.1-SNAPSHOT
server: port: 18000 mail: hostname: smtp.mxhichina.com username: service@lance.com password: lance@123456 port: 253.启动加载配置文件, 并放入到config中去
public class MailApplication { public static void main(String[] args) { Vertx vertx = Vertx.vertx(); ConfigRetriever retriever = readYaml(vertx); retriever.getConfig(json -> { JsonObject object = json.result(); MailHelper dbHelper = new MailHelper(object.getJsonObject("mail"), vertx); dbHelper.afterPropertiesSet(); DeploymentOptions options = new DeploymentOptions().setConfig(object); vertx.deployVerticle(MainApp.class.getName(), options); }); } private static ConfigRetriever readYaml(Vertx vertx) { ConfigStoreOptions store = new ConfigStoreOptions() .setType("file") .setFormat("yaml") .setOptional(true) .setConfig(new JsonObject().put("path", "application.yaml")); return ConfigRetriever.create(vertx, new ConfigRetrieverOptions().addStore(store)); } }4.Mail client连接配置
public class MailHelper { private final JsonObject object; private final Vertx vertx; @Getter private static MailClient mailClient; @Getter private static String from; public void afterPropertiesSet() { ConfigProperties.MailProperties properties = object.mapTo(ConfigProperties.MailProperties.class); from = properties.getUsername(); MailConfig config = new MailConfig(); config.setHostname(properties.getHostname()); config.setUsername(from); config.setPassword(properties.getPassword()); config.setPort(properties.getPort()); config.setStarttls(properties.getStarttls()); mailClient = MailClient.create(vertx, config); } }5.Email发送执行实例
public class UserService { private final static String SUFFIX_JAR = ".jar!"; public void sendMessage(RoutingContext ctx) { UserVo userVo = ctx.getBodyAsJson().mapTo(UserVo.class); userVo.setHtml(false); send(ctx, userVo, null); } public void sendHtmlMessage(RoutingContext ctx) { UserVo userVo = ctx.getBodyAsJson().mapTo(UserVo.class); String html = "This is html text Vertx--How To"; userVo.setContent(html); userVo.setHtml(true); send(ctx, userVo, null); } public void sendAttachMessage(RoutingContext ctx) { UserVo userVo = ctx.getBodyAsJson().mapTo(UserVo.class); String html = "Hello world.6.项目完整地址This is attach text Vertx--How To
"; userVo.setContent(html); userVo.setHtml(true); FileSystem fs = Vertx.vertx().fileSystem(); String path = getPathString("attach/file.txt"); Buffer buffer = fs.readFileBlocking(path); MailAttachment attachment = MailAttachment.create(); attachment.setContentType("text/plain"); attachment.setData(buffer); attachment.setName(new File(path).getName()); attachment.setDescription("Attachments can be created by the MailAttachment object using data stored in a Buffer, this supports base64 attachments."); send(ctx, userVo, Collections.singletonList(attachment)); } private void send(RoutingContext ctx, UserVo userVo, Listattachments) { MailMessage message = new MailMessage(); message.setFrom(MailHelper.getFrom()); message.setTo(userVo.getTo()); message.setCc(userVo.getCc()); message.setSubject(userVo.getSubject()); if (userVo.isHtml()) { message.setHtml(userVo.getContent()); } else { message.setText(userVo.getContent()); } if (attachments != null) { message.setAttachment(attachments); } MailHelper.getMailClient().sendMail(message) .onSuccess(r -> { log.info("===>send content mail success: {}", r); ctx.json(R.data(r.toJson())); }) .onFailure(e -> { log.error("===> send content mail fail: ", e); ctx.json(R.data(e.getMessage())); }); } private String getPathString(String fileName) { if (StringUtils.isBlank(fileName)) { throw new RuntimeException("filename is null"); } URL path = Thread.currentThread().getContextClassLoader().getResource(fileName); if (path != null && path.getPath().contains(SUFFIX_JAR)) { return fileName; } else { return path == null ? "" : path.getPath(); } } }
发送邮件附件截图
Vertx之Mail邮件客户端 Github 地址
Vertx之Mail邮件客户端 Gitee 地址
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)