Click here to Skip to main content
15,892,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
i want to retrieve email in C# windows form and download their attachment and save email body and subject in database.

this is code i used to see the messages body but it cant download attachment.
C#
string response;
   string from = null;
   string subject = null;
   int totmessages;
   try
   {
       mailclient = new TcpClient(hostname.Text, 110);
   }
   catch (SocketException)
   {
       status.Text = "Unable to connect to server";
       return;
   }
ns = mailclient.GetStream();
   sr = new StreamReader(ns);
   sw = new StreamWriter(ns);
   response = sr.ReadLine();
   sw.WriteLine("User " + username.Text); //Send username
   sw.Flush();
   response = sr.ReadLine();
   if (response.Substring(0, 3) == "-ER")
   {
       status.Text = "Unable to log into server";
       return;
   }
   sw.WriteLine("Pass " + password.Text); //Send password
   sw.Flush();
   try
   {
       response = sr.ReadLine();
   }
   catch (IOException)
   {
       status.Text = "Unable to log into server";
       return;
   }
   if (response.Substring(0, 4) == "-ERR")
   {
       status.Text = "Unable to log into server";
       return;
   } sw.WriteLine("stat"); //Send stat command to getnumber of messages
   sw.Flush();
   response = sr.ReadLine();
   string[] nummess = response.Split(' ');
   totmessages = Convert.ToInt16(nummess[1]);
   if (totmessages > 0)
   {
       status.Text = "you have " + totmessages + " messages";
   }
   else
   {
       status.Text = "You have no messages";
   }
   for (int i = 1; i <= totmessages; i++)
   {
       sw.WriteLine("top " + i + " 0"); //read header of each message
       sw.Flush();
       response = sr.ReadLine();
       while (true)
       {
           response = sr.ReadLine();
           if (response == ".")
           break;
           if (response.Length > 4)
           {
               if (response.Substring(0, 5) == "From:")
                   from = response;
               if (response.Substring(0, 8) == "Subject:")
                   subject = response;
           }
       }
       messages.Items.Add(i + " " + from + " " + subject);



thanks
Posted
Updated 23-Nov-14 6:29am
v2
Comments
DamithSL 22-Nov-14 12:07pm    
what have you tried and where you stuck? can you update the question with your code?
[no name] 22-Nov-14 13:38pm    
I aggree

I'll trade you solutions!

Here's my question: Why is this function not working? (BASIC JAVASCRIPT)[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900