我是靠谱客的博主 大胆鲜花,这篇文章主要介绍java EmailKit内嵌图片,带附件发送邮件,现在分享给大家,希望可以做个参考。

测试代码如下

复制代码
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
import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableScheduling; import sun.misc.BASE64Encoder; import cn.com.wy.kafka.util.EmailKit; /** * Hello world! * */ @EnableScheduling @SpringBootApplication public class email { protected final static Logger logger = LoggerFactory.getLogger(email.class); public static void main( String[] args ) throws Exception { SpringApplication.run(email.class, args); sencEmail(); } public static void sencEmail(){ //email服务url String mailServer="emailServerUrl"; //账号 String emailUserName="zh"; //密码 String emailPassword="mm"; EmailKit kit = new EmailKit(mailServer, emailUserName, emailPassword); //标题 String title = "标题"; //内容 String content = "<span style = 'font-family:"Microsoft YaHei";font-size:"14px"'>各位领导、同事:<br>" + "这是一封测试邮件。</span>"+ "<br><br><br>" + "<table border = '1' cellspacing = '0' >" + "<tr>" + "<th style = 'text-align:center;'>序号</th>" + "<th style = 'text-align:center;'>测试1</th>" + "<th style = 'text-align:center;'>测试2</th>" + "<th style = 'text-align:center;'>测试3</th>" + "<th style = 'text-align:center;'>测试4</th>" + "<th style = 'text-align:center;'>测试5</th>" + "</tr>" ; content+= "<tr>" + "<td style = 'text-align:center;font-size:"14px"'>1</td>" + "<td style = 'text-align:center;font-size:"14px"'>11</td>" + "<td style = 'text-align:center;font-size:"14px"'>12</td>" + "<td style = 'text-align:center;font-size:"14px"'>13</td>" + "<td style = 'text-align:center;font-size:"14px"'>14</td>" + "<td style = 'text-align:center;font-size:"14px"'>15</td>" + "</tr>"; content+= "</table>"+ "<br><div><span style = 'float:left;font-size:"11px"'><img alt='Embedded Image' src='data:image/png;base64,"+getPhoto()+"'></span></div><br>"; try{ //邮箱地址 String[] toAddr=new String[1]; toAddr[0]="123@qq.com"; //附件数组 String[] addressStr = new String[1]; addressStr[0]="C:\Users\Administrator\Desktop\测试.xlsx";//全路径 //发送邮件 kit.send(title, toAddr, null, content,addressStr); }catch (Exception e) { logger.error("emailService sendMail():==============================="+e.getMessage()); e.printStackTrace(); // throw new } } /** * 图片转base64编码 * @return */ private static String getPhoto(){ //图片路径 String filePath= "C:\Users\Administrator\Desktop\1.png"; String img =null; if(filePath!=null){ InputStream in = null; byte[] picdata = null; try { in = new FileInputStream(filePath); picdata = new byte[in.available()]; in.read(picdata); } catch (Exception e) { e.printStackTrace(); }finally{ try { in.close(); } catch (IOException e) { e.printStackTrace(); } } BASE64Encoder encoder = new BASE64Encoder(); img = encoder.encode(picdata); return img; }else{ return null; } } }

EmailKit工具类,如下

复制代码
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
import java.io.Serializable; import java.net.URI; import java.net.URISyntaxException; import microsoft.exchange.webservices.data.core.ExchangeService; import microsoft.exchange.webservices.data.core.enumeration.misc.ExchangeVersion; import microsoft.exchange.webservices.data.core.enumeration.property.BodyType; import microsoft.exchange.webservices.data.core.service.item.EmailMessage; import microsoft.exchange.webservices.data.credential.WebCredentials; import microsoft.exchange.webservices.data.property.complex.MessageBody; public class EmailKit implements Serializable { private static final long serialVersionUID = 1L; private String mailServer; private String user; private String password; private String domain; public EmailKit(String mailServer, String user, String password) { this.mailServer = mailServer; this.user = user; this.password = password; } public EmailKit(String mailServer, String user, String password, String domain) { this.mailServer = mailServer; this.user = user; this.password = password; this.domain = domain; } public ExchangeService getExchangeService() { ExchangeService service = new ExchangeService(ExchangeVersion.Exchange2010); WebCredentials credentials; if (this.domain == null) { credentials = new WebCredentials(this.user, this.password); } else { credentials = new WebCredentials(this.user, this.password, this.domain); } service.setCredentials(credentials); try { service.setUrl(new URI(this.mailServer)); } catch (URISyntaxException var4) { var4.printStackTrace(); } return service; } public void send(String subject, String[] to, String[] cc, String bodyText, String[] attachmentPaths) throws Exception { ExchangeService service = this.getExchangeService(); EmailMessage msg = new EmailMessage(service); msg.setSubject(subject); MessageBody body = MessageBody.getMessageBodyFromText(bodyText); body.setBodyType(BodyType.HTML); msg.setBody(body); String[] var9 = to; int var10 = to.length; int var11; String attachmentPath; for(var11 = 0; var11 < var10; ++var11) { attachmentPath = var9[var11]; msg.getToRecipients().add(attachmentPath); } if (cc != null) { var9 = cc; var10 = cc.length; for(var11 = 0; var11 < var10; ++var11) { attachmentPath = var9[var11]; msg.getCcRecipients().add(attachmentPath); } } if (attachmentPaths != null) { var9 = attachmentPaths; var10 = attachmentPaths.length; for(var11 = 0; var11 < var10; ++var11) { attachmentPath = var9[var11]; msg.getAttachments().addFileAttachment(attachmentPath); } } msg.send(); } public void send(String subject, String[] to, String[] cc, String bodyText) throws Exception { this.send(subject, to, cc, bodyText, (String[])null); } }

pom所需要的依赖,如下

复制代码
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
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <groupId>cn.com.wy</groupId> <artifactId>test</artifactId> <version>0.0.1-SNAPSHOT</version> <name>test</name> <!-- FIXME change it to the project's website --> <url>http://www.example.com</url> <!-- 核心启动器 --> <!-- 编码 --> <properties> <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> <java.version>1.8</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.5.14</version> <type>pom</type> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <version>2.5.14</version> </dependency> <dependency> <groupId>com.microsoft.ews-java-api</groupId> <artifactId>ews-java-api</artifactId> <version>2.0</version> </dependency> <dependency> <groupId>org.slf4j</groupId> <artifactId>slf4j-api</artifactId> <version>1.7.36</version> </dependency> </dependencies> </project>

最后

以上就是大胆鲜花最近收集整理的关于java EmailKit内嵌图片,带附件发送邮件的全部内容,更多相关java内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(41)

评论列表共有 0 条评论

立即
投稿
返回
顶部