我正在编写一个测试电子邮件地址的应用程序,我需要发送少量数据到电子邮件地址以完全验证它的一部分,我目前使用套接字来实现这一点。C#SMTP套接字问题
虽然我正在测试我使用了一个朋友的SMTP服务器进行测试,并且我的套接字工作正常,但是当我在一个大的电子邮件提供商(gmail,hotmail等)上尝试同样的事情时,却没有它。
现在我得出结论,认证和安全相关,所以我测试了使用StlEnabled内建的SmtpClient和各种Mail对象中的.Nets发送一条消息给gmail,并给它提供它要求的凭证,上班。
我需要做到的是:
,而无需提供登录信息纯粹作为交换发送这个数据。 也是一种将Ssl安全性融入到我的套接字中的方法。
任何这方面的帮助将是伟大的和appriciated!下面的代码...
/// <summary>
/// Opens up the socket and begins trying to connect to the provided domain
/// </summary>
/// <param name="domain">The domain listening on SMTP ports</param>
/// <param name="recipient">The email recipient</param>
/// <returns>Bool verifying success</returns>
public static bool ActivateSocket(string domain, string recipient)
{
//X509Certificate cert = new X509Certificate();
//Prepare our first command
string SMTPcommand = "HELO www.codegremlin.co.uk\r\n";
Encoding ASCII = Encoding.ASCII;
//convert to byte array and get the buffers ready
Byte[] ByteCommand = ASCII.GetBytes(SMTPcommand);
Byte[] RecvResponseCode = new Byte[3];
Byte[] RecvFullMessage = new Byte[256];
//method response value
bool TransactionSuccess = false;
try
{
// do all of this outside so its fresh after every iteration
Socket s = null;
IPEndPoint hostEndPoint;
IPAddress hostAddress = null;
int conPort = 587;
// get all the ip's assosciated with the domain
IPHostEntry hostInfo = Dns.GetHostEntry(domain);
IPAddress[] IPaddresses = hostInfo.AddressList;
// go through each ip and attempt a connection
for (int index = 0; index < IPaddresses.Length; index++)
{
hostAddress = IPaddresses[index];
// get our end point
hostEndPoint = new IPEndPoint(hostAddress, conPort);
// prepare the socket
s = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
s.ReceiveTimeout = 2000;
s.SendTimeout = 4000;
try { s.Connect(hostEndPoint); }
catch { Console.WriteLine("Connection timed out..."); }
if (!s.Connected)
{
// Connection failed, try next IPaddress.
TransactionSuccess = false;
s = null;
continue;
}
else
{
// im going through the send mail, SMTP proccess here, slightly incorrectly but it
//is enough to promote a response from the server
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
s.Send(ByteCommand, ByteCommand.Length, 0);
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
ByteCommand = ASCII.GetBytes("MAIL FROM:[email protected]\r\n");
s.Send(ByteCommand, ByteCommand.Length, 0);
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
ByteCommand = ASCII.GetBytes("RCPT TO:[email protected]\r\n");
s.Send(ByteCommand, ByteCommand.Length, 0);
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
ByteCommand = ASCII.GetBytes("DATA\r\n");
s.Send(ByteCommand, ByteCommand.Length, 0);
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
ByteCommand = ASCII.GetBytes("this email was sent as a test!\r\n.\r\n");
s.Send(ByteCommand, ByteCommand.Length, 0);
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
ByteCommand = ASCII.GetBytes("SEND\r\n");
s.Send(ByteCommand, ByteCommand.Length, 0);
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
ByteCommand = ASCII.GetBytes("QUIT\r\n");
s.Send(ByteCommand, ByteCommand.Length, 0);
s.Receive(RecvFullMessage);
Console.WriteLine(ASCII.GetString(RecvFullMessage));
int i = 0;
TransactionSuccess = true;
}
}
}
catch (SocketException e)
{
Console.WriteLine("SocketException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch (ArgumentNullException e)
{
Console.WriteLine("ArgumentNullException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch (NullReferenceException e)
{
Console.WriteLine("NullReferenceException caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
catch (Exception e)
{
Console.WriteLine("Exception caught!!!");
Console.WriteLine("Source : " + e.Source);
Console.WriteLine("Message : " + e.Message);
}
return TransactionSuccess;
}
对不起,删除我的帖子,因为它是不准确的。感谢您的更正,@Daniel A. White和@Storm – David 2009-11-12 14:18:34
嗨,这段代码的目的是什么?它看起来像垃圾邮件目的的邮件列表清理程序。无论哪种方式,我建议你看看你从你连接的服务器收到的错误,并从中推导出解决方案。如果没有明确指出问题,很难提出解决方案。 – Lazarus 2009-11-12 14:19:47
目的是纯粹的电子邮件验证,即时通讯对垃圾邮件的战争的好方面:P – DevlopingTheNextGen 2009-11-12 14:26:12