using System;
using System.Diagnostics;
using System.IO;
using System.Net.Sockets;
using System.Text;
using System.Text.RegularExpressions;
namespace ConsoleApp1 {
class Program {
static void Main(string[] args) {
bool doTwice = false;
string adress = "tklustig@web.de";
if (isEmail(adress) && checkIfMailExists(adress, "MX")) {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Mailadresse ist valide und existent");
doTwice = true;
}
else {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine("Mailadresse ist entwender invalide oder inexistent");
}
if (isEmail(adress) && doTwice) {
string fb = checkIfMailExists(adress);
if (!String.IsNullOrEmpty(fb)) {
Console.ForegroundColor = ConsoleColor.Red;
Console.WriteLine(fb);
}
else {
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine("Mailadresse ist valide und existent");
}
}
Console.ReadKey();
}
protected static bool isEmail(string inputEmail) {
inputEmail = inputEmail ?? string.Empty; ;
string strRegex = @"^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}" +
@"\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\" +
@".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$";
Regex re = new Regex(strRegex);
if (re.IsMatch(inputEmail))
return true;
else
return false;
}
protected static bool checkIfMailExists(string adress, string recType = "MX") {
string[] host = (adress.Split('@'));
string hostname = host[1];
bool result = false;
try {
using (Process proc = new Process()) {
proc.StartInfo.FileName = "nslookup";
proc.StartInfo.Arguments = string.Format("-type={0} {1}", recType, hostname);
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.ErrorDialog = false;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.OutputDataReceived += (object sender, DataReceivedEventArgs e) =>
{
if ((e.Data != null) && (!result))
result = e.Data.StartsWith(hostname);
};
proc.ErrorDataReceived += (object sender, DataReceivedEventArgs e) =>
{
if (e.Data != null) {
//read error output here, not sure what for?
}
};
proc.Start();
proc.BeginErrorReadLine();
proc.BeginOutputReadLine();
proc.WaitForExit(30000); //timeout after 30 seconds.
}
}
catch {
result = false;
}
return result;
}
protected static string checkIfMailExists(string adress) {
TcpClient tClient = new TcpClient("gmail-smtp-in.l.google.com", 25);
const string CRLF = "\r\n";
byte[] dataBuffer;
string responseString;
NetworkStream netStream = tClient.GetStream();
StreamReader reader = new StreamReader(netStream);
responseString = reader.ReadLine();
/* Perform HELO to SMTP Server and get Response */
dataBuffer = Encoding.ASCII.GetBytes("HELO KirtanHere" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
responseString = reader.ReadLine();
dataBuffer = Encoding.ASCII.GetBytes("MAIL FROM:<" + adress + ">" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
responseString = reader.ReadLine();
/* Read Response of the RCPT TO Message to know from google if it exist or not */
dataBuffer = Encoding.ASCII.GetBytes("RCPT TO:<" + adress + ">" + CRLF);
netStream.Write(dataBuffer, 0, dataBuffer.Length);
responseString = reader.ReadLine();
/* QUITE CONNECTION */
netStream.Write(dataBuffer, 0, dataBuffer.Length);
tClient.Close();
if (getResponseCode(responseString) > 500) {
return responseString;
}
return String.Empty;
}
private static int getResponseCode(string responseString) {
return int.Parse(responseString.Substring(0, 3));
}
}
}