zhouxingyu пре 3 недеља
родитељ
комит
369f829afd

+ 1 - 1
lg-code/src/main/java/org/jeecg/modules/ProdPlan/service/impl/ProdPlanServiceImpl.java

@@ -1018,7 +1018,7 @@ public class ProdPlanServiceImpl extends ServiceImpl<ProdPlanMapper, ProdPlan> i
             workbook.write(outputStream);
             excelBytes = outputStream.toByteArray();
             workbook.close();
-            Boolean success = EmailUtil.sendEmailWithExcelAttachment(contactsTO, contactsCC, "生产异常追踪表_" + date, "生产异常追踪表_" + date, excelBytes);
+            Boolean success = EmailUtil.sendEmailWithPicAndExcelAttachment(contactsTO, contactsCC, "生产异常追踪表_" + date, "生产异常追踪表_" + date, pie, excelBytes);
             return success;
         } catch (IOException e) {
             throw new RuntimeException(e);

+ 106 - 0
lg-code/src/main/java/org/jeecg/modules/ProdPlan/util/EmailUtil.java

@@ -1,13 +1,25 @@
 package org.jeecg.modules.ProdPlan.util;
 
+import com.mchange.io.FileUtils;
 import com.sun.mail.util.MailSSLSocketFactory;
+import org.aspectj.util.FileUtil;
+import org.jeecg.common.util.DateUtils;
+import org.springframework.beans.factory.annotation.Value;
 
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.activation.FileDataSource;
 import javax.mail.*;
 import javax.mail.internet.InternetAddress;
 import javax.mail.internet.MimeBodyPart;
 import javax.mail.internet.MimeMessage;
 import javax.mail.internet.MimeMultipart;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
 import java.security.GeneralSecurityException;
+import java.util.Base64;
 import java.util.Date;
 import java.util.List;
 import java.util.Properties;
@@ -100,4 +112,98 @@ public class EmailUtil {
             return false;
         }
     }
+
+    /**
+     * 设置邮件,发送excel附件
+     *
+     * @param recipientTo 接收者
+     * @param recipientCc 抄送者
+     * @param subject   邮件标题
+     * @param content   邮件内容
+     * @param pie 图片内容
+     * @param excelBytes excel附件
+     */
+    public static Boolean sendEmailWithPicAndExcelAttachment(List<String> recipientTo, List<String> recipientCc, String subject, String content, byte[] pie, byte[] excelBytes) {
+        String username = "shudianfapiao@eguancloud.com";
+        String password = "TaHMQE2egJm5R5bg";
+        Properties prop = new Properties();
+        //协议
+        prop.setProperty("mail.transport.protocol", "smtp");
+        //服务器
+        prop.setProperty("mail.smtp.host", "smtp.exmail.qq.com");
+        //端口
+        prop.setProperty("mail.smtp.port", "465");
+        //使用smtp身份验证
+        prop.setProperty("mail.smtp.auth", "true");
+        //企业邮箱必须要SSL
+        prop.put("mail.smtp.ssl.enable", "true");
+        MailSSLSocketFactory sf = null;
+        try {
+            sf = new MailSSLSocketFactory();
+            sf.setTrustAllHosts(true);
+        } catch (GeneralSecurityException e1) {
+            e1.printStackTrace();
+        }
+        prop.put("mail.smtp.ssl.socketFactory", sf);
+        //获取Session对象
+        Session s = Session.getDefaultInstance(prop, new Authenticator() {
+            //此访求返回用户和密码的对象
+            @Override
+            protected PasswordAuthentication getPasswordAuthentication() {
+                PasswordAuthentication pa = new PasswordAuthentication(username, password);
+                return pa;
+            }
+        });
+        //设置session的调试模式,发布时取消
+        s.setDebug(true);
+        MimeMessage mimeMessage = new MimeMessage(s);
+        try {
+            mimeMessage.setFrom(new InternetAddress(username));//发送邮箱账号
+            //发送多个人
+            Address[] addressTo = new Address[recipientTo.size()];
+            for (int i = 0; i < recipientTo.size(); i++) {
+                addressTo[i] = new InternetAddress(recipientTo.get(i));
+            }
+            //抄送多个人
+            Address[] addressCc = new Address[recipientCc.size()];
+            for (int i = 0; i < recipientCc.size(); i++) {
+                addressCc[i] = new InternetAddress(recipientCc.get(i));
+            }
+
+            mimeMessage.addRecipients(Message.RecipientType.TO, addressTo);//接受者
+            mimeMessage.addRecipients(Message.RecipientType.CC, addressCc);//抄送
+            //设置主题
+            mimeMessage.setSubject(subject);//邮件标题
+            mimeMessage.setSentDate(new Date());
+
+            //设置内容
+            BodyPart messageBodyPart = new MimeBodyPart();
+            //String htmlText = content + "<img src='cid:image'>";
+            String htmlText = content + "<img src='data:image/png;base64," + Base64.getEncoder().encodeToString(pie);
+            messageBodyPart.setContent(htmlText,"text/html;charset=UTF-8");
+
+            //附件
+            MimeBodyPart attachmentPart = new MimeBodyPart();
+            attachmentPart.setFileName(subject + ".xlsx");
+            attachmentPart.setContent(excelBytes, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
+
+            // 创建多重消息
+            Multipart multipart = new MimeMultipart();
+            // 设置文本消息部分
+            multipart.addBodyPart(messageBodyPart);
+            multipart.addBodyPart(attachmentPart);
+
+            mimeMessage.setContent(multipart);
+            mimeMessage.saveChanges();
+            //发送
+            Transport.send(mimeMessage);
+
+            //imgFile.delete();
+
+            return true;
+        } catch (MessagingException e) {
+            e.printStackTrace();
+            return false;
+        }
+    }
 }