图像在网络浏览器中显示为base64,但是当我通过电子邮件发送时,它不显示。
String filePath = System.getProperty("catalina.base")+"/conf/app-core/logo.png";
Path path = Paths.get(filePath);
byte[] data = new byte[0];
data = Files.readAllBytes(path);
byte[] encoded = java.util.Base64.getEncoder().encode(data);
String imgDataAsBase64 = new String(encoded);
String imgAsBase64 = "data:image/png;base64," + imgDataAsBase64;
map.put("logo", imgAsBase64);
模板如下所示:
<img alt="Logo" src="${logo}" />
电子邮件的来源如下:
<img alt="logo" src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAGQAAAAXCAYAAAD9VOo7AAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAA3JpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuNi1jMDY3IDc5LjE1Nzc0NywgMjAxNS8wMy8zMC0yMzo0MDo0MiAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmRmPSJodHRwOi8vd3d3LnczLm9yZy8xOTk5LzAyLzIyLXJkZi1zeW50YXgtbnMjIj4gPHJkZjpEZXNjcmlwdGlvbiByZGY6YWJvdXQ9IiIgeG1sbnM6eG1wTU09Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC9tbS8iIHhtbG5zOnN0UmVmPSJodHRwOi8vbnMuYWRvYmUuY29tL3hhcC8xLjAvc1R5cGUvUmVzb3VyY2VSZWYjIiB4bWxuczp4bXA9Imh0dHA6Ly9ucy5hZG9iZS5jb20veGFwLzEuMC8iIHhtcE1NOk9yaWdpbmFsRG9jdW1lbnRJRD0ieG1wLmRpZDpmN2FhZjg1NS1iZTdkLTRlNGUtYjU4Zi1mNjIwYTU4NDMwYWQiIHht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mczqOVXmfV97TVHANG15HIz0AFvDep53lf8JRVgzyuQnKe56aOb+DF1U9/XgVYu5VMor3c+0OlXRw30wzZuR8hqe4LHFzgqzvb+VNcr6c8+GL/d6y3OJH59B3d5OHjfR3qvOITTbzMBphnLLlUDqPYTlVL+a1xWiVLpZtyXfx+eUsVAeGu8SPBH/x6dwenASTGJ/u0vbZlrLksLeyizOIU2cr+ya6KfAZP+GKB3EXzwGx9K5qOB03cjSYksg8t/KysJMun2xyn1q8qoEcabncxE9zCVwHPrfUId7W/dzA+6iLxYX0fPTaP4tl2M45vXmuKLVcNDou/WQuhdvpQk9leSJLWL6WFfztw5Q87omrA5gl+TxjxZx1HAKw4v+NFN6HDVPfJrmsGNGhrt8X/icHzzOJyGjO9GS+N3DPKD4vFYcN4imy9Czj7yxV/O3DbckLifwrwAA4KctId+YfSgAAAABJRU5ErkJggg==" />
如果我使用base64在线转换器,则可以从上面正确生成图像。但是,图像不会显示在任何电子邮件程序中。它只显示alt消息(在gmail中)或X(在outlook中)</ p>