package com.ots.project.tool.email; import com.alibaba.fastjson.JSONObject; import lombok.extern.slf4j.Slf4j; import org.springframework.core.io.FileSystemResource; import org.springframework.http.*; import org.springframework.util.LinkedMultiValueMap; import org.springframework.util.MultiValueMap; import org.springframework.web.client.RestTemplate; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.*; import javax.mail.internet.*; import java.io.File; import java.io.UnsupportedEncodingException; import java.util.*; @Slf4j public class SimpleMailSender { public static void sendEmail(String SMTP, String PORT, String EMAIL, String PAW, String toEMAIL, String TITLE, String CONTENT, String TYPE) throws Exception { MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost(SMTP); mailInfo.setMailServerPort(PORT); mailInfo.setValidate(true); mailInfo.setUserName(EMAIL); mailInfo.setPassword(PAW); mailInfo.setFromAddress(EMAIL); mailInfo.setToAddress(toEMAIL); mailInfo.setSubject(TITLE); mailInfo.setContent(CONTENT); SimpleMailSender sms = new SimpleMailSender(); if ("1".equals(TYPE)) { sms.sendTextMail(mailInfo); } else { sms.sendHtmlMail(mailInfo); } } public static void main(String[] args) throws Exception { MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.mxhichina.com"); mailInfo.setMailServerPort("465"); mailInfo.setValidate(true); mailInfo.setUserName("test@tai-online.com"); mailInfo.setPassword("Wlz@2020"); mailInfo.setFromAddress("test@tai-online.com"); mailInfo.setToAddress("guobiao8900@126.com"); mailInfo.setSubject("测试发送邮件"); mailInfo.setContent("评测系统测试邀请链接http://139.199.11.114/exam-stu/#/demography/ed860660-95a7-4feb-aab9-f56b1eff1a3c/12/index"); String[] attachFileNames = {"D:\\ots\\uploadPath\\upload\\94a3847d-901a-4063-9d2d-ef3a345f46d0.png", "D:\\ots\\uploadPath\\upload\\94a3847d-901a-4063-9d2d-ef3a345f46d0.png"}; mailInfo.setAttachFileNames(attachFileNames); SimpleMailSender sms = new SimpleMailSender(); sms.sendTextMail(mailInfo); } public RushMailResult sendPushMail(RushMailInfo into) { RestTemplate infoRestTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap param = new LinkedMultiValueMap<>(); if (Objects.nonNull(into.getAttachFileNames())) { String[] attachFileNames = into.getAttachFileNames(); for (int i = 0; i < attachFileNames.length; i++) { File file = new File(attachFileNames[i]); FileSystemResource resource = new FileSystemResource(file); param.add(file.getName(), resource); } } param.add("account", into.getAccount()); param.add("receivename", "receivename"); param.add("toemail", into.getEmails()); param.add("title", into.getTitle()); param.add("content", into.getContext()); HttpEntity> entity = new HttpEntity<>(param, headers); RushMailResult rushMailResult = new RushMailResult(); try { ResponseEntity response = infoRestTemplate.exchange(into.getUrl(), HttpMethod.POST, entity, JSONObject.class); JSONObject responseBody = response.getBody(); rushMailResult = JSONObject.toJavaObject(responseBody, RushMailResult.class); } catch (Exception e) { rushMailResult.setResult(false); rushMailResult.setMessage(e.getMessage()); log.error("sendPushMail:{}",e.getMessage(), e); } return rushMailResult; } public RushMailResultCheck mailResult (String url, String account, String date) { RestTemplate infoRestTemplate = new RestTemplate(); HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.MULTIPART_FORM_DATA); MultiValueMap param = new LinkedMultiValueMap<>(); param.add("account", account); param.add("date", date); HttpEntity> entity = new HttpEntity<>(param, headers); ResponseEntity response = null; RushMailResultCheck check = new RushMailResultCheck(); try { response = infoRestTemplate.exchange(url, HttpMethod.POST, entity, JSONObject.class); JSONObject responseBody = response.getBody(); System.out.println(responseBody); check = JSONObject.toJavaObject(responseBody, RushMailResultCheck.class); } catch (Exception e) { check.setResult(false); check.setMessage(e.getMessage()); } return check; } public boolean sendTextMail (MailSenderInfo mailInfo) throws Exception { Message mailMessage = initMailMessage(mailInfo); return true; } public boolean sendHtmlMail(MailSenderInfo mailInfo) throws Exception { Message mailMessage = initMailMessage(mailInfo); Multipart mainPart = new MimeMultipart(); BodyPart html = new MimeBodyPart(); html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); mainPart.addBodyPart(html); mailMessage.setContent(mainPart); Transport.send(mailMessage); return true; } private Message initMailMessage(MailSenderInfo mailInfo) throws MessagingException { MyAuthenticator authenticator = null; Properties pro = mailInfo.getProperties(); if (mailInfo.isValidate()) { authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); } Session sendMailSession = Session.getDefaultInstance(pro, authenticator); Message mailMessage = new MimeMessage(sendMailSession); Address from = new InternetAddress(mailInfo.getFromAddress()); mailMessage.setFrom(from); Address to = new InternetAddress(mailInfo.getToAddress()); mailMessage.setRecipient(Message.RecipientType.TO, to); mailMessage.setSubject(mailInfo.getSubject()); mailMessage.setSentDate(new Date()); String mailContent = mailInfo.getContent(); MimeMultipart mm_text_image = new MimeMultipart(); MimeBodyPart text = new MimeBodyPart(); text.setContent(mailContent, "text/html;charset=UTF-8"); mm_text_image.addBodyPart(text); mm_text_image.setSubType("related"); MimeBodyPart text_image = new MimeBodyPart(); text_image.setContent(mm_text_image); MimeMultipart mm = new MimeMultipart(); mm.addBodyPart(text_image); String[] attachFileNames = mailInfo.getAttachFileNames(); if (Objects.nonNull(attachFileNames) && attachFileNames.length > 0) { for (int i = 0; i < attachFileNames.length; i++) { MimeBodyPart attachment = getMimeBodyPart(attachFileNames[i]); mm.addBodyPart(attachment); } mm.setSubType("mixed"); } mailMessage.setContent(mm); Transport transport = sendMailSession.getTransport(); transport.connect(mailInfo.getUserName(), mailInfo.getPassword()); Address[] allRecipients = mailMessage.getAllRecipients(); transport.sendMessage(mailMessage, allRecipients); transport.close(); return mailMessage; } private MimeBodyPart getMimeBodyPart(String name) throws MessagingException { MimeBodyPart attachment = new MimeBodyPart(); DataHandler dh2 = new DataHandler(new FileDataSource(name)); attachment.setDataHandler(dh2); try { attachment.setFileName(MimeUtility.encodeText(dh2.getName())); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return attachment; } public boolean sendMail(String title, String content, String type, String tomail) throws Exception { MailSenderInfo mailInfo = new MailSenderInfo(); mailInfo.setMailServerHost("smtp.qq.com"); mailInfo.setMailServerPort("25"); mailInfo.setValidate(true); mailInfo.setUserName("itfather@1b23.com"); mailInfo.setPassword("tttt"); mailInfo.setFromAddress("itfather@1b23.com"); mailInfo.setToAddress(tomail); mailInfo.setSubject(title); mailInfo.setContent(content); SimpleMailSender sms = new SimpleMailSender(); if ("1".equals(type)) { return sms.sendTextMail(mailInfo); } else if ("2".equals(type)) { return sms.sendHtmlMail(mailInfo); } return false; } }