I wanna only check any new incoming emails and save any attachment to a folder. How to do it? Currently I could check my gmail inbox. My code is below

Try
    Dim client As New Imap
    client.Connect("imap.gmail.com", 993, Nothing, ImapSecurity.Implicit)
    client.Login(strUName, strPWD)

    client.SelectFolder("Inbox")

    'Label1.Text = client.CurrentFolder.RecentMessageCount
    If client.CurrentFolder.RecentMessageCount > 0 Then
        MessageBox.Show("You have new mail in your inbox!")
    End If


Catch ex As Exception
    MsgBox(ex.Message)
End Try

asked 11 Feb '11, 10:28

soclose's gravatar image

soclose
181
accept rate: 0%


Please note that "Recent" messages are emails which arrived during your current session. Emails which arrived between your current session and last session are not marked as recent. Therefore you probably need to check "Unread" emails.

Following code should do the work:

Dim client As New Imap
client.Connect("imap.gmail.com", 993, Nothing, ImapSecurity.Implicit)
client.Login(strUName, strPWD)

client.SelectFolder("Inbox")

Dim list As ImapMessageCollection = client.Search( _
    ImapMessageSet.All, _
    ImapListFields.MessageStructure Or ImapListFields.UniqueId, _
    ImapSearchParameter.Unread)

For Each message As ImapMessageInfo In list
    If message.HasAttachment Then
        Dim mail As MailMessage = client.GetMailMessage(message.UniqueId)
        For Each attach As Attachment In mail.Attachments
            attach.Save(message.UniqueId & " - " & attach.FileName)
        Next
    End If
Next
link

answered 11 Feb '11, 14:38

Lukas%20Matyska's gravatar image

Lukas Matyska ♦♦
90117
accept rate: 28%

Thank you, Lukas. It works

(14 Feb '11, 06:00) soclose
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×76
×53
×20
×13

Asked: 11 Feb '11, 10:28

Seen: 413 times

Last updated: 11 Feb '11, 14:38