• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 745
  • Last Modified:

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
0
codeloveratl
Asked:
codeloveratl
  • 5
  • 5
  • 3
  • +2
2 Solutions
 
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
 
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
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
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
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 5
  • 5
  • 3
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now