我正在使用Java获取邮件消息并将该消息保留到文件中。当我回读文件时,遇到了javax.mail.internet.AddressException
下面是我用来写消息的代码
Properties mailProps = System.getProperties();
mailProps.setProperty("mail.store.protocol", "imaps");
Session session = null;
Folder inbox = null;
Store store = null;
Message[] messages = null;
try {
session = Session.getDefaultInstance(mailProps, null);
store = session.getStore("imaps");
store.connect(config.getMailHost(), config.getMailUserId(), config.getMailPasswd());
inbox = store.getFolder("inbox");
inbox.open(Folder.READ_WRITE); // Folder.READ_ONLY
logger.info("Total Messages found on Host: <{}>", inbox.getMessageCount());
logger.info("Total Unread Messages on Host: <{}>", inbox.getUnreadMessageCount());
messages = inbox.getMessages();
for (Message currMessage : messages) {
File mailFile = buildMailFileName();
currMessage.writeTo(new FileOutputStream(mailFile));
}
}
我可以看到该文件已创建,并且包含以下行:
收件人:[email protected];
当我回读文件时,出现以下错误:
javax.mail.internet.AddressException:非法分号,不在字符串'Name @ domain中。
这是我用来读取文件的代码:
Session session = Session.getDefaultInstance(mailProps, null);
FileInputStream fis = new FileInputStream(mailFile);
MimeMessage mime = new MimeMessage(null, fis);
Address[] addresses = mime.getRecipients(Message.RecipientType.TO);
最佳答案
不允许使用分号作为电子邮件地址的分隔符,请使用逗号代替!