0

我在使用 javamail 从 gmail (pop3) 读取邮件时遇到问题。如果邮件是从 ubuntu 的 Thnderbird 发送的,我有一个代码可以完美运行。但是,如果邮件最初是从 mac 发送的,它会失败。

这是我使用的代码:

    private static final String UNKNOWN_BRAND_PATH = "UNKNOWN";
public static final String FOLDER_NAME = "INBOX";

private static Logger LOG = org.slf4j.LoggerFactory.getLogger(LzMailRecieverService.class);

@Value("${lz.mail.address}")
private String lzMailUserName;

@Value("${lz.mail.password}")
private String lzMailPassword;

@Value("${lz.mail.tmp.folder}")
private String lzMailTmpFolder;

public Store connect() throws Exception {

    String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";

    Properties pop3Props = new Properties();

    pop3Props.setProperty("mail.pop3.socketFactory.class", SSL_FACTORY);
    pop3Props.setProperty("mail.pop3.socketFactory.fallback", "false");
    pop3Props.setProperty("mail.pop3.port", "995");
    pop3Props.setProperty("mail.pop3.socketFactory.port", "995");

    URLName url = new URLName("pop3", "pop.gmail.com", 995, "", lzMailUserName, lzMailPassword);

    Session session = Session.getInstance(pop3Props, null);
    Store store = new POP3SSLStore(session, url);
    store.connect();
    return store;
}

public Folder openFolder(Store store) throws MessagingException {
    Folder folder = store.getDefaultFolder();
    folder = folder.getFolder(FOLDER_NAME);
    folder.open(Folder.READ_ONLY);
    return folder;
}

public List<MailDetails> readAttachement(Folder folder) throws IOException, MessagingException {
    Message[] messages = folder.getMessages();
    List<MailDetails> mailDetails = new ArrayList<MailDetails>();
    for (Message message : messages) {
        logMailDetails(message);
        if (message.getContent() instanceof Multipart) {
            Multipart multipart = (Multipart) message.getContent();
            for (int i = 0; i < multipart.getCount(); i++) {
                BodyPart bodyPart = multipart.getBodyPart(i);
                if (!Part.ATTACHMENT.equalsIgnoreCase(bodyPart.getDisposition())) {

                    continue; // dealing with attachments only
                }
                InputStream is = bodyPart.getInputStream();
                String uid = getUid(message);
                String to = getTo(message);
                String from = getFrom(message);
                File d = new File(lzMailTmpFolder + File.separator + uid);
                if (d.exists() == false) {
                    d.mkdir();
                }
                File f = new File(d, new DateTime().getMillis() + "-" + bodyPart.getFileName());
                FileOutputStream fos = new FileOutputStream(f);
                IOUtils.copy(is, fos);
                MailDetails md = new MailDetails(to, from, f, uid);
                mailDetails.add(md);
            }
        }
        else {
            LOG.warn("Message conteant is not Multipart " + message.getContentType() + " skipping ...");
        }
    }
    return mailDetails;
}

private String getFrom(Message message) throws MessagingException {
    Address[] froms = message.getFrom();
    return froms[0].toString();
}

private String getTo(Message message) throws MessagingException {
    Address[] tos = message.getAllRecipients();
    return tos[0].toString();
}

public void logMailDetails(Message m) throws MessagingException {
    Address[] f = m.getFrom();
    if (f != null) {
        for (int j = 0; j < f.length; j++)
            LOG.debug("FROM: " + f[j].toString());
    }

    Address[] r = m.getRecipients(Message.RecipientType.TO);
    if (r != null) {
        for (int j = 0; j < r.length; j++) {
            LOG.debug("TO: " + r[j].toString());
        }
    }
    LOG.debug("SUBJECT: " + m.getSubject());
    Date d = m.getSentDate();
    LOG.debug("SendDate: " + d);
}

private String getUid(Message m) throws MessagingException {
    try {
        Address[] tos = m.getAllRecipients();
        String to = tos[0].toString();
        to = to.split("@")[0];
        String[] parts = to.split("\\+");
        return parts[parts.length - 1];
    }
    catch (Exception e) {
        LOG.error("Failes to extract brand hash from email address " + Lists.newArrayList(m.getFrom()));
        return UNKNOWN_BRAND_PATH;
    }
}

问题是最初在 mac bodyPart.getDisposition() 中创建的邮件总是返回 null。无论我尝试了什么,我都无法理解哪个部分是附件部分(这是我真正需要的:从邮件中提取附件)。

我浏览了整个网络以找到原因,但我未能找到答案。我是怎么发现 Juergen Hoeller 写的下面的注释表明这里可能存在问题(更多细节在这里:http ://static.springsource.org/spring/docs/3.0.x/javadoc-api/org/springframework /mail/javamail/MimeMessageHelper.html )

关于多部分邮件的警告:仅包含 HTML 文本但没有内联元素或附件的简单 MIME 消息将或多或少适用于任何能够呈现 HTML 的电子邮件客户端。然而,内联元素和附件仍然是电子邮件客户端之间的一个主要兼容性问题:几乎不可能让内联元素和附件在 Microsoft Outlook、Lotus Notes 和 Mac Mail 上工作。考虑为您的需要选择特定的多部分模式: MULTIPART_MODE 常量上的 javadoc 包含更详细的信息。

如果邮件是从 Mac 发送的,是否有关于使用 JavaMail 的示例或说明?

约西

4

1 回答 1

0

“性情”充其量只是一个暗示;它不需要包括在内。

这些 JavaMail FAQ 条目可能会有所帮助:

于 2013-02-28T23:21:18.880 回答