郵件傳送案例流的形式檔案不落盤

堕落先锋發表於2024-08-22

可以實際使用的

  public static void main(String[] args) {
        // 身份認證
        Session session = authorUser();
        // 收件人電子郵箱
        String from = "郵箱";
        // 發件人電子郵箱
        String to = "郵箱";
        send2User(session, from, to);
    }

    private static Session authorUser() {
        // 獲取系統屬性
        Properties properties = System.getProperties();
        // 指定傳送郵件的主機為 localhost
        String host = "xamail.byd.com";
        // 設定郵件伺服器
        properties.setProperty("mail.smtp.host", host);
        properties.put("mail.smtp.port", "25");
        properties.put("mail.smtp.auth", "true");
        // 判斷是否需要身份認證 如果需要身份認證,則建立一個密碼驗證器
        MailAuthenticator authenticator = new MailAuthenticator("郵箱", "123@123");
        // 根據郵件會話屬性和密碼驗證器構造一個傳送郵件的session
        Session session = Session.getDefaultInstance(properties, authenticator);
        return session;
    }

    private static void send2User(Session session, String from, String to) {
        try {
            // 建立預設的 MimeMessage 物件
            Message message = new MimeMessage(session);
            // Set From: 頭部頭欄位
            message.setFrom(new InternetAddress(from));
            // Set To: 頭部頭欄位
            message.addRecipient(Message.RecipientType.TO,
                    new InternetAddress(to));
            // Set Subject: 頭部頭欄位
            message.setSubject("某專案資料20240822");
            // Create a multipar message
            Multipart multipart = new MimeMultipart();
            // Create the message part
            BodyPart messageBodyPart = new MimeBodyPart();
            // Fill the message
            messageBodyPart.setText("Message test bym!!!");
            // Set text message part
            multipart.addBodyPart(messageBodyPart);

            // Part two is attachment 附件傳送
            messageBodyPart = new MimeBodyPart();

//            方式一傳送附件為檔案的形式
//            String filename = "D:\\test\\test.txt";
 //            DataSource source = new FileDataSource(filename);
//            messageBodyPart.setDataHandler(new DataHandler(source));
//            messageBodyPart.setFileName("test.txt");

//            方式二、傳送附件為流的形式,好處:資料直接查詢出來,不落檔案
            List<List<String>> head = head();
            List<List<Object>> lists = dataList();
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            EasyExcel.write(bos).sheet("sheet1").head(head).doWrite(lists);
            byte[] array = bos.toByteArray();
            ByteArrayDataSource byteArrayDataSource = new ByteArrayDataSource(array, "application/octet-stream");
            messageBodyPart.setDataHandler(new DataHandler(byteArrayDataSource));
            messageBodyPart.setFileName("test.xlsx");


            multipart.addBodyPart(messageBodyPart);

            // Send the complete message parts
            message.setContent(multipart);
            // 傳送訊息
            Transport.send(message);
            System.out.println("Sent message successfully....");
        } catch (MessagingException mex) {
            mex.printStackTrace();
        }
    }


    private static List<List<String>> head() {
        List<List<String>> list = com.alibaba.excel.util.ListUtils.newArrayList();
        List<String> head0 = com.alibaba.excel.util.ListUtils.newArrayList();
        head0.add("字串" + System.currentTimeMillis());
        List<String> head1 = com.alibaba.excel.util.ListUtils.newArrayList();
        head1.add("數字" + System.currentTimeMillis());
        List<String> head2 = com.alibaba.excel.util.ListUtils.newArrayList();
        head2.add("日期" + System.currentTimeMillis());
        list.add(head0);
        list.add(head1);
        list.add(head2);
        return list;
    }

    private static List<List<Object>> dataList() {
        List<List<Object>> list = com.alibaba.excel.util.ListUtils.newArrayList();
        for (int i = 0; i < 10; i++) {
            List<Object> data = com.alibaba.excel.util.ListUtils.newArrayList();
            data.add("字串" + i);
            data.add(0.56);
            data.add(new Date());
            list.add(data);
        }
        return list;
    }


相關文章