Free Support Forum - aspose.com

Reading Mails with Attachment

Hi,

I have a requirement, where I want to read all mails from a specific inbox(e,g siraj@mydomain.com)

Requirement

1. Should be able to download all incoming mail, along with ATTACHMENTS if any.

2. Should be able to delete the downloaded mail (the mail which are already read).

Actually Iam planning to set up an Inbox, where all my clients will send their reports. I want to access those reports in my ASP.Net Application.

Please suggest

Thanks

Siraj

Hi Siraj,

Below is the sample code snippet of a program that

  • checks the inbox of pop3 mail server
  • downloads all the emails
  • delete the email (based on a condition)
Pop3Client client = null;

try
{
Console.WriteLine(“Connecting to pop3…”);
client = new Pop3Client(“host”, 110, "user@host.com", “pwd”);
client.Connect(true);
Console.WriteLine(“Connected to pop3.”);

if (client.GetMessageCount() > 0)
{
Console.WriteLine(“Checking messages…”);
Pop3MessageInfoCollection msgInfoColl = client.ListMessages();

// check all messages in inbox
foreach (Pop3MessageInfo msgInfo in msgInfoColl)
{
MailMessage msg = client.FetchMessage(msgInfo.SequenceNumber);
Console.WriteLine(“Subject: " + msg.Subject);
foreach (Attachment att in msg.Attachments)
{
Console.WriteLine(“Attachment Name: " + att.Name);
}

// delete message according to a condition
if (msgInfo.Subject == “some special words”)
{
client.DeleteMessage(msgInfo.SequenceNumber);
Console.WriteLine(”===========This message is deleted============”);
}

Console.WriteLine("===============================");
}
}
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
finally
{
client.Disconnect();
Console.WriteLine(“Disconnected.”);
}