概述
测试代码如下
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工具类,如下
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所需要的依赖,如下
<?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 EmailKit内嵌图片,带附件发送邮件所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复