注意:回答后添加: 谢谢..是的,我曾尝试过Flag.SEEN为true和saveChanges ..我也读过了读取的getContent标记。我尝试在循环消息的for语句中使用它。但是我在下一个循环中再次从文件夹中收到了消息。我假设文件夹是实时的,所以抓住内容,然后从过滤器的文件夹中再次抓取消息,看不到任何看到的应该工作,但我仍然得到相同的消息。我可以尝试关闭文件夹并重新打开作为测试,以查看它是否已标记。此外,如果我转到我的客户端并单击该消息,那么即使在循环中我的代码也会停止查看它,所以我希望在代码中也这样做。
原: 我正在使用javamail从gmail帐户获取电子邮件,它工作得很好,当我收到消息时我想将其标记为已阅读,是否有人可以给我一些指示?这是我目前的代码:
Properties props = System.getProperties();
props.setProperty("mail.store.protocol", "imaps");
try {
Session session = Session.getDefaultInstance(props, null);
Store store = session.getStore("imaps");
store.connect("imap.gmail.com", eUserName, ePassWord);
// Get folder
Folder folder = store.getFolder("INBOX");
if (folder == null || !folder.exists()) {
return null;
}
folder.open(Folder.READ_ONLY);
// Only pull unread
FlagTerm ft = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
Message messages[]; // = folder.search(ft);
for(int x = 0; x < timeOutInSeconds; x++) {
log.reportMessage("looking for emails");
try {
folder.getMessages();
messages = folder.search(ft);
if (messages.length > 0) {
for (Message message : messages) {
//log.reportMessage("found message: should not see again, marking read");
// want to mark as read
}
}
Thread.sleep(1000);
}
catch(Exception ex) {
}
}
// Close connection
folder.close(false);
store.close();
return null;
}
catch (NoSuchProviderException ex) {
return null;
}
catch (MessagingException ex) {
return null;
}
}
答案 0 :(得分:48)
首先,如果您使用的是POP3服务器,则无法将邮件标记为已读 - POP3协议不支持该邮件。但是,IMAP v4协议确实如此。
您可能认为这样做的方法是获取消息,将Flags.Flag.SEEN
标志设置为true,然后调用message.saveChanges()
。奇怪的是,事实并非如此。
相反,JavaMail API设计规范第4章“标志类”部分指出在检索消息内容时隐式设置SEEN
标志。因此,要将邮件标记为已读,您可以使用以下代码:
myImapFolder.open(Folder.READ_WRITE);
myImapFolder.getMessage(myMsgID).getContent();
myImapFolder.close(false);
或者另一种方法是使用MimeMessage复制构造函数,即:
MimeMessage source = (MimeMessage) folder.getMessage(1)
MimeMessage copy = new MimeMessage(source);
构造副本时,将为源引用的消息隐式设置seen标志。
答案 1 :(得分:35)
在不下载整个邮件的情况下执行此操作的一个班轮:
单条消息:
folder.setFlags(new Message[] {message}, new Flags(Flags.Flag.SEEN), true);
所有消息:
folder.setFlags(messages, new Flags(Flags.Flag.SEEN), true);
答案 2 :(得分:2)
这篇文章很老,但最简单的解决方案尚未发布。
您正在访问该消息。 message.setFlag(Flag.SEEN,true);
答案 3 :(得分:1)
您也可以考虑拥有public static int max_message_number
,并在阅读邮件后立即在其中存储message[i].getMessageNumber();
。然后在阅读任何消息之前,只需检查max_message_number < message[i].getmessageNumber()
。如果为true,则不要打印此消息(因为它已被读取)
答案 4 :(得分:1)
如果您使用for循环逐个阅读或检查邮件,则代码可以如下将gmail邮件标记为已读:
Message[] unreadMessages = inbox.search(new FlagTerm(new Flags(Flag.SEEN), false));
for (int q = 0; q < unreadMessages.length; q++) {
unreadMessages[q].setFlag(Flag.SEEN, true);
}
这段代码的作用是让它一个接一个地被读取。
并且文件夹/收件箱也需要是READ_WRITE,而不是READ_ONLY:
folder.open(Folder.READ_WRITE);
答案 5 :(得分:0)
message.setFlag(Flag.SEEN,true)给出“找不到符号” message.setFlag(Flags.Flag.SEEN,true)似乎很好。
答案 6 :(得分:0)
最简单的方法是设置要读取或写入的文件夹。像这样的意思......
Folder inbox = null;
inbox.open(Folder.READ_WRITE);
应该导入Folder类。
答案 7 :(得分:0)
folder.open(Folder.READ_ONLY);
和 更改以下行
folder.open(Folder.READ_WRITE);
到此
<强> import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
public class Test extends JApplet implements ItemListener,ActionListener
{
private String[] description = { "4.EarthWork", "5.Concrete",
"6.ReinforcedConcrete" };
private JTextField t = new JTextField(15);
private JComboBox c = new JComboBox();
private JButton view = new JButton("VIEW");
private JButton update = new JButton("UPDATE");
private int count = 0;
Container cp;
ResultSet rs,rs2,rs3,rs4,con,rcon;
Statement st;
String msg = "";
String str1,str2;
public void init()
{
try
{
String database="Proj.accdb";
Connection
c=DriverManager.getConnection("jdbc:ucanaccess://W:\\1.Intern
Project\\Proj.accdb");
st=c.createStatement();
}
catch(Exception e)
{
System.out.println(e);
}
for (int i = 0; i < 3; i++)
c.addItem(description[count++]);
t.setEditable(true);
t.setVisible(false);
view.addActionListener(this);
update.addActionListener(this);
c.addItemListener(this);
cp = getContentPane();
cp.setLayout(new FlowLayout());
cp.add(t);
cp.add(c);
cp.add(view);
cp.add(update);
}
public void itemStateChanged(ItemEvent ie)
{
str2=(String)ie.getItem();
System.out.println(str2);
repaint();
}
public void actionPerformed(ActionEvent ae)
{
str1=ae.getActionCommand();
System.out.println(str1);
repaint();
}
public void paint(Graphics g)
{
myMethod(g);
}
public void myMethod(Graphics g)
{
int y=200;
if(str2=="4.EarthWork" && str1=="View")
{
try
{
rs = st.executeQuery("select * from items where Itemid=1");
while(rs.next())
{
int i11 = rs.getInt("Itemid");
int id = rs.getInt("Id");
String name = rs.getString("IName");
String desc = rs.getString("Description");
String unit = rs.getString("Unit");
int price = rs.getInt("Price");
String s11 = String.valueOf(i11);
String s12 = String.valueOf(id);
String s13 = String.valueOf(price);
if(id==101)
{
g.drawString(s12,10,y);
g.drawString(name,20,y);
g.drawString(desc,30,y);
try
{
rs2 = st.executeQuery("select * from sitems where
id = 101");
}
catch(Exception e)
{
System.out.println(e);
}
while(rs2.next())
{
y = (y+5);
String sname = rs2.getString("SName");
String unt = rs2.getString("Unit");
int p = rs2.getInt("Price");
String sp = String.valueOf(p);
g.drawString(sname,30,y);
g.drawString(unt,40,y);
g.drawString(sp,50,y);
}
}
if(id==102)
{
y = (y+5);
g.drawString(s12,10,y);
g.drawString(name,20,y);
g.drawString(desc,30,y);
try
{
rs3 = st.executeQuery("select * from sitems where
id=102");
}
catch(Exception e)
{
System.out.println(e);
}
while(rs3.next())
{
y = (y+5);
String snm = rs3.getString("SName");
String ut = rs3.getString("Unit");
int pp = rs3.getInt("Price");
String spp = String.valueOf(pp);
g.drawString(snm,30,y);
g.drawString(ut,40,y);
g.drawString(spp,50,y);
}
}
if(id==103)
{
y = (y+5);
g.drawString(s12,10,y);
g.drawString(name,20,y);
g.drawString(desc,30,y);
try
{
rs4 = st.executeQuery("select * from sitems where
id=103");
}
catch(Exception e)
{
System.out.println(e);
}
while(rs4.next())
{
y = (y+5);
String snm1 = rs4.getString("SName");
String ut1 = rs4.getString("Unit");
int pp1 = rs4.getInt("Price");
String spp1 = String.valueOf(pp1);
g.drawString(snm1,30,y);
g.drawString(ut1,40,y);
g.drawString(spp1,50,y);
}
}
else
{
y = (y+5);
g.drawString(s12,10,y);
g.drawString(name,20,y);
g.drawString(desc,30,y);
g.drawString(unit,40,y);
g.drawString(s13,50,y);
}
}
}
catch(Exception e)
{
System.out.println(e);
}
}
if(str2=="5.Concrete" && str1=="View")
{
try
{
con = st.executeQuery("select * from items where Itemid=2");
while(con.next())
{
y = (y+5);
int i111 = con.getInt("Itemid");
int i1dd1 = con.getInt("Id");
String name1 = con.getString("IName");
String desc1 = con.getString("Description");
String unit1 = con.getString("Unit");
int price1 = con.getInt("Price");
String s111 = String.valueOf(i111);
String s121 = String.valueOf(i1dd1);
String s131 = String.valueOf(price1);
g.drawString(s121,10,y);
g.drawString(name1,20,y);
g.drawString(desc1,30,y);
g.drawString(unit1,40,y);
g.drawString(s131,50,y);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
if(str2=="6.ReinforcedConcrete" && str1=="View")
{
try
{
rcon = st.executeQuery("select * from items where Itemid=2");
while(rcon.next())
{
y = (y+5);
int zxc = rcon.getInt("Itemid");
int id56 = rcon.getInt("Id");
String name5 = rcon.getString("IName");
String desc5 = rcon.getString("Description");
String unit5 = rcon.getString("Unit");
int price5 = rcon.getInt("Price");
String s115 = String.valueOf(zxc);
String s125 = String.valueOf(id56);
String s135 = String.valueOf(price5);
g.drawString(s125,10,y);
g.drawString(name5,20,y);
g.drawString(desc5,30,y);
g.drawString(unit5,40,y);
g.drawString(s135,50,y);
}
}
catch(Exception e)
{
System.out.println(e);
}
}
}
public static void main(String[] args)
{
run(new Test(), 800, 600);
}
public static void run(JApplet applet, int width, int height)
{
JFrame frame = new JFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().add(applet);
frame.setSize(width, height);
applet.init();
applet.start();
frame.setVisible(true);
}
}
强>
答案 8 :(得分:0)
您也可以尝试
转到gmail设置>转发和POP / IMAP
从使用POP访问邮件时的下拉列表中 选择将Gmail的副本标记为已读并保存更改