<dependency>
<groupId>javax.mailgroupId>
<artifactId>mailartifactId>
<version>1.4.7version>
dependency>
<dependency>
<groupId>javax.activationgroupId>
<artifactId>activationartifactId>
<version>1.1.1version>
dependency>
163邮箱配置实例:
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", mailSmtpHost);
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.port",port);
gmail邮箱配置实例:
Properties properties = new Properties();
properties.setProperty("mail.transport.protocol", "smtp");
properties.setProperty("mail.smtp.host", mailSmtpHost);
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.port",port);
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.starttls.required", "true");
properties.setProperty("mail.smtp.ssl.enable", "true");
properties.setProperty("mail.smtp.auth", "true");
public static final String MAIL_TEMPLATE_CONTENT = "用户:%s
图片:
(1)、
";
Session session = Session.getInstance(properties);
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(mailAccount));
InternetAddress[] internetAddresses = InternetAddress.parse(receiveMaile);
message.setRecipients(Message.RecipientType.TO, internetAddresses);
message.setSubject("主题");
//设置邮件发送内容
message.setContent(createMimeMultipart(mailContentDTO));
message.saveChanges();
Transport transport = session.getTransport();
transport.connect(mailAccount, mailAccountPassword);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
public MimeMultipart createMimeMultipart(MailContentDTO mailContentDTO) throws MessagingException, UnsupportedEncodingException {
MimeMultipart mmTextImage = new MimeMultipart();
int picNum = 1;
for (MultipartFile multipartFile : mailContentDTO.getMultipartFiles()) {
File file1 = new File(sendmailLocaldirPath);
if (!file1.exists()) {
file1.mkdirs();
}
String fileName = multipartFile.getOriginalFilename();
if (fileName == null) {
continue;
}
log.info("fileName={}", fileName);
String fileNameEndPrefix = "";
if (fileName.contains(".")) {
fileNameEndPrefix = fileName.substring(fileName.lastIndexOf(".") - 1);
}
File file = new File(String.format(LOCAL_FILE_PATH, sendmailLocaldirPath, UUID.randomUUID().toString(), fileNameEndPrefix));
try (InputStream inputStream = multipartFile.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
ByteArrayOutputStream output = new ByteArrayOutputStream();
) {
byte[] buffer = new byte[1024];
int i = 0;
while ((i = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
fileOutputStream.write(output.toByteArray());
} catch (IOException e) {
log.error(e.getMessage(), e);
continue;
}
MimeBodyPart image = new MimeBodyPart();
image.setDataHandler(new DataHandler(new FileDataSource(file)));
image.setContentID("picture_view" + picNum);
mmTextImage.addBodyPart(image);
picNum++;
}
MimeBodyPart text = new MimeBodyPart();
text.setContent(mimeMessageHelper.setText(String.format(MAIL_TEMPLATE_CONTENT,"用户名"), "text/html;charset=UTF-8");
mmTextImage.addBodyPart(text);
mmTextImage.setSubType("related");
return mmTextImage;
}
<dependency>
<groupId>org.springframework.bootgroupId>
<artifactId>spring-boot-starter-mailartifactId>
dependency>
163邮箱配置实例:
spring:
mail:
host: smtp.163.com
password: xxxx
username: [email protected]
port: 25
recive:
mail: [email protected],[email protected]
gmail邮箱配置实例:
spring:
mail:
username: [email protected]
password: xxxxxxx
host: smtp.gmail.com
port: 465
properties:
mail:
smtp:
starttls:
enable: true
required: true
ssl:
enable: true
auth: true
recive:
mail: [email protected],[email protected],[email protected]
spring官方文档参考地址:
https://docs.spring.io/spring-boot/docs/current/reference/html/io.html#io.email
https://docs.spring.io/spring-framework/reference/integration/email.html
配置文件官方文档参考地址:
https://docs.spring.io/spring-boot/docs/current/reference/html/application-properties.html#appendix.application-properties.mail
public static final String MAIL_TEMPLATE_CONTENT = "用户:%s
图片:
(1)、
";
MimeMessage message = mailSender.createMimeMessage();
MimeMessageHelper mimeMessageHelper = new MimeMessageHelper(message, true);
mimeMessageHelper.setFrom(username);
mimeMessageHelper.setText(String.format(MAIL_TEMPLATE_CONTENT,"用户名"), true);
mimeMessageHelper.setSubject("主题");
//设置邮件发送内容
createMimeMultipart(mailContentDTO, mimeMessageHelper);
mimeMessageHelper.setTo(mailContentDTO.getUserMail());
mailSender.send(message);
mimeMessageHelper.setTo(receiveMaile.split(","));
mailSender.send(message);
public MimeMessageHelper createMimeMultipart(MailContentDTO mailContentDTO, MimeMessageHelper mimeMessageHelper)
throws MessagingException, UnsupportedEncodingException {
MimeMultipart mmTextImage = new MimeMultipart();
int picNum = 1;
for (MultipartFile multipartFile : mailContentDTO.getMultipartFiles()) {
File file1 = new File(sendmailLocaldirPath);
if (!file1.exists()) {
file1.mkdirs();
}
String fileName = multipartFile.getOriginalFilename();
if (fileName == null) {
continue;
}
log.info("fileName={}", fileName);
String fileNameEndPrefix = "";
if (fileName.contains(".")) {
fileNameEndPrefix = fileName.substring(fileName.lastIndexOf(".") - 1);
}
File file = new File(String.format(SendMailManager.LOCAL_FILE_PATH, sendmailLocaldirPath, UUID.randomUUID().toString(), fileNameEndPrefix));
try (InputStream inputStream = multipartFile.getInputStream();
FileOutputStream fileOutputStream = new FileOutputStream(file);
ByteArrayOutputStream output = new ByteArrayOutputStream();
) {
byte[] buffer = new byte[1024];
int i = 0;
while ((i = inputStream.read(buffer)) != -1) {
output.write(buffer, 0, i);
}
fileOutputStream.write(output.toByteArray());
} catch (IOException e) {
log.error(e.getMessage(), e);
continue;
}
mimeMessageHelper.addInline("picture_view" + picNum, new FileDataSource(file));
picNum++;
}
return mimeMessageHelper;
}