Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. how to delete mail from mail server using C#

how to delete mail from mail server using C#

Scheduled Pinned Locked Moved C#
questioncsharpdatabasesysadminhelp
9 Posts 3 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • K Offline
    K Offline
    khosnur
    wrote on last edited by
    #1

    i programmatically send and read mail and save the mail into my database. but i want delete a mail which will delete from my database and also from my mail server.how can i delete mail from mail server??? please help me. advance thanks Md Shafikul Islam

    T T 2 Replies Last reply
    0
    • K khosnur

      i programmatically send and read mail and save the mail into my database. but i want delete a mail which will delete from my database and also from my mail server.how can i delete mail from mail server??? please help me. advance thanks Md Shafikul Islam

      T Offline
      T Offline
      ThatsAlok
      wrote on last edited by
      #2

      Which mail server are you talking about??

      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
      Never mind - my own stupidity is the source of every "problem" - Mixture

      cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

      K 1 Reply Last reply
      0
      • K khosnur

        i programmatically send and read mail and save the mail into my database. but i want delete a mail which will delete from my database and also from my mail server.how can i delete mail from mail server??? please help me. advance thanks Md Shafikul Islam

        T Offline
        T Offline
        Tamer Oz
        wrote on last edited by
        #3

        If you are talking about Exchange server there is a web service of Exchange 2007 sp2, if you are talking about pop3 mail server you can try connecting to server by simulating telnet from your application and delete mail.

        K 2 Replies Last reply
        0
        • T ThatsAlok

          Which mail server are you talking about??

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
          Never mind - my own stupidity is the source of every "problem" - Mixture

          cheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You

          K Offline
          K Offline
          khosnur
          wrote on last edited by
          #4

          linux server using pop3 and smtp

          1 Reply Last reply
          0
          • T Tamer Oz

            If you are talking about Exchange server there is a web service of Exchange 2007 sp2, if you are talking about pop3 mail server you can try connecting to server by simulating telnet from your application and delete mail.

            K Offline
            K Offline
            khosnur
            wrote on last edited by
            #5

            many many thanks i am using pop3 mail server i programmatically connect the server and the mail ID but it do not delete the mail would u see the code and answer me the fault. public MAIL_ERROR Connect(string host, int port, string username, string password) { try { _conn.Host = host; _conn.Port = port; _conn.Connect(); System.Threading.Thread.Sleep(_TimeOut); return Login(username, password); } catch (Exception e) { System.Diagnostics.Trace.Write(e.ToString()); return MAIL_ERROR.ERROR_CANTCONNECT; } } public MAIL_ERROR DeleteMessage(int id) { if (this._state != POP3_STATE.POP3_LOGGEDIN) { throw new MailException("You are not logged into the mail server"); } string ret = ""; SendCommandSynchronous(string.Format("DELE {0}\r\n", id), out ret, false); if (ret.Contains("OK") == false) { throw new MailException(ret); } else { return MAIL_ERROR.ERROR_NONE; } }

            T 1 Reply Last reply
            0
            • T Tamer Oz

              If you are talking about Exchange server there is a web service of Exchange 2007 sp2, if you are talking about pop3 mail server you can try connecting to server by simulating telnet from your application and delete mail.

              K Offline
              K Offline
              khosnur
              wrote on last edited by
              #6

              here id i send mail receive number when read the mail i got that number. i pass that receive number for mail delete

              1 Reply Last reply
              0
              • K khosnur

                many many thanks i am using pop3 mail server i programmatically connect the server and the mail ID but it do not delete the mail would u see the code and answer me the fault. public MAIL_ERROR Connect(string host, int port, string username, string password) { try { _conn.Host = host; _conn.Port = port; _conn.Connect(); System.Threading.Thread.Sleep(_TimeOut); return Login(username, password); } catch (Exception e) { System.Diagnostics.Trace.Write(e.ToString()); return MAIL_ERROR.ERROR_CANTCONNECT; } } public MAIL_ERROR DeleteMessage(int id) { if (this._state != POP3_STATE.POP3_LOGGEDIN) { throw new MailException("You are not logged into the mail server"); } string ret = ""; SendCommandSynchronous(string.Format("DELE {0}\r\n", id), out ret, false); if (ret.Contains("OK") == false) { throw new MailException(ret); } else { return MAIL_ERROR.ERROR_NONE; } }

                T Offline
                T Offline
                Tamer Oz
                wrote on last edited by
                #7

                Everything seems ok. Could you pls send the SendCommandSynchronous method

                K 1 Reply Last reply
                0
                • T Tamer Oz

                  Everything seems ok. Could you pls send the SendCommandSynchronous method

                  K Offline
                  K Offline
                  khosnur
                  wrote on last edited by
                  #8

                  thanks again for reply.. here is the method: private MAIL_ERROR SendCommandSynchronous(string cmd, out string response, bool bMultiLineResponse) { // Remove any data that was left over on the buffer string deadData = _conn.Receive(); _conn.Send(cmd); response = WaitForResponse(bMultiLineResponse); return MAIL_ERROR.ERROR_NONE; } private string WaitForResponse(bool bMultiLine) { int timeout = 5000; // wait 10 seconds for response int waitedTime = 0; string strRet = ""; while (waitedTime < timeout) { System.Threading.Thread.Sleep(_TimeOut); strRet += _conn.Receive(); if (CommandComplete(strRet, bMultiLine) == true) { return strRet; } waitedTime += _TimeOut; } return strRet; }

                  T 1 Reply Last reply
                  0
                  • K khosnur

                    thanks again for reply.. here is the method: private MAIL_ERROR SendCommandSynchronous(string cmd, out string response, bool bMultiLineResponse) { // Remove any data that was left over on the buffer string deadData = _conn.Receive(); _conn.Send(cmd); response = WaitForResponse(bMultiLineResponse); return MAIL_ERROR.ERROR_NONE; } private string WaitForResponse(bool bMultiLine) { int timeout = 5000; // wait 10 seconds for response int waitedTime = 0; string strRet = ""; while (waitedTime < timeout) { System.Threading.Thread.Sleep(_TimeOut); strRet += _conn.Receive(); if (CommandComplete(strRet, bMultiLine) == true) { return strRet; } waitedTime += _TimeOut; } return strRet; }

                    T Offline
                    T Offline
                    Tamer Oz
                    wrote on last edited by
                    #9

                    Could you try

                        private string GetResponse()
                        {
                    
                            //read till finding new line, return.
                    
                            string strPrev = null;
                    
                            string strCurrent = null;
                    
                            string strMessage = null;
                    
                            NetworkStream ns = default(NetworkStream);
                    
                            ns = client.GetStream;//Previously instanced and connected TcpClient
                    
                            while (true)
                            {
                    
                                byte\[\] buffer = new byte\[1\];
                                //read by 1 byte 1 byte
                    
                                ns.Read(buffer, 0, buffer.Length);
                    
                                strCurrent = System.Text.Encoding.GetEncoding(1254).GetString(buffer);
                    
                                strMessage = strMessage + strCurrent;
                    
                                if (strCurrent == @"\\r" && strPrev == @"\\n")
                                {
                    
                                    break; // TODO: might not be correct. Was : Exit While
                                }
                    
                    
                                strPrev = strCurrent;
                            }
                    
                    
                            return strMessage;
                        }
                    
                        private void SendMessage(string str)
                        {
                    
                            str = str + "\\r\\n";
                            //Controlchars.CrLf '10+13
                    
                            byte\[\] buffer = System.Text.Encoding.GetEncoding(1254).GetBytes(str);
                    
                            NetworkStream ns = client.GetStream;
                    
                    
                            ns.Write(buffer, 0, buffer.Length);
                        }
                    
                    1 Reply Last reply
                    0
                    Reply
                    • Reply as topic
                    Log in to reply
                    • Oldest to Newest
                    • Newest to Oldest
                    • Most Votes


                    • Login

                    • Don't have an account? Register

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • World
                    • Users
                    • Groups