1 当内容为html格式时,中文文字就是乱码了:不知道应该在哪里设置,我的代码如下:
    public void sendHtml()
        throws Exception
    {
     sun.misc.BASE64Encoder  enc= new sun.misc.BASE64Encoder();
        Properties props = System.getProperties();
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.host", host);
        Authenticator auth = new MyAuthenticator(user, password);
        Session session = Session.getInstance(props, auth);
        MimeMessage message = new MimeMessage(session);
        message.setFrom(new InternetAddress(from));
        message.addRecipient(javax.mail.Message.RecipientType.TO, new InternetAddress(to));
        if(bcc != null && !bcc.equals(""))
        {
            message.addRecipient(javax.mail.Message.RecipientType.BCC, new InternetAddress(bcc));
        }
        message.setSubject(subject,"gb2312");
        message.setDataHandler(new DataHandler(new StringDataSource(messageText, "text/html")));
        Transport.send(message);
    }

解决方案 »

  1.   

    Properties props = new Properties(); 
    Session sendMailSession; 
    Transport transport;
    MyAuthenticator myauth = new MyAuthenticator("邮件服务器用户名","邮件服务器密码");
    sendMailSession = Session.getInstance(props, myauth); 
    props.put("mail.smtp.host",smtphost); 
    props.put("mail.smtp.auth","true"); //这样才能通过验证Message newMessage = new MimeMessage(sendMailSession); 
    newMessage.setFrom(new InternetAddress(from)); 
    newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(to)); 
    newMessage.setSubject(subject); 
    newMessage.setSentDate(new Date()); 
    newMessage.setText(content); //给消息对象设置内容
    BodyPart mdp=new MimeBodyPart();//新建一个存放信件内容的BodyPart对象
    mdp.setContent(content,"text/html;charset=gb2312");//给BodyPart对象设置内容和格式/编码方式
    Multipart mm=new MimeMultipart();//新建一个MimeMultipart对象用来存放BodyPart对象(事实上可以存放多个)
    mm.addBodyPart(mdp);//将BodyPart加入到MimeMultipart对象中(可以加入多个BodyPart)
    newMessage.setContent(mm);//把mm作为消息对象的内容
    newMessage.saveChanges();
    transport = sendMailSession.getTransport("smtp"); 
    transport.send(newMessage); 
      

  2.   

    可能是因为你编码类型和html邮件的要显示用的编码不一致,比如
    html中的编码描述为:
    <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
    但是你邮件编码使用另外一种编码,那就会出错