如何使用Java邮件获取未答复的邮件?

时间:2013-03-07 11:38:38

标签: java email javamail flags

我试过了

ft = new FlagTerm(new Flags(Flags.Flag.ANSWERED), false);

从收件箱中获取未接听邮件的列表,但会从收件箱中返回所有邮件。

message.isSet(Flags.Flag.ANSWERED);

也提供相同的输出。

如何从收件箱中列出已读但未接听的邮件?

2 个答案:

答案 0 :(得分:0)

您使用的是IMAP吗?如果没有,这将无效。

当您回复该邮件时,邮件阅读器可以在邮件上设置ANSWERED标志。有些邮件读者可能不这样做。你在用什么邮件阅读器?

答案 1 :(得分:0)

下载JavaMail API并确保相关的jar文件在您的类路径中。

使用gmail.com网络界面,您需要将阻止的设置更改为取消阻止到Gmail帐户。     import java.util。; import javax.mail。; import javax.mail.internet。*;

public class Main {

private static String USER_NAME = "*****";  // GMail user name (just the part before "@gmail.com")
private static String PASSWORD = "********"; // GMail password
private static String RECIPIENT = "lizard.bill@myschool.edu";

public static void main(String[] args) {
    String from = USER_NAME;
    String pass = PASSWORD;
    String[] to = { RECIPIENT }; // list of recipient email addresses
    String subject = "Java send mail example";
    String body = "Welcome to JavaMail!";

    sendFromGMail(from, pass, to, subject, body);
}

private static void sendFromGMail(String from, String pass, String[] to, String subject, String body) {
    Properties props = System.getProperties();
    String host = "smtp.gmail.com";
    props.put("mail.smtp.starttls.enable", "true");
    props.put("mail.smtp.host", host);
    props.put("mail.smtp.user", from);
    props.put("mail.smtp.password", pass);
    props.put("mail.smtp.port", "587");
    props.put("mail.smtp.auth", "true");

    Session session = Session.getDefaultInstance(props);
    MimeMessage message = new MimeMessage(session);

    try {
        message.setFrom(new InternetAddress(from));
        InternetAddress[] toAddress = new InternetAddress[to.length];

        // To get the array of addresses
        for( int i = 0; i < to.length; i++ ) {
            toAddress[i] = new InternetAddress(to[i]);
        }

        for( int i = 0; i < toAddress.length; i++) {
            message.addRecipient(Message.RecipientType.TO, toAddress[i]);
        }

        message.setSubject(subject);
        message.setText(body);
        Transport transport = session.getTransport("smtp");
        transport.connect(host, from, pass);
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }
    catch (AddressException ae) {
        ae.printStackTrace();
    }
    catch (MessagingException me) {
        me.printStackTrace();
    }
}

}