• 使用hutool阿里云企业邮箱发送邮件和附件,包含PDF转图片base64,PDF转HTML


    请务必开启阿里云服务器465 ssl邮件端口
    废话不多,我们直接上代码。

    maven添加依赖:

    <dependency>
      <groupId>cn.hutool</groupId>
        <artifactId>hutool-all</artifactId>
        <version>5.8.22</version>
    </dependency>
    
    <dependency>
      <groupId>e-iceblue</groupId>
        <artifactId>spire.pdf.free</artifactId>
        <version>4.4.1</version>
    </dependency>
    
    <dependency>
        <groupId>org.apache.pdfbox</groupId>
        <artifactId>pdfbox</artifactId>
        <version>2.0.24</version>
    </dependency>
    
    
    <repositories>
        <repository>
            <id>com.e-iceblue</id>
            <url>https://repo.e-iceblue.cn/repository/maven-public/</url>
        </repository>
    </repositories>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25

    完整代码:

    import cn.hutool.core.collection.CollUtil;
    import cn.hutool.core.io.FileUtil;
    import cn.hutool.core.util.CharsetUtil;
    import cn.hutool.extra.mail.MailAccount;
    import cn.hutool.extra.mail.MailUtil;
    import com.sun.mail.util.MailSSLSocketFactory;
    import lombok.extern.slf4j.Slf4j;
    import org.apache.commons.io.FileUtils;
    import org.apache.pdfbox.pdmodel.PDDocument;
    import org.apache.pdfbox.rendering.ImageType;
    import org.apache.pdfbox.rendering.PDFRenderer;
    import org.junit.jupiter.api.Test;
    
    import java.awt.image.BufferedImage;
    import java.io.*;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.nio.charset.StandardCharsets;
    import java.util.ArrayList;
    import java.util.Base64;
    import java.util.List;
    
    import com.spire.pdf.*;
    
    import javax.imageio.ImageIO;
    
    /**
     * @author hsj
     * @description:阿里云企业邮箱发送邮件和附件(请开启阿里云服务器465端口)
     * @date 2024-4-25 8:59
     */
    @Slf4j
    public class Temail {
    	/**
    	 * @Description:发邮件
    	 * @author HeShengjin 2356899074@qq.com
    	 * @date 2024-4-25 10:11
    	 */
    	@Test
    	public void t() throws IOException {
    		InputStream inputStream = null;
    		try {
    			//公司阿里云企业邮箱
    			String from = "你的阿里云企业邮箱";
    
    			// 加载PDF文档URL
    			URL url = new URL("https://jixujiaoyu-test.oss-cn-shenzhen.aliyuncs.com/upload/20240425/1954fae396aa5aa1eb3dbad8edc19547.pdf");
    			HttpURLConnection conn = (HttpURLConnection) url.openConnection();
    			inputStream = conn.getInputStream();
    
    //			// 获取字节数组
    			byte[] bytesPdf = readInputStream(inputStream);
    
    
    			MailSSLSocketFactory sf = new MailSSLSocketFactory();
    			sf.setTrustAllHosts(true);
    
    			MailAccount account = new MailAccount();
    			account.setHost("smtp.mxhichina.com");
    			account.setPort(465);
    			account.setAuth(true);
    			account.setSslEnable(true);
    			account.setFrom(from.trim());
    			account.setUser(from.trim());
    			account.setPass("你的阿里云企业邮箱密码");
    			account.setCharset(CharsetUtil.CHARSET_UTF_8);
    			account.setCustomProperty("mail.smtp.ssl.socketFactory", sf);
    			MailUtil.send(account,
    				CollUtil.newArrayList("你的要发送的目标邮箱"),//目标邮箱
    				"主题名称",//邮件主题
    				"

    测试发送邮件,使用阿里云云邮箱(发送中文名字附件)。

    "
    , true,//支持HTML内容 FileUtil.writeBytes(bytesPdf,String.format("%s%s",FileUtil.getTmpDirPath(),"中文名字附件.pdf"))); } catch (Exception e){ e.printStackTrace(); log.error(String.format("发送邮件失败,%s",e.getMessage())); } finally { if (inputStream != null){ inputStream.close(); } } } /** * @Description:PDF转HTML * @author HeShengjin 2356899074@qq.com * @date 2024-4-25 10:11 */ @Test public void t2() throws IOException{ InputStream inputStream = null; try { // 加载PDF文档URL URL url = new URL("https://jixujiaoyu-test.oss-cn-shenzhen.aliyuncs.com/upload/20240425/1954fae396aa5aa1eb3dbad8edc19547.pdf"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); inputStream = conn.getInputStream(); // 获取字节数组 byte[] bytes = readInputStream(inputStream); //加载PDF文档 PdfDocument pdf = new PdfDocument(); pdf.loadFromBytes(bytes); //设置useEmbeddedSvg和 useEmbeddedImg布尔值为true pdf.getConvertOptions().setPdfToHtmlOptions(true,true); //保存到流 File outFile = new File("PDFtoHTML.html"); OutputStream outputStream = new FileOutputStream(outFile); pdf.saveToStream(outputStream, FileFormat.HTML); pdf.close(); System.out.println("PDF转换成HTML完成!"); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null){ inputStream.close(); } } } /** * @Description:PDF转图片base64 * @author HeShengjin 2356899074@qq.com * @date 2024-4-25 10:12 */ @Test public void t3() throws IOException{ InputStream inputStream = null; ByteArrayOutputStream byteArrayOutputStream = null; try { // 加载PDF文档URL URL url = new URL("https://jixujiaoyu-test.oss-cn-shenzhen.aliyuncs.com/upload/20240425/1954fae396aa5aa1eb3dbad8edc19547.pdf"); HttpURLConnection conn = (HttpURLConnection) url.openConnection(); inputStream = conn.getInputStream(); // // 获取字节数组 byte[] bytesPdf = readInputStream(inputStream); List<BufferedImage> bufferedImages = pdfToImage(bytesPdf); byteArrayOutputStream = new ByteArrayOutputStream(); ImageIO.write(bufferedImages.get(0),"jpg",byteArrayOutputStream);//只要PDF第一页的一张图片 // 清流 byteArrayOutputStream.flush(); // 转为byte[] byte[] byteImage = byteArrayOutputStream.toByteArray(); // 将图片数据转换为Base64字符串 String base64String = Base64.getEncoder().encodeToString(byteImage); String html = String.format("",base64String); // //保存到流 File outFile = new File("PDFtoIMGBase64.html"); FileUtils.writeByteArrayToFile(outFile,html.getBytes(StandardCharsets.UTF_8)); System.out.println("PDF转换成HTML完成!"); } catch (Exception e) { e.printStackTrace(); } finally { if (inputStream != null){ inputStream.close(); } if (byteArrayOutputStream != null){ // 关流 byteArrayOutputStream.close(); } } } /** * * @Description: (InputStream转字节数组) * @author hsj * @date 2019年8月8日 */ public byte[] readInputStream(InputStream inputStream) throws IOException { byte[] buffer = new byte[1024]; int len = 0; ByteArrayOutputStream bos = new ByteArrayOutputStream(); while((len = inputStream.read(buffer)) != -1) { bos.write(buffer, 0, len); } bos.close(); return bos.toByteArray(); } /** * PDF转图片 (一个PDF很多页,所以很多图片) * @param bytes:文件输入流 * @return */ public List<BufferedImage> pdfToImage(byte[] bytes){ List<BufferedImage> images = new ArrayList<>(); PDDocument document = null; try { // 加载PDF文档 document = PDDocument.load(bytes); // 创建PDFRenderer对象 PDFRenderer renderer = new PDFRenderer(document); for (int i = 0; i < document.getNumberOfPages(); i++) { /** * 《 72 》 此处设置得越大像素越高,生成得时候也会越久 * DPI 的设置一般根据具体的需求和使用场景来决定。DPI 越高,生成的图片分辨率越大,图像质量也越高, * 但同时文件大小也会变得更大。通常情况下,如果需要对生成的图片进行放大、裁剪等操作,建议将 DPI 设置得较高, * 以保证图像质量和细节的清晰度;如果只是需要简单地浏览或共享图片,可以适当降低 DPI 以减小文件大小。在实际开发中, * 可以根据不同的应用场景进行调整。一般来说,72 DPI 是一个比较常见的默认值,可以作为参考。 */ BufferedImage image = renderer.renderImageWithDPI(i, 72, ImageType.RGB); images.add(image); } return images; } catch (Exception e) { e.printStackTrace(); return null; } finally { // 关闭文档 try { if (document != null) { document.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
    • 213
    • 214
    • 215
    • 216
    • 217
    • 218
    • 219
    • 220
    • 221
    • 222
    • 223
    • 224
    • 225
    • 226
    • 227
    • 228
    • 229
    • 230
    • 231
  • 相关阅读:
    webrtc终极版(二)搭建自己的iceserver服务,并用到RTCMultiConnection的demo中
    OpenGLES:绘制一个混色旋转的3D球体
    SpringCloud Alibaba&注册中心(nacos)&远程调用(OpenFeign)使用
    什么是电源高压测试标准?如何测试?测试时要注意什么?
    python中StringIO和BytesIO
    超算基础概念
    1.4+1.5 L1、L2正则化
    Android : ListView + BaseAdapter-简单应用
    【微信小程序开发】之微信授权登陆
    springbootAsyncConfig配置的处理
  • 原文地址:https://blog.csdn.net/HSJ0170/article/details/138184209