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:
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:
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;
}