我的mail: [email protected]

解决方案 »

  1.   

    附件的中文名问题是javamail的bug,他忘了处理编码了你自己分析那个字符串,然后赋值就好了String xxx = yourcode(fileName);
    //bodyPart2.setFileName(fileDataSource.getName());
    bodyPart2.setFileName(xxx);这样应该可以了
      

  2.   


            String fileName = "e:/temp/中文.htm";
            
            //这里比较一下就知道了,如果设置为fileName2,文件名就对了,但是却无法找到文件了,所以这样不行
            String fileName2 = new String(fileName.getBytes("GBK"),"ISO-8859-1");
            System.out.println(fileName); 
            System.out.println(fileName2);
            //fileName = fileName2;        Properties aProperties = System.getProperties();
            aProperties.put("mail.transport.protocol","smtp");
            aProperties.put("mail.smtp.host","smtp.xxx.com");        Session session = Session.getInstance(aProperties,null);        MimeMessage message = new MimeMessage(session);        message.setFrom(new InternetAddress("[email protected]"));
            message.setRecipient(Message.RecipientType.TO,new InternetAddress("[email protected]"));        message.setSubject("Hello,中文");        BodyPart bodyPart1 = new MimeBodyPart();        bodyPart1.setText("Hello,中文");        BodyPart bodyPart2 = new MimeBodyPart();        FileDataSource fileDataSource = new FileDataSource(fileName);
            bodyPart2.setDataHandler(new DataHandler(fileDataSource));
            
            //bodyPart2.setFileName(fileDataSource.getName());
            String fn2 = getFileName (fileName); //这个自己去实现,应该很简单吧
            bodyPart2.setFileName(fn2);        Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(bodyPart1);
            multipart.addBodyPart(bodyPart2);        message.setContent(multipart);        message.saveChanges();
            message.writeTo(System.out);        Transport.send(message);