Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6960301
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T15:23:06+00:00 2026-05-27T15:23:06+00:00

How can I browse the email and download all attachments ? public string Connect_Email

  • 0

How can I browse the email and download all attachments ?

public string Connect_Email ()
{
    string Res = "";

    try
    {
        mailclient = new TcpClient("pop.orange.fr", Convert.ToInt16("110"));
    }
    catch ( SocketException ExTrhown )
    {
        Res = "Unable to connect to server 1";
        throw new Exception(ExTrhown.Message + "Unable to connect to server 1");
    }

    ns = mailclient.GetStream();
    sr = new StreamReader(ns);
    sw = new StreamWriter(ns);

    response = sr.ReadLine(); //Get opening POP3 banner

    sw.WriteLine("USER " + "xxxxx@orange.fr"); //Send username
    sw.Flush();

    response = sr.ReadLine();

    if ( response.Substring(0, 4) == "-ERR" )
    {
        Res = "Unable to log into server 2";
    }

    sw.WriteLine("PASS " + "xxxxx"); //Send password
    sw.Flush();

    response = sr.ReadLine();

    if ( response.Substring(0, 3) == "-ER" )
    {
        Res = "Unable to log into server 3";
    }

    return Res;
}

public void Get_Attacht ()
{
    string ClientName = "";

    #region Chercher Attachment
    sw.WriteLine("STAT"); //Send stat command to get number of messages
    sw.Flush();

    response = sr.ReadLine();

    //find number of message
    string[] nummess = response.Split(' ');
    totmessages = Convert.ToInt16(nummess[1]);

    //read emails
    for ( int i = 1; i <= totmessages; i++ )
    {
        msg = null;

        sw.WriteLine("top " + i + " 0"); //read header of each message
        sw.Flush();
        response = sr.ReadLine();

        while ( true )
        {
            response = sr.ReadLine();
            if ( response == "." )
                break;
            msg = msg + response + "\r\n";
        }

        //read attachment
        attachment = null;
        if ( Regex.Match(msg, "multipart/mixed").Success )
        {
            msg = null;
            sw.WriteLine("retr " + i.ToString()); //Retrieve entire message
            sw.Flush();
            response = sr.ReadLine();

            while ( true )
            {
                response = sr.ReadLine();
                if ( response == "." )
                    break;
                msg = msg + response + "\r\n";
            }

            int End = msg.IndexOf(".csv");
            string LeFile = msg.Substring(End - 9, 9);

            if ( Regex.Match(msg, LeFile + ".csv").Success )
            {
                data = msg.Split('\r');

                startindex = 0;
                index = 0;
                lastindex = 0;
                x = null;
                ms = null;
                fs = null;

                while ( true )
                {
                    attachment = null;
                    while ( !Regex.Match(data[index].Trim(), "filename").Success )
                    {
                        index++;
                    }
                    if ( index == data.Length - 1 ) break;
                    FileName_Email = data[index].Trim().Substring(42).Replace("\"", "");

                    //find start of attachment data
                    index++;
                    while ( data[index].Length != 1 )
                    {
                        index++;
                    }

                    if ( index == data.Length - 1 ) break;
                    startindex = index + 1;

                    //find end of data
                    index = startindex + 1;
                    while ( ( !Regex.Match(data[index].Trim(), "--0").Success ) && ( data[index].Length != 1 ) && ( index < data.Length - 1 ) )
                    {
                        index++;
                    }
                    if ( index == data.Length ) break;
                    lastindex = index - 2;

                    for ( int j = startindex; j <= lastindex; j++ )
                    {
                        attachment = attachment + data[j];
                    }
                    attachment = attachment + "\r\n";

                    if ( Regex.Match(FileName_Email.ToLower(), "csv").Success )
                    {
                        byte[] filebytes = Convert.FromBase64String(attachment);
                        FileStream LeFS = new FileStream(filePath + "\\testDEC.csv", FileMode.Create, FileAccess.Write, FileShare.None);
                        LeFS.Write(filebytes, 0, filebytes.Length);
                        LeFS.Close();
                        break;
                    }

                }
            }
        }
    }

    sw.WriteLine("quit"); //quit
    sw.Flush();
    #endregion
}

It does not work, have you another simple idea ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T15:23:07+00:00Added an answer on May 27, 2026 at 3:23 pm

    Try something like this

    using(Pop3 pop3 = new Pop3())  
     {  
         pop3.Connect("server");  
         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();  
     } 
    

    // here is a reference link you can use as well
    Getting Email Attachments

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I can't understand why object is null: WebClient browse = new WebClient(); StreamReader res
I can browse using the desktop browsers via ..localhost:54647/... and from windows mobile emulators
We have a page where use can browse 2000 profiles max, we add 20
I have the directory mapped on my machine so that I can browse and
I have a flashlite3 application with navigation consisting of icons the user can browse
My application displays a folder structure in a tree. The user can browse the
I have installed apache on my server however I can not browse my website
My application needs to implement dynamic images, where a browse can get served a
Why can't I browse deeper into the folder, I still have some folders there:
Can I define upload and browse baseurl to ckfinder dynamicly in the following code?

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.