邮箱发送,带附件,图片`
一、代码实现
package org.jeecg.modules.sendEmail.job;
import com.alibaba.excel.metadata.Sheet;
import com.sun.mail.util.MailSSLSocketFactory;
import com.sun.prism.Image;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.catalina.connector.Response;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.web.servlet.ShiroHttpServletResponse;
import org.jeecg.common.api.vo.Result;
import org.jeecg.modules.sendEmail.entity.Aging;
import org.jeecg.modules.sendEmail.entity.Boh;
import org.jeecg.modules.sendEmail.mapper.AgingMapper;
import org.jeecg.modules.utils.ExcelExportUtils;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.annotation.Resource;
import javax.imageio.stream.FileImageOutputStream;
import javax.mail.*;
import javax.mail.internet.*;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpServletResponseWrapper;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.GeneralSecurityException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Properties;
@Slf4j
@Component
public class SendMailService {
@Resource
private AgingMapper agingMapper;
@XxlJob(value = "sendEmailAging")
public ReturnT<String> sendEmailAging(String param){
String s = webPageToPicture("http://13.13.32.25:8091/pubhtmltoimg/?url=http://11.11.211.215:8080/webroot/decision/view/report?viewlet=.cpt&imgname=AGING","");
String imagePath = "D:/AGING/AGING.png";
getPicture("http://113.13.32.25:8091/pubFile/?filename=AGING.png","",imagePath);
toAgingExcel();
String fileName = "D:/AGING/AGING.xlsx";
String fileNameReal = "AGING.xlsx";
String imgName = "D:/AGING/AGING.png";
sendEmailWithPictureAndFileAging(fileName,fileNameReal,imgName);
return ReturnT.SUCCESS;
}
@XxlJob(value = "sendEmailBoh")
public ReturnT<String> sendEmailBoh(String param){
String s = webPageToPicture("http://13.13.32.25:8091/pubhtmltoimg/?url=http://13.13.32.25:8091/webroot/decision/view/report?viewlet=U09/U09/BOH_NEW_TEST.cpt&imgname=BOH","");
String imagePath = "D:/BOH/BOH.png";
getPicture("http://13.13.32.25:8091/pubFile/?filename=BOH.png","",imagePath);
toBohExcel();
String fileName = "D:/BOH/BOH.xlsx";
String fileNameReal = "BOH.xlsx";
String imgName = "D:/BOH/BOH.png";
sendEmailWithPictureAndFileBoh(fileName,fileNameReal,imgName);
return ReturnT.SUCCESS;
}
@XxlJob(value = "sendDailyIoReport")
public ReturnT<String> DailyIoReport(String param){
String s = webPageToPicture("http://13.13.32.25:8091/pubhtmltoimg/?url=http://10.10.10.215:8080/webroot/decision/view/report?viewlet=U09/U09/Customer/customer_mail.cpt&imgname=report","");
String imagePath = "D:/REPORT/report.png";
getPicture("http://13.13.32.25:8091/pubFile/?filename=report.png","",imagePath);
toAgingExcel();
String imgName = "D:/REPORT/report.png";
sendEmailDailyIoReport(imgName);
return ReturnT.SUCCESS;
}
public static String webPageToPicture(String path,String data) {
String str = "";
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
InputStream is = conn.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(is));
String s = "";
while ((s = br.readLine()) != null) {
str=new String(s.getBytes(),"UTF-8");
}
is.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
return str;
}
public static void getPicture(String path,String data,String imagePath) {
try {
URL url = new URL(path);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
PrintWriter out = null;
conn.setRequestProperty("accept", "*/*");
conn.setRequestProperty("connection", "Keep-Alive");
conn.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1)");
conn.setDoOutput(true);
conn.setDoInput(true);
conn.setRequestMethod("GET");
conn.connect();
InputStream is = conn.getInputStream();
ByteArrayOutputStream output = new ByteArrayOutputStream();
byte[] buffer = new byte[1024*4];
int n = 0;
while (-1 != (n = is.read(buffer))) {
output.write(buffer, 0, n);
}
byte[] bytes = output.toByteArray();
File file = new File(imagePath);
if (file.exists()) {
file.delete();
}
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(imagePath));
imageOutput.write(bytes, 0, bytes.length);
imageOutput.close();
System.out.println("Make Picture success,Please find image in " + imagePath);
is.close();
conn.disconnect();
} catch (Exception e) {
e.printStackTrace();
}
}
public void toAgingExcel(){
try {
String path = "D:/AGING/AGING.xlsx";
File file = new File(path);
if (file.exists()) {
file.delete();
}
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
Map columnWidth = new HashMap();
columnWidth.put(0, 8000);
String fileName = "AGING";
List<Aging> Aging = agingMapper.agingExportXlsx();
Sheet sheet1 = new Sheet(1, 0, Aging.class);
sheet1.setColumnWidthMap(columnWidth);
sheet1.setSheetName(fileName);
ExcelExportUtils.writeExcelOneSheet3(Aging,sheet1,fileName,path);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void toBohExcel(){
try {
String path = "D:/BOH/BOH.xlsx";
File file = new File(path);
if (file.exists()) {
file.delete();
}
File parentFile = file.getParentFile();
if (!parentFile.exists()) {
parentFile.mkdirs();
}
file.createNewFile();
Map columnWidth = new HashMap();
columnWidth.put(0, 8000);
String fileName = "BOH";
List<Boh> boh = agingMapper.bohExportXlsx();
Sheet sheet1 = new Sheet(1, 0, Boh.class);
sheet1.setColumnWidthMap(columnWidth);
sheet1.setSheetName(fileName);
ExcelExportUtils.writeExcelOneSheet3(boh,sheet1,fileName,path);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
public void sendEmailWithPictureAndFileAging(String fileName,String fileNameReal,String imgName) {
String from = "u09noreply.sys@goertek.com";
String host = "smtp.goertek.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("u09noreply.sys@goertek.com", "Hddb8442");
}
});
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
Address[] addresses = new Address[10];
addresses[0] = new InternetAddress("aileen.gai@goertek.com");
addresses[1] = new InternetAddress("cindy.wang@goertek.com");
addresses[2] = new InternetAddress("alan.zhaoyh@goertek.com");
addresses[3] = new InternetAddress("terry.wangs@goertek.com");
addresses[4] = new InternetAddress("owen.liuz@goertek.com");
addresses[5] = new InternetAddress("ebony.liu@goertek.com");
addresses[6] = new InternetAddress("zhijun.ding@goertek.com");
addresses[7] = new InternetAddress("dick.du@goertek.com");
addresses[8] = new InternetAddress("apple.jian@goertek.com");
addresses[9] = new InternetAddress("devin.jii@goertek.com");
message.addRecipients(Message.RecipientType.TO,addresses);
message.setSubject("Hollywood/Seacliff-Aging Kanban");
MimeBodyPart messageBodyPart = new MimeBodyPart();
MimeBodyPart img = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(new File(imgName)));
img.setDataHandler(dh);
img.setContentID("mailTestPic");
messageBodyPart.setContent("Dear Team:<br/>Below is Hollywood/Seacliff-Aging Kanban information, please kindly review, thanks.<br/>This email is automatically sent through system, please don't reply this email.<br/><img src='cid:mailTestPic'/></a>", "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(img);
messageBodyPart = new MimeBodyPart();
try {
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
try {
messageBodyPart.setFileName(MimeUtility.encodeText(fileNameReal));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception e) {
e.printStackTrace();
}
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
public void sendEmailWithPictureAndFileBoh(String fileName,String fileNameReal,String imgName) {
String from = "unoreply.sys@.com";
String host = "smtp.goertek.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("u09noreply.sys@goertek.com", "Hddb8442");
}
});
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
Address[] addresses = new Address[10];
addresses[0] = new InternetAddress("aileen.gai@.com");
addresses[1] = new InternetAddress("cindy.wang@.com");
addresses[2] = new InternetAddress("alan.zhaoyh@.com");
addresses[3] = new InternetAddress("terry.wangs@.com");
addresses[4] = new InternetAddress("owen.liuz@.com");
addresses[5] = new InternetAddress("ebony.liu@com");
addresses[6] = new InternetAddress("zhijun.ding@.com");
addresses[7] = new InternetAddress("dick.du@.com");
addresses[8] = new InternetAddress("apple.jian@.com");
addresses[9] = new InternetAddress("devin.jii@.com");
message.addRecipients(Message.RecipientType.TO,addresses);
message.setSubject("Hollywood/Seacliff-BOH Kanban");
MimeBodyPart messageBodyPart = new MimeBodyPart();
MimeBodyPart img = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(new File(imgName)));
img.setDataHandler(dh);
img.setContentID("mailTestPic");
messageBodyPart.setContent("Dear Team:<br/>Below is Hollywood/Seacliff-BOH Kanban information, please kindly review, thanks.<br/>This email is automatically sent through system, please don't reply this email.<br/><img src='cid:mailTestPic'/></a>", "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(img);
messageBodyPart = new MimeBodyPart();
try {
DataSource source = new FileDataSource(fileName);
messageBodyPart.setDataHandler(new DataHandler(source));
try {
messageBodyPart.setFileName(MimeUtility.encodeText(fileNameReal));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
multipart.addBodyPart(messageBodyPart);
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception e) {
e.printStackTrace();
}
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
public void sendEmailDailyIoReport(String imgName) {
String from = "u09noreply.sys@goertek.com";
String host = "smtp.goertek.com";
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
properties.put("mail.smtp.auth", "true");
MailSSLSocketFactory sf = null;
try {
sf = new MailSSLSocketFactory();
} catch (GeneralSecurityException e) {
e.printStackTrace();
}
sf.setTrustAllHosts(true);
properties.put("mail.smtp.ssl.enable", "true");
properties.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(properties,new Authenticator(){
public PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("u9noreply.sys@.com", "XXXXXXX");
}
});
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
Address[] addresses = new Address[2];
addresses[0] = new InternetAddress("devin.jii@goertek.com");
addresses[1] = new InternetAddress("zhuang.qiao@goertek.com");
message.addRecipients(Message.RecipientType.TO,addresses);
message.setSubject("Hollywood/Seacliff-Aging Kanban");
MimeBodyPart messageBodyPart = new MimeBodyPart();
MimeBodyPart img = new MimeBodyPart();
DataHandler dh = new DataHandler(new FileDataSource(new File(imgName)));
img.setDataHandler(dh);
img.setContentID("mailTestPic");
messageBodyPart.setContent("Dear Team:<br/>Below is Hollywood/Seacliff-Aging Kanban information, please kindly review, thanks.<br/>This email is automatically sent through system, please don't reply this email.<br/><img src='cid:mailTestPic'/></a>", "text/html;charset=UTF-8");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(img);
try {
message.setContent(multipart);
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (Exception e) {
e.printStackTrace();
}
}catch (MessagingException mex) {
mex.printStackTrace();
}
}
}