Click here to Skip to main content
15,917,329 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi all,

I want to check email login using C#. I just want it return true if login successfully and fail if login fail. How can I do that? :)

Thanks in advance for any help. :)
Posted
Comments
dan!sh 9-Aug-11 1:58am    
Are you using SMTP for sending mail? If yes, there is no easy way to do this.
Lương Thông Đạt 9-Aug-11 2:03am    
I don't know anything about SMTP. Can you give an example code :). Thanks

You can go through the link below:
Retrieve Mail From a POP3 Server Using C#[^]

Take the only thing that you require.
 
Share this answer
 
Comments
Lương Thông Đạt 9-Aug-11 3:51am    
Thanks a lot :)
blueye89 6-Feb-12 15:36pm    
I'am working on .Net application and I have a problem.Can anyone help me to resolve?
http://www.codeproject.com/Questions/325822/email-validation-problem
Hello,

if you use smtp only, you can use the .net-Namespace 'System.Net.Mail'.
The code to check the connection to the SMTP-Server can look like this:

C#
public bool CheckSMTPConnection(string SMTPServer, string Benutzer, string Passwort)
       {
           bool result = false;
           try
           {
               MailAddress MailEmpfaenger = new MailAddress("Test@Test.de");
               MailAddress MailAbsender = new MailAddress("Test@Test.de");
               MailMessage message = new MailMessage();
               SmtpClient client = new SmtpClient(SMTPServer);
               NetworkCredential credentials = new NetworkCredential(Benutzer, Passwort);
               client.Credentials = credentials;
               message.Priority = MailPriority.Normal;
               message.From = MailAbsender;
               message.To.Add(MailEmpfaenger);
               message.Subject = "VERBINDUNGSTEST";
               message.Body = "VERBINDUNGSTEST";
               client.Send(message);
               result = true;
           }
           catch (Exception ex)
           {
               result = false;
               throw new Exception(ex.Message);
           }
           return result;
       }


If you want receive Mails you must/can use the Classes 'TcpClient' and 'NetworkStream' in the 'System.Net.Sockets-namespace'.
The code to check the connection to the pop3-Srever can look like this:

C#
public bool CheckPop3Connection(string Pop3Server, string Benutzer, string Passwort)
        {
            bool result = false;
            try
            {
                #region Objekte
                TcpClient Server;
                NetworkStream NetStrm;
                StreamReader reader;
                string Data;
                byte[] szData;
                string CRLF = "\r\n";
                #endregion

                #region Server erreichbar
                Server = new TcpClient(Pop3Server, 110);
                NetStrm = Server.GetStream();
                reader = new StreamReader(Server.GetStream());
                if (reader.ReadLine().Substring(0, 3).ToUpper() != "+OK")
                {
                    throw new Exception("Der Server konnte nicht erreicht werden.");
                }
                #endregion

                #region Benutzer akzeptiert
                Data = "USER " + Benutzer + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                if (reader.ReadLine().Substring(0, 3).ToUpper() != "+OK")
                {
                    throw new Exception("Der Benutzername wurde nicht erkannt.");
                }
                #endregion

                #region Passwort übergeben
                Data = "PASS " + Passwort + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                string text = reader.ReadLine();
                if (text.Substring(0, 3).ToUpper() != "+OK")
                {
                    throw new Exception(text.Replace("-ERR ", ""));
                }
                result = true;
                #endregion

                #region Verbindung schließen
                Data = "QUIT" + CRLF;
                szData = System.Text.Encoding.ASCII.GetBytes(Data.ToCharArray());
                NetStrm.Write(szData, 0, szData.Length);
                if (reader.ReadLine().Substring(0, 3).ToUpper() != "+OK")
                {
                    throw new Exception("Fehler beim Abmelden.");
                }
                NetStrm.Close();
                reader.Close();
                #endregion
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }

            return result;
        }
 
Share this answer
 
Comments
Lương Thông Đạt 9-Aug-11 3:51am    
Thank you. It is very useful for me :)

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