C# MailSystem.NET IMAP4,将邮件标记为未读
有熟悉MailSystem.NET的人吗 我有一个应用程序可以定期检查gmail帐户中的新邮件。如果主题标题包含特定短语,则会采取行动。不过,我需要修改一下应用程序,将某些消息标记为未读 这是现有的代码。单击按钮将调用logInLogOut()子线程,并启动一个计时器,通过在另一个线程中调用checkNewMail()子线程,该计时器负责应用程序定期检查新邮件。该应用程序按预期运行,但以下可能不是最好的方式C# MailSystem.NET IMAP4,将邮件标记为未读,c#,email,C#,Email,有熟悉MailSystem.NET的人吗 我有一个应用程序可以定期检查gmail帐户中的新邮件。如果主题标题包含特定短语,则会采取行动。不过,我需要修改一下应用程序,将某些消息标记为未读 这是现有的代码。单击按钮将调用logInLogOut()子线程,并启动一个计时器,通过在另一个线程中调用checkNewMail()子线程,该计时器负责应用程序定期检查新邮件。该应用程序按预期运行,但以下可能不是最好的方式 private void logInLogOut() { try {
private void logInLogOut()
{
try
{
Client.ConnectSsl(txtIMAPServer.Text, int.Parse(txtIMAPPort.Text));
Client.Login(@txtUserName.Text, txtPassword.Text);
globalClientConnected = true;
}
catch (Exception ex)
{
globalClientConnected = false;
}
}
private void checkNewMail()
{
if (globalClientConnected)
{
foreach (ActiveUp.Net.Mail.Message email in GetUnreadMails("Inbox"))
{
string from = parseEmailAddress(email.From.ToString());
string subject = email.Subject;
string receivedDateTime = email.ReceivedDate.Date.ToString()
string updateString = receivedDateTime + ", " + from + ", " + subject + "\r\n";
if (subject.Contains("ABC"))
{
string to = from;
try
{
//do something
}
catch (Exception ex)
{
//bla bla
}
}
else
{
//If mail subject not like "ABC"
//Do something else
//Mark the mail as unread
}
}
}
}
不熟悉,但他们的源代码中有一个示例
Imap4Client imap = new Imap4Client();
imap.Connect("mail.myhost.com");
imap.Login("jdoe1234","tanstaaf");
Mailbox inbox = imap.SelectInbox("inbox");
FlagCollection flags = new FlagCollection();
flags.Add("Read");
flags.Add("Answered");
inbox.AddFlags(1,flags);
//Message is marked as read and answered. All prior flags are unset.
imap.Disconnect();
诀窍是解开“看见”的旗帜。有一种方法可以删除标志:RemoveFlags()。您只需要要从中删除标志的消息的id
var imap = new Imap4Client();
imap.ConnectSsl(hostname, port);
imap.Login(username, password);
var inbox = imap.SelectMailbox("inbox");
var ids = inbox.Search("UNSEEN");
foreach (var messageId in ids)
{
var message = inbox.Fetch.MessageObject(messageId);
// process message
var flags = new FlagCollection { "Seen" };
inbox.RemoveFlagsSilent(messageId, flags);
}
谢谢你!当你第二次看的时候,你会惊奇地发现:)