在JSP开发中,在网页上可以显示中文,但在用邮件发送后, 到 邮件接收人那里出来 的乱码,请问各位大侠如何解决

解决方案 »

  1.   

    在接收页面开始加一句request.setCharEncoding("gb2312");
    试看看
      

  2.   

    应该是编码的问题,如果邮件内容是unicode字符,对方必须给自己的浏览器设置为utf-8编码
      

  3.   

    public void postMail(String to,String sb,String con) 
     throws MessagingException,Exception 
     { 
    String[] recipients = new String[1];
        recipients[0] = to;
    if (recipients != null && recipients.length > 0) 
        { 
    /*String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
    Properties props = System.getProperties();
    props.setProperty("mail.smtp.host", "smtp.gmail.com");
    props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
    props.setProperty("mail.smtp.socketFactory.fallback", "false");
    props.setProperty("mail.smtp.port", "465");
    props.setProperty("mail.smtp.socketFactory.port", "465");
    props.put("mail.smtp.auth", "true");

    PopupAuthenticator pauth = new PopupAuthenticator("[email protected]", "313131");
    java.security.Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
    Session session = Session.getInstance(props,pauth);
    Message msg = new MimeMessage(session);
    */
    String contentType = "html";
    Properties props = new Properties();
         props.put("mail.smtp.host","smtp.163.com");
         props.put("mail.smtp.auth","true");
    props.put("mail.smtp.user", "[email protected]"); 
    props.put("mail.smtp.password", "61664774"); 
    Session session = Session.getInstance(props, new PopupAuthenticator("[email protected]", "61664774"));
    Message msg = new MimeMessage(session);
    InternetAddress addressFrom = new InternetAddress("[email protected]");msg.setFrom(addressFrom);
    InternetAddress[] addressTo = new InternetAddress[recipients.length];
    for (int i = 0; i < recipients.length; i++) 
    {
    addressTo[i] = new InternetAddress(recipients[i]);
    }
    msg.setRecipients(Message.RecipientType.TO, addressTo);
    // 设置邮件标题,中文编码
    sb = MimeUtility.encodeText(sb, "GBK", "B");
    msg.setSubject(sb);
    if (contentType == null || contentType.equals("text")) 
    {
    msg.setText(con);

    else if (contentType.equals("html")) 
    {
    BodyPart bodyPart1 = new MimeBodyPart();
    bodyPart1.setContent(con, "text/html;charset=gbk");
    Multipart mmp = new MimeMultipart();
    mmp.addBodyPart(bodyPart1);
    msg.setContent(mmp);
    }
    msg.setSentDate(new java.util.Date());
    Transport.send(msg);
        } 
    }
      

  4.   

    这个是我发邮件的一个方法。注释部分是采用gmail服务器