downloading email attachment using .net code

I have a requirement to download email file attachment and use the attachment file for another purpose in some web application.

Currently we use Lotus Notes and I tried to use domino objects to do this but in the future we may need another email program and hence the solution is needed to be generic for any email program . However I want to use .net as my development technology interms of communicating with the email program and download the attachemnts .

What are my options here .

Your help is greatly appriciated
codeloveratlAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

ukerandiCommented:
First you’ll need an IMAP client or POP3 client to download emails from the server.
When you use IMAP server:

using(Imap imap = new Imap())
{
    imap.Connect("server");   // or ConnectSSL
          imap.UseBestLogin("user", "password");
 
          imap.SelectInbox();
          List<long> uids = imap.SearchFlag(Flag.All);
    foreach (long uid in uids)
          {
        string eml = imap.GetMessageByUID(uid);
              IMail email = new MailBuilder()
                  .CreateFromEml(eml);
      
              Console.WriteLine(email.Subject);
      
              // save all attachments to disk
              email.Attachments.ForEach(mime => mime.Save(mime.SafeFileName));
          }
          imap.Close();
      }


When you use POP3 server:

using(Pop3 pop3 = new Pop3())
      {
          pop3.Connect("server");   // or ConnectSSL
          pop3.UseBestLogin("user", "password");
      
          foreach (string uid in pop3.GetAll())
          {
              IMail email = new MailBuilder()
                  .CreateFromEml(pop3.GetMessageByUID(uid));
      
              Console.WriteLine(email.Subject);
      
              // save all attachments to disk
              email.Attachments.ForEach(mime => mime.Save(mime.SafeFileName));
          }
          pop3.Close();
      }
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
ukerandiCommented:
0
codeloveratlAuthor Commented:
ukerandi:

 On your first comment, you said "need an IMAP client or POP3 client " then you said "When you use POP3 server:"

now my question is I have a lotus notes mail server so what is the role of POP3 client and POP3 server with regard to this implementation.

I need to understand the architecture of the mail system after incorporating POP3 client and server in to the existing lotus notes mail server.
0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

umeliCommented:
Hi codeloverati

the domino is the pop3 server. It has its own pop3 task.  the pop3 client is in the solution you are writing.
0
Sjef BosmanGroupware ConsultantCommented:
Just a well-meant question: could you have developed your web application in Lotus Domino? Domino is also a web server (and a darn good one), so maybe you could have saved yourself an enormous heap of problems that way...
0
ukerandiCommented:
hi,i said if you have Pop3 server you can use

using(Pop3 pop3 = new Pop3())
      {
          pop3.Connect("server");   // or ConnectSSL
          pop3.UseBestLogin("user", "password");
       
          foreach (string uid in pop3.GetAll())
          {
              IMail email = new MailBuilder()
                  .CreateFromEml(pop3.GetMessageByUID(uid));
       
              Console.WriteLine(email.Subject);
       
              // save all attachments to disk
              email.Attachments.ForEach(mime => mime.Save(mime.SafeFileName));
          }
          pop3.Close();
      }

But if your server using IMAP server you need to use following code

using(Imap imap = new Imap())
{
    imap.Connect("server");   // or ConnectSSL
          imap.UseBestLogin("user", "password");
 
          imap.SelectInbox();
          List<long> uids = imap.SearchFlag(Flag.All);
    foreach (long uid in uids)
          {
        string eml = imap.GetMessageByUID(uid);
              IMail email = new MailBuilder()
                  .CreateFromEml(eml);
       
              Console.WriteLine(email.Subject);
       
              // save all attachments to disk
              email.Attachments.ForEach(mime => mime.Save(mime.SafeFileName));
          }
          imap.Close();
      }


i have no idea what kind of server you are using
0
codeloveratlAuthor Commented:
The mail server that we are using now is lotus notes domino . my overall aim is to process the emails that are sent to a certain account created in the lotus notes domino server.

Now I could have used domino objects technology to get connected to the inbox and do the processing but I want a more generic approach like POP3 so that in the future if the email server is changed the solution will be generic enough not to be affected.

0
mhd196Commented:
Kind of theoretical advice since i never implemented it at work, you can make an application that provides your needed info as a web service. By this way, changing platform that consumes the web service will not impose changes on web service provider.
0
codeloveratlAuthor Commented:
I have now POP3 server configured with our lotus notes server . What are my possiblities to communicate from my .net code in such a way that I will access inbox of a certain account created for this purpose and process the body of the email and also download the attachments.
0
Sjef BosmanGroupware ConsultantCommented:
First, try with a POP3 client to connect to the Domino server, e.g. with Thunderbird (or Outlook).  Check that you can retrieve mails.

When you know it works, try ukerandi's code.
0
codeloveratlAuthor Commented:
Why I use with Thunderbird (or Outlook)? I want a POP3 library that I can use from my .net code to connect to the POP3 server and retrieve the email process .
0
Sjef BosmanGroupware ConsultantCommented:
One step at the time... Don't you want proof that the POP3 server is accessible? And allows you to fetch mails? Did you try the code above?
0
codeloveratlAuthor Commented:
sjef_bosman:
Of course that is what I want to proof first but kind of client library should I download to use the above code is not clear .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Lotus IBM

From novice to tech pro — start learning today.