how to delete mail from mail server using C#
-
-
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
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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
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
-
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" - Mixturecheers, Alok Gupta VC Forum Q&A :- I/IV Support CRY- Child Relief and You
-
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.
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; } }
-
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.
-
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; } }
-
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; }
-
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; }
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); }