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
本主題所含的標籤
無標籤
作者 中文顯示問題,已設big5但還是亂碼,solaris系統
chialin0216





發文: 4
積分: 0
於 2014-02-14 10:11 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
程式內容是為了讀檔案,然後依檔案內容發送給不同寄件者與mail內容
程式內容如下,有人可以幫忙解答嗎??謝謝了
String fromLang = "BIG5";
String strSubject;

//讀入檔案
String inFile = "sendcontent.txt";

try{
File readfile = new File(inFile);
if (readfile.exists())
{
System.setProperty( "mail.mime.charset", "big5" );
// Create a default MimeMessage object.
MimeMessage message = new MimeMessage(session);

BufferedReader input = new BufferedReader(new FileReader(readfile));
String strContent;

// Create the message part
BodyPart messageBodyPart = new MimeBodyPart();

// Create a multipar message
Multipart multipart = new MimeMultipart();

while ((strContent = input.readLine()) != null)
{
while (!strContent.equals("END") && strContent != null)
{
String[] strArry = strContent.split(":");
//收件者
if (strArry[0].equals("TO"))
{
// Set To: header field of the header.
to = strArry[1];
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
}
//主旨、寄件者
else if (strArry[0].equals("SUBJECT"))
{
// Set From: header field of the header.
InternetAddress from = new InternetAddress(fromAdd, fromName, fromLang);
message.setFrom(from);

// Set Subject: header field
message.setSubject(strArry[1], "big5");
//message.setSubject(MimeUtility.encodeText(strArry[1], "big5", "B"));
//message.setHeader("Subject", MimeUtility.encodeText(strArry[1], "big5", "B"));
//message.setSubject(MimeUtility.encodeText(message.getSubject(), "big5", "B"));
System.out.println(strArry[1]);
}
//本文內容
else if (strArry[0].equals("TEXT"))
{
messageBodyPart = new MimeBodyPart();
//* Send the actual HTML message, as big as you like(HTML)
StringBuffer mailText = new StringBuffer("");
mailText.append(strArry[1]);
messageBodyPart.setContent(mailText.toString(), "text/html;charset=BIG5");
multipart.addBodyPart(messageBodyPart);
}
//附件
else if (strArry[0].equals("ATTFILE"))
{
messageBodyPart = new MimeBodyPart();
String filename = strArry[1];
DataSource source = new FileDataSource(filename);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(MimeUtility.encodeText(filename,"BIG5","B"));
multipart.addBodyPart(messageBodyPart);
}
strContent = input.readLine();
}
message.setContent(multipart);
// Send message
Transport.send(message);
message = new MimeMessage(session);
multipart = new MimeMultipart();
}
System.out.println("END結束");
}
}catch (MessagingException mex) {
mex.printStackTrace();
}catch (Exception e) {
e.printStackTrace();
}


reply to postreply to post
作者 Re:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:chialin0216]
jimwayne





發文: 220
積分: 0
於 2014-02-14 11:22 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
Java 工作者,但仍然是個初心者.....
作者 Re:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:jimwayne]
chialin0216





發文: 4
積分: 0
於 2014-02-14 11:24 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:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:chialin0216]
jimwayne





發文: 220
積分: 0
於 2014-02-14 11:46 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
chialin0216 wrote:
收到信件時,信件主旨、內容都是亂碼

先問個問題,為什麼想用 BIG5 而不用 UTF8 呢?


reply to postreply to post
Java 工作者,但仍然是個初心者.....
作者 Re:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:jimwayne]
chialin0216





發文: 4
積分: 0
於 2014-02-14 13: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
因為主要維護系統是老語言cobol,而cobol目前只能使用big5,
所以任何輔助的工具都會設成big5去處理


reply to postreply to post
作者 Re:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:chialin0216]
roytsang





發文: 619
積分: 1
於 2014-02-14 14:11 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
chialin0216 wrote:
程式內容是為了讀檔案,然後依檔案內容發送給不同寄件者與mail內容
程式內容如下,有人可以幫忙解答嗎??謝謝了
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
      String fromLang = "BIG5";
      String strSubject;
 
      //讀入檔案
      String inFile = "sendcontent.txt";
 
      try{
         File readfile = new File(inFile);
                 if (readfile.exists())
                 {
                          System.setProperty( "mail.mime.charset", "big5" ); 
                         // Create a default MimeMessage object.
                         MimeMessage message = new MimeMessage(session);
 
                         BufferedReader input = new BufferedReader(new FileReader(readfile));
                         String strContent;
 
                         // Create the message part
                         BodyPart messageBodyPart = new MimeBodyPart();
 
                         // Create a multipar message
                         Multipart multipart = new MimeMultipart();
 
                        while ((strContent = input.readLine()) != null)
                        {
                                while (!strContent.equals("END") && strContent != null)
                                {
                                        String[] strArry = strContent.split(":");
                                        //收件者
                                        if (strArry[0].equals("TO"))
                                        {
                                        // Set To: header field of the header.
                                         to = strArry[1];
                                         message.addRecipient(Message.RecipientType.TO,
                                                                                          new InternetAddress(to));
                                        }
                                        //主旨、寄件者
                                        else if (strArry[0].equals("SUBJECT"))
                                        {
                                         // Set From: header field of the header.
                                         InternetAddress from = new InternetAddress(fromAdd, fromName, fromLang);
                                         message.setFrom(from);
 
                                         // Set Subject: header field
                                         message.setSubject(strArry[1], "big5");
                                         //message.setSubject(MimeUtility.encodeText(strArry[1], "big5", "B"));
                                         //message.setHeader("Subject", MimeUtility.encodeText(strArry[1], "big5", "B"));
                                         //message.setSubject(MimeUtility.encodeText(message.getSubject(), "big5", "B"));
                                         System.out.println(strArry[1]);
                                        }
                                        //本文內容
                                        else if (strArry[0].equals("TEXT"))
                                        {
                                         messageBodyPart = new MimeBodyPart();
                                         //* Send the actual HTML message, as big as you like(HTML)
                                         StringBuffer mailText = new StringBuffer("");
                                         mailText.append(strArry[1]);
                                         messageBodyPart.setContent(mailText.toString(), "text/html;charset=BIG5");
                                         multipart.addBodyPart(messageBodyPart);
                                        }
                                        //附件
                                        else if (strArry[0].equals("ATTFILE"))
                                        {
                                         messageBodyPart = new MimeBodyPart();
                                         String filename = strArry[1];
                                         DataSource source = new FileDataSource(filename);
                                         messageBodyPart.setDataHandler(new DataHandler(source));
                                         messageBodyPart.setFileName(MimeUtility.encodeText(filename,"BIG5","B"));
                                         multipart.addBodyPart(messageBodyPart);
                                        }
                                        strContent = input.readLine();
                                }
                                 message.setContent(multipart);
                                 // Send message
                                 Transport.send(message);
                                 message = new MimeMessage(session);
                                 multipart = new MimeMultipart();
                        }
                         System.out.println("END結束");
                 }
          }catch (MessagingException mex) {
                 mex.printStackTrace();
          }catch (Exception e) {
                 e.printStackTrace();
          }
 


內容亂碼可以試試將15行,改成:
1
BufferedReader input =new InputStreamReader(new FileInputStream(readfile),"big5");


reply to postreply to post
作者 Re:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:chialin0216]
jimwayne





發文: 220
積分: 0
於 2014-02-14 14:27 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
chialin0216 wrote:
收到信件時,信件主旨、內容都是亂碼

那麼先確認一下,伺服器從檔案裡讀出來的內容是正確的 Big-5 吧
Java 預設在沒有特別宣告時,沒記錯是使用 UTF-16 作為預設編碼
所以如果把 UTF-16 的字串直接塞到郵件裡,但郵件卻宣告編碼為 Big-5,那會亂碼就不奇怪了~。


reply to postreply to post
Java 工作者,但仍然是個初心者.....
作者 Re:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:roytsang]
chialin0216





發文: 4
積分: 0
於 2014-02-14 15:18 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
BufferedReader input =new InputStreamReader(new FileInputStream(readfile),"big5");
照這個會出現incompatible type的訊息

不過solaris上的語系設為big5後能正常發送了,
雖然昨天有設過語系,但昨天試不行,所以就沒把語系寫到profile裡了,
只後加加減減一些東西後,沒有再設語系,感謝大家提供的意見,謝謝了!!


chialin0216 edited on 2014-02-14 15:22
reply to postreply to post
作者 Re:中文顯示問題,已設big5但還是亂碼,solaris系統 [Re:chialin0216]
ycpu





發文: 6
積分: 0
於 2014-03-26 23:29 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
BufferedReader input = new BufferedReader(new InputStreamReader(new FileInputStream (readfile), "BIG5"));
這樣應該就ok了!


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