I have to write an application which will retrieve emails from the mail server. If message text includes the "message identification string" text than I need to extract message content and save it to the database. Only new messages should be processed. The mail server supports both POP3 and IMAP protocols. How to achieve it?

asked 12 Jan '10, 16:02

Rebex%20KB's gravatar image

Rebex KB ♦♦
256312
accept rate: 78%

edited 14 Jan '10, 20:06

Martin%20Vobr's gravatar image

Martin Vobr ♦♦
335310


It looks like Rebex Secure IMAP can do what you need. In C#, you might use a code like this:

// connect and log in
Imap imap = new Imap();
imap.Connect("imap.gmail.com", 993, null, ImapSecurity.Implicit);
imap.Login(username, password);

// go to inbox
imap.SelectFolder("Inbox");

// serch for e-mails that contain the expected text – get list of their IDs
ImapMessageCollection messages = imap.Search(
     ImapListFields.UniqueId, 
     ImapSearchParameter.Body("message identification string"),
     ImapSearchParameter.HasFlagsNoneOf(ImapMessageFlags.Seen)
);

// download each of the e-mails found, 
// retrieve its body and check whether it is the message you need
foreach (ImapMessageInfo info in messages)
{
    MailMessage mail = imap.GetMailMessage(info.UniqueId);
    string mailText = mail.BodyText;
    // search the string here, 
    // extract data you need and do what you need with it
 }
 imap.Disconnect();
link

answered 12 Jan '10, 16:12

Rebex%20KB's gravatar image

Rebex KB ♦♦
256312
accept rate: 78%

In VB.NET, the code might look like this:

''# connect and log in
Dim imap As New Imap
imap.Connect("imap.gmail.com", 993, Nothing, ImapSecurity.Implicit)
imap.Login(username, password)

''# go to inbox
imap.SelectFolder("Inbox")

''# serch for e-mails that contain the expected text – get list of their IDs
Dim messages As ImapMessageCollection = imap.Search(_ 
     ImapListFields.UniqueId, _ 
     ImapSearchParameter.Body("Congratulations you have sold your product"), _
     ImapSearchParameter.HasFlagsNoneOf(ImapMessageFlags.Seen) _
)

''# download each of the e-mails found, 
''# retrieve its body and check whether it is the message you need
For Each info As ImapMessageInfo In messages

    Dim mail As MailMessage = imap.GetMailMessage(info.UniqueId)
    Dim mailText As String = mail.BodyText
    ''# search the string here, extract data you need and do what you need with it
Next

imap.Disconnect()
link

answered 12 Jan '10, 16:15

Martin%20Vobr's gravatar image

Martin Vobr ♦♦
335310
accept rate: 37%

edited 14 Jan '10, 18:26

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:

×71
×46
×10
×2

Asked: 12 Jan '10, 16:02

Seen: 477 times

Last updated: 14 Jan '10, 20:06