key=new String(strKey.getBytes("UTF-8"),"GBK");或者内容做成htm格式,加个 sb.append("  <meta http-equiv='Content-Type' content='text/html; charset=GBK'>").append("\n");

解决方案 »

  1.   

    //设置邮件正文
        public boolean setBody(String mailBody) {
            try {
                BodyPart bp = new MimeBodyPart();
                bp.setContent(
                        "<meta http-equiv=Content-Type content='text/html; charset=GBK'>" +
                        mailBody, "text/html;charset=GBk");
                mp.addBodyPart(bp);            return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
      

  2.   

    package mail;import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;
    import java.io.*;public class SendMail {    private MimeMessage mimeMsg; //MIME邮件对象
        private Session session; //邮件会话对象
        private Properties props; //系统属性
        private String username = ""; //smtp认证用户名和密码
        private String password = "";
        private Multipart mp; //Multipart对象    public SendMail(String smtp) {
            setSmtpHost(smtp);
            createMimeMessage();
        }
        //设置发送邮件服务器
        public void setSmtpHost(String hostName) {
            System.out.println("设置系统属性:mail.smtp.host = " + hostName);
            if (props == null)
                props = System.getProperties(); //获得系统属性对象        props.put("mail.smtp.host", hostName); //设置SMTP主机
        }
        //创建邮件session
        public boolean createMimeMessage() {
            try {
                session = Session.getDefaultInstance(props, null); //获得邮件会话对象
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
            try {
                mimeMsg = new MimeMessage(session); //创建MIME邮件对象
                mp = new MimeMultipart();            return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        //是否发邮件需要验证
        public void setNeedAuth(boolean need) {
            if (props == null)
                props = System.getProperties();        if (need) {
                props.put("mail.smtp.auth", "true");
            } else {
                props.put("mail.smtp.auth", "false");
            }
        }
        //设置用户名 密码
        public void setNamePass(String name, String pass) {
            username = name;
            password = pass;
        }
        //设置邮件主题
        public boolean setSubject(String mailSubject) {
            System.out.println("设置邮件主题!");
            try {
                mimeMsg.setSubject(mailSubject);
                return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        //设置邮件正文
        public boolean setBody(String mailBody) {
            try {
                BodyPart bp = new MimeBodyPart();
                bp.setContent(
                        "<meta http-equiv=Content-Type content='text/html; charset=GBK'>" +
                        mailBody, "text/html;charset=GBk");
                mp.addBodyPart(bp);            return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        //添加附件
        public boolean addFileAffix(String filename) {        System.out.println("增加邮件附件:" + filename);        try {
                BodyPart bp = new MimeBodyPart();
                FileDataSource fileds = new FileDataSource(filename);
                bp.setDataHandler(new DataHandler(fileds));
                bp.setFileName(fileds.getName());            mp.addBodyPart(bp);            return true;
            } catch (Exception e) {
                e.printStackTrace();
                return false;
            }
        }
        //设置发件人信息
        public boolean setFrom(String from) {
            System.out.println("设置发信人!");
            try {
                mimeMsg.setFrom(new InternetAddress(from)); //设置发信人
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        //发送地址
        public boolean setTo(String to) {
            if (to == null)
                return false;        try {
                mimeMsg.setRecipients(Message.RecipientType.TO,
                                      InternetAddress.parse(to));
                return true;
            } catch (Exception e) {
                return false;
            }    }    //抄送一个地址
        public boolean setCopyTo(String copyto) {
            if (copyto == null)
                return false;
            try {
                mimeMsg.setRecipients(Message.RecipientType.CC,
                                      (Address[]) InternetAddress.parse(copyto));
                return true;
            } catch (Exception e) {
                return false;
            }
        }
        //发送邮件
        public boolean sendout() {
            try {
                mimeMsg.setContent(mp);
                mimeMsg.saveChanges();
                System.out.println("正在发送邮件....");
                Session mailSession = Session.getInstance(props, null);
                Transport transport = mailSession.getTransport("smtp");
                transport.connect((String) props.get("mail.smtp.host"), username,
                                  password);
                transport.sendMessage(mimeMsg,
                                      mimeMsg.getRecipients(Message.RecipientType.
                        TO));            System.out.println("发送邮件成功!");
                transport.close();            return true;
            } catch (Exception e) {
                System.err.println("邮件发送失败!" + e);
                return false;
            }
        }
        /**
         *  Just do it as this
         */
        public static void main(String[] args) {
            String mailbody = "hello,你好吗????这是中文邮件测试";
            SendMail themail = new SendMail("mail.tcaccp.com"); //设置smtp服务器
            themail.setNeedAuth(true); //服务器需要身份验证        if (themail.setSubject("标题") == false)
                return; //设置标题
            if (themail.setBody(mailbody) == false)
                return; //正文内容
            if (themail.setTo("[email protected]") == false)
                return; //收件人
            if (themail.setFrom("[email protected]") == false)
                return; //发件人
            if (themail.addFileAffix("C:\\tmp\\a.rar") == false)
                return; //附件   请使用绝对地址
            themail.setNamePass("[email protected]", "....."); //发件人的邮箱帐号,密码        if (themail.sendout() == false)
                return;
        }
    }
      

  3.   

    请问楼上,我测试了一下,你的程序邮件文本内容显示正常,但标题(subject)显示仍为乱码,请问是这样吗?
      

  4.   

    我在linux测试的,显示标题仍为乱码。
      

  5.   

    标题要注意编码,linux用什么编码的?
      

  6.   

    你的程序编码是什么?转下编码 key=new String(strKey.getBytes("GBK"),"UTF-8");
      

  7.   

    new String(strKey.getBytes("GBK"),"UTF-8"); 我试了,不行
    new String(strKey.getBytes("UTF-8"),"GBK"); 不行