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.