现在因为需要用到javamai编写一个可以收发邮件的程序,我网上看了很久,把发邮件差不多搞好了,但是收邮件的时候会有很多的问题,我现在碰到一个满严重的问题,就是如果是中文邮件的话,正文是乱码。主题如果是中午的话,却可以正常显示。我试了编码改成UTF8貌似也没用只是从方块变成了问号。。高人来指点下我吧~以下是我的代码
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Folder;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.NoSuchProviderException;
import javax.mail.Part;
import javax.mail.Session;
import javax.mail.Store;public class ReceiveEmail {
public static void receive() {
Properties props = new Properties();
//存储接收邮件服务器使用的协议,这里以POP3为例
props.setProperty("mail.store.protocol", "pop3");
//设置接收邮件服务器的地址,这里还是以网易163为例
props.setProperty("mail.pop3.host", "pop3.sina.cn");
//根据属性新建一个邮件会话.
Session session=Session.getInstance(props);

try {
//从会话对象中获得POP3协议的Store对象
Store store = session.getStore("pop3");

//如果需要查看接收邮件的详细信息,需要设置Debug标志
session.setDebug(false);

//连接邮件服务器
store.connect("pop3.sina.cn", "用户名", "密码");

//获取邮件服务器的收件箱
Folder folder = store.getFolder("INBOX");
//以只读权限打开收件箱
folder.open(Folder.READ_ONLY);

//获取收件箱中的邮件,也可以使用getMessage(int 邮件的编号)来获取具体某一封邮件
Message[] message = folder.getMessages();
for (int i=0; i < message.length; i++) {
Part messPart = message[i];
Object content = message[i].getContent();
messPart = ((Multipart)content).getBodyPart(0);
InputStream   in   =   messPart.getInputStream(); 
                BufferedReader   reader   =   new   BufferedReader(new   InputStreamReader(in)); 
                String   str   =   reader.readLine(); 
                
                while (str!=null){
System.out.println(str);
str=reader.readLine();
}
}
//关闭连接
folder.close(false);
store.close();
} catch (NoSuchProviderException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MessagingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
  
}

 public static void main(String[] args) {
receive();
System.out.println("ok");
}
}

解决方案 »

  1.   

    发送时候 最好放在body里面 可以防止乱码出现  这是我的JAVA MAIL 你可以看看
    public class SendMail {
    private String toMail;

    public static void main(String[] args) throws Exception {

    new SendMail().send("[email protected]");
    } public void send(String toMail)throws Exception{
    //发出邮箱
    String fromMail = "[email protected]";
    //发送邮箱
    //String toMail = "[email protected]";
    //主题
    String subject = "等待审批";
    //内容
    String body = "请即时处理等待审批流程"; Properties props = System.getProperties(); // 设置SMTP邮件服务器:
    props.put("mail.smtp.host", "smtp.163.com");
    // SMTP服务器需要验证:
    props.put("mail.smtp.auth", "true"); // 传入用户名和口令:
    Session session = Session.getDefaultInstance(props,new PasswordAuthenticator("luwei2it", "weiwei")); // 创建新邮件:
    Message msg = new MimeMessage(session);
    msg.setFrom(new InternetAddress(fromMail));
    msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toMail, false));
    msg.setSubject(subject);
    msg.setText(body);
    msg.setSentDate(new Date()); // 发送:
    Transport.send(msg);
    } public String getToMail() {
    return toMail;
    } public void setToMail(String toMail) {
    this.toMail = toMail;
    }
    }
    8319449高级JAVA交流群