0 votes

Hello,

I want to update email downloaded from server. If I change email message to read/unread or flag changed.

How can I update email? If I can't update, suggest me another way if possible.

by (3.0k points)

1 Answer

+1 vote

Emails are immutable, which meant you can not change them - you can't add an attachment or change email's subject.

You can update their properties, such as flags for example. Following code searches for unseen messages and marks them as seen:

using(Imap imap = new Imap())
{
    imap.Connect("imap.example.com");    // or ConnectSSL for SSL
    imap.UseBestLogin("user", "password");

    imap.SelectInbox();

    List<long> uids = client.Search(Flag.Unseen);
    if (uids.Count > 0)
        client.MarkMessageSeenByUID(uids[0]);

    imap.Close();
}

Note that when you download a message from IMAP server (using Imap.GetMessageByUID) it's automatically marked as seen (you can avoid that using one of the *Imap.Peek** methods)

https://www.limilabs.com/blog/mark-emails-as-read-with-imap

by (301k points)
Thank you.
Which method is used to mark email as unread/unseen?
Which other properties I can update?
Plz update in last comment.
I get task details of the email by following code.

MimeTextStructure structure = (MimeTextStructure)emailEntity.BodyStructure.Attachments.Find(x => x.ContentType == ContentType.ApplicationIcs || x.ContentType == ContentType.TextCalendar);
if (structure != null)
{
    imap.GetDataByUID(structure);
    Appointments.Appointment appointment = new AppointmentParser().Parse(structure.Text);

    if (appointment.Event.Status != EventStatus.Tentative
        & appointment.Event.Status != EventStatus.Confirmed)
    {
        task.StartDate = appointment.Event.Start;
        task.DueDate = appointment.Event.End;
        task.Subject = appointment.Event.Description;
        task.Description = appointment.Event.Summary;
        task.CreateFrom = "Mail"; task.MailID = MailID;
        task.MailAddress = ""; task.MailSettingID = MailID;
        task.UserID = UserID;
        task.Description = emailEntity.BodyStructure.Text.Text;
        new clsTaskManagerDAL().AddUpdateTask(ref task, objConn);
    }   
}

Can I update this details also?
To change seen status use Imap.MarkMessageSeenByUID and Imap.MarkMessageUnseenByUID. You can change email flags, email folder, labels if it is Gmail server.
No. As I said before emails are immutable. You can not change attachment content. Appointment is just a attachment (or MIME entity to be precise)
Can you please explain what do you mean by flags?
Email flags:https://www.limilabs.com/mail/rfc/3501 Section 2.3.2. Flags Message Attribute

Also you can use BodyStructure.Appointments to get MimeCalendarStructure, then you can use GetDataByUID and use its Appointment property
Hello,
I have read the Section 2.3.2. Is there any method like "client.MarkMessageSeenByUID(uids[0]);" for update Flagged status?
If not, suggest me way to update Flagged information on server.
This is my basic requirement for all the server Gmail, Yahoo etc:
1- When user change mail for read/unread it should update to mail server.

2- When user change flag details of mail, it should update to mail server.
What are you talking about? \Seen flag is not \Flagged flag.

\Seen flag is added automatically when you download a message (see my answer).
You can add or remove it using Imap.MarkMessageSeenByUID and Imap.MarkMessageUnseenByUID (see my comment above).

If you want to set \Flagged flag use Imap.FlagMessageByUID(uid, Flag.Flagged) to unset it use UnFlagMessageByUID.
Thank you very much. I got my answer.
...