作者graceliu (累)
看板java
標題[問題] javamail已設big5但還是亂碼,solaris系統
時間Fri Feb 14 10:48:14 2014
程式內容是為了讀檔案,然後依檔案內容發送給不同寄件者與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();
}
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 115.80.39.159
1F:→ Lordaeron:你file 上來就編碼不對了吧, 讀檔可以設定編碼的. 02/14 11:16
2F:推 PsMonkey:推樓上,另外這種問題最好補 client 環境 & 設定 02/14 11:46
3F:→ swpoker:這種大概是開發都在windows環境~然後一上線就有問題 02/14 15:50
4F:→ Lordaeron:unix上JAVAAP讀檔的預設編碼=locale的設定. 02/15 01:01
5F:→ graceliu:s大請問一上線就有問題是可能什麼樣的問題啊? 02/15 12:21
6F:→ graceliu:L大,在solaris上語系設為BIG5即解決了,謝謝!! 02/15 12:23
7F:→ Lordaeron:基本上,並不建議動locale,即跑該JAVA AP的ENV的LOCALE 02/16 08:38