JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » EJB / 其它Java EE 討論區 » JavaMail  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友   
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 Java Mail排程
hygravity





發文: 3
積分: 0
於 2017-06-06 10:33 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
各位大家好~

想請教有關於JavaMail排程的問題

目前我遇到的問題是:

1. 當特定時機到時,會自動發出通知信給使用者。(這部分問題解決差不多了)
2. 但是通知信中內容需要將資料庫中指定欄位轉換成xls檔案,再附加於通知信中寄給使用者

主要是第二個問題我不知道該如何解決。

有想到是將產生xls程式和排程程式分開寫,但又怕產生xls會造成系統負擔,謝謝。


reply to postreply to post
作者 Re:Java Mail排程 [Re:hygravity]
roytsang





發文: 619
積分: 1
於 2017-06-07 02:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
可否產生csv 來取代產生XLS 呢?

reply to postreply to post
作者 Re:Java Mail排程 [Re:roytsang]
hygravity





發文: 3
積分: 0
於 2017-06-07 05:57 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
這我倒沒有想過...

reply to postreply to post
作者 Re:Java Mail排程 [Re:hygravity]
kentyeh





發文: 643
積分: 6
於 2017-06-08 09:30 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
有時侯信件夾xls可能會因為太大而被mail server拒絕,
所以通常我都是把xls先壓縮再夾檔,
以前產生xls我都是用jxl,後來發現產出的xls有時用POI讀回來會有問題,
所以慢慢就改用POI了,以下範例自行參考
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public static void main(String[] args) throws Exception {
    ByteArrayOutputStream mailBaos = new ByteArrayOutputStream();
    try (ByteArrayOutputStream mailBaos = new ByteArrayOutputStream();
            ZipOutputStream zos = new ZipOutputStream(new BufferedOutputStream(mailBaos));
            HSSFWorkbook wb = new HSSFWorkbook()) {
        org.apache.poi.ss.usermodel.Font font = wb.createFont();
        font.setFontHeightInPoints((short) 11);
        org.apache.poi.ss.usermodel.CellStyle style = wb.createCellStyle();
        style.setFont(font);
        org.apache.poi.ss.usermodel.DataFormat format = wb.createDataFormat();
        style.setDataFormat(format.getFormat("#,##0.#"));
        Sheet sheet = wb.createSheet("第一頁");
        org.apache.poi.ss.usermodel.Row row = sheet.createRow(0);
        org.apache.poi.ss.usermodel.Cell cell = row.createCell(0);//A1欄位
        cell.setCellStyle(style);
        cell.setCellValue(1234);
        cell = row.createCell(1);//建立公式欄位
        cell.setCellStyle(style);
        cell.setCellType(HSSFCell.CELL_TYPE_FORMULA);
        cell.setCellFormula("A1");
        //建立壓縮
        ZipEntry ze = new ZipEntry("report.xls");//最好不用中文,平台編碼不同會造成問題
        zos.putNextEntry(ze);
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        wb.write(bos);
        ze.setSize(bos.size());
        zos.write(bos.toByteArray());
        zos.closeEntry();
        //開始郵寄
        MimeMessage mail = new MimeMessage(getMailSessionFromSomeWhere());
        mail.setFrom();
        mail.setRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress("someone@comany.com"));
        mail.setSubject("郵寄Excel報表,請自行解壓縮");
        MimeBodyPart bodyPart = new MimeBodyPart();
        ByteArrayDataSource source = new ByteArrayDataSource(mailBaos.toByteArray(), "application/zip");
        bodyPart.setDataHandler(new DataHandler(source));
        bodyPart.setFileName(MimeUtility.encodeWord("你的報表.zip"));
        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(bodyPart);
        mail.setContent(multipart);
        Transport.send(mail);
    }
}


reply to postreply to post
作者 Re:Java Mail排程 [Re:kentyeh]
hygravity





發文: 3
積分: 0
於 2017-06-09 08:48 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
謝謝!!

reply to postreply to post
» JWorld@TW »  EJB / 其它Java EE 討論區 » JavaMail

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8