0

I am trying to send and receive messages through mail. But its only working on Gmail domain using SMTP TCP client. Its not working on other domains.

Please somebody help me with your valuable answers..!!

[HttpGet("MyValidate")]
        public async Task<ActionResult> MyValidate(string email)
        {
            var inputEmail = email;
            string[] host = (inputEmail.Split('@'));
            string hostname = host[1];
            var lookup = new LookupClient();
            var result = await lookup.QueryAsync(hostname, QueryType.MX).ConfigureAwait(false);

            var records = result.Answers.Where(record => record.RecordType == DnsClient.Protocol.ResourceRecordType.MX);
            var finalres = new List<object>();
            foreach (MxRecord mxRecord in records)
            {
                finalres.Add(mxRecord.Exchange);
            }
            var serverlocation = finalres.First().ToString();           
            TcpClient tClient = new TcpClient(serverlocation, 25);
            string CRLF = "\r\n";

            string ResponseString;
            UTF8Encoding objUTF8Encoding = new System.Text.UTF8Encoding();
            NetworkStream netStream = tClient.GetStream();
            netStream.Flush();
            StreamReader reader = new StreamReader(netStream);
            ResponseString = reader.ReadLine();
            byte[] dataBuffer = new byte[2048];
            /* Perform HELO to SMTP Server and get Response */
            dataBuffer = objUTF8Encoding.GetBytes("HELO hiii"+CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            netStream.Flush();
           
            ResponseString = reader.ReadLine();
            string from = "example@gmail.com";
            dataBuffer = BytesFromString("MAIL FROM:<"+from.Trim()+">"+CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            netStream.Flush();
          

            ResponseString = reader.ReadLine();
      
            var ToMail = inputEmail;
            dataBuffer = BytesFromString("RCPT TO:<"+ ToMail.Trim()+">"+CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            netStream.Flush();
            netStream.Close();

            ResponseString = reader.ReadLine();

            if (GetResponseCode(ResponseString) == 550)
            {
                Console.Write("Mai Address Does not Exist !<br/><br/>");
                Console.Write("<B><font color='red'>Original Error from Smtp Server :</font></b>" + ResponseString);
                return Ok("Mai Address Does not Exist");
            }
            /* QUITE CONNECTION */
            dataBuffer = objUTF8Encoding.GetBytes("QUITE" + CRLF);
            netStream.Write(dataBuffer, 0, dataBuffer.Length);
            tClient.Close();

            return Ok("Mai Address Does Exist");
        }

This is what I tried from the online sources.

Yogesh
  • 1
  • Is there any error message that you see? Are you sure that you have setup correct SMTP and POP? – Mark Spencer Mar 03 '23 at 03:46
  • we can't use POP because of using MX Record. But I think we have configure correctly. If any mistakes you can find you can correct it and reply me – Yogesh Mar 03 '23 at 05:00
  • Do you mean [check if an email address exists using .net?](https://stackoverflow.com/questions/3883518/can-i-check-if-an-email-address-exists-using-net) – Qing Guo Mar 03 '23 at 05:39
  • @YogeshS Sorry, I don't understand you. Is there any error message that you see? – Mark Spencer Mar 03 '23 at 05:49
  • @MarkSpencer , Yes, we have faced this Error while checking With other Domain "System.IO.IOException: Unable to read data from the transport connection: An established connection was aborted by the software in your host machine.." – Yogesh Mar 06 '23 at 10:51
  • @QingGuo No.It's not like that. – Yogesh Mar 08 '23 at 09:15

0 Answers0