这两天在弄邮件群发的项目。下面是邮件接收的代码。
public List<MailsForm> getMails(SetForm setForm)
{

String hostmail = setForm.getHostmail();
String password = setForm.getPassword();
String user = hostmail.substring(0,hostmail.indexOf('@'));
System.out.println("hostmail: "+hostmail);
System.out.println("user: "+user);
String mailserver ="pop3."+hostmail.substring(hostmail.indexOf('@')+1,hostmail.length());

Properties prop = new Properties();
prop.setProperty("mail.store.protocol", "pop3");
prop.setProperty("mail.pop3.host", mailserver);
Session mailSession = Session.getDefaultInstance(prop,null);
mailSession.setDebug(false);
List<MailsForm> list =new ArrayList<MailsForm>();
try {


Store store = mailSession.getStore("pop3");
store.connect(mailserver, user, password);
Folder folder = store.getFolder("inbox");
folder.open(Folder.READ_WRITE);
Message messages[] = folder.getMessages();

MailsForm mails = new MailsForm();
String from = "";
String subject = "";
String sendDate = "";
for(int i = 0;i<messages.length;i++)
{ from = messages[i].getFrom()[0].toString();
subject = messages[i].getSubject();
sendDate = messages[i].getSentDate().toLocaleString();

Object content = messages[i].getContent();

System.out.println("from: "+from);
System.out.println("subject: "+subject);
Part messagePart = messages[i];  if (content instanceof Multipart) {
       messagePart = ((Multipart)content).getBodyPart(0);
      } String contentType = messagePart.getContentType();
      System.out.println("发送类型:"+contentType);
      String thisLine = null;
      StringBuffer str = new StringBuffer();
      if (contentType.startsWith("text/plain")
        || contentType.startsWith("text/html")) {
       InputStream is = messagePart.getInputStream();
       BufferedReader reader = new BufferedReader(
         new InputStreamReader(is));
       thisLine = reader.readLine();
    
       while (thisLine != null) {
        str.append(thisLine);
        thisLine = reader.readLine();
     
       }
       
      }
       mails.setMailfrom(ChStr.toChinese(from));
mails.setSubject(subject);
mails.setSendtime(ChStr.toChinese(sendDate));
mails.setContent(str.toString());

list.add(mails);

}


} catch (Exception e) {
e.printStackTrace();
}

return list;

}
我遍历list的时候结果list中的数据都是同一个数据。数据数量是邮件的数量,可是数据都是同一封邮件的数据。很奇怪。
我在控制台看数据:发现没有问题。
DEBUG POP3: server doesn't support TOP, disabling it
from: kissyou39 <[email protected]>
subject: 新的主题2
发送类型:text/plain; charset=gbk
from: kissyou39 <[email protected]>
subject: 新的主题2
发送类型:text/plain; charset=gbk
from: [email protected]
subject: ddd
发送类型:text/html;charset=gb2312
from: [email protected]
subject: 道德经