使用C#检查电子邮件登录 [英] Check email login using C#
本文介绍了使用C#检查电子邮件登录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
大家好,
我想使用C#检查电子邮件登录.我只希望如果登录成功就返回true,而如果登录失败则失败.我怎样才能做到这一点? :)
在此先感谢您的帮助. :)
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. :)
推荐答案
您可以通过下面的链接:
使用C#从POP3服务器中检索邮件 [
You can go through the link below:
Retrieve Mail From a POP3 Server Using C#[^]
Take the only thing that you require.
您好,
如果仅使用smtp,则可以使用.net命名空间"System.Net.Mail".
用于检查与SMTP服务器的连接的代码如下所示:
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;
}
如果要接收邮件,则必须/可以使用"System.Net.Sockets-命名空间"中的类"TcpClient"和"NetworkStream".
用于检查与pop3-Srever的连接的代码如下所示:
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;
}
这篇关于使用C#检查电子邮件登录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文