SMTP RCPT TO Testing



我正在为客户开发一个电子邮件验证工具。他们要求的一个功能是确认邮件服务器上是否存在用于测试电子邮件地址的邮箱。我已经使用C#中的TCP客户端实现了SMTP协议,但是当我尝试发出MAIL FROM命令时,我会收到一条错误消息,说我没有通过身份验证;这是在尝试验证GMail上的电子邮件地址时发生的。

我知道这应该是可能的,因为客户端提供了一个链接到一个做类似事情的服务(https://www.upwork.com/leaving?ref=https://verifalia.com/validate-电子邮件(。使用该网站,如果我输入有效的GMail地址,它会返回正确的信息(一个有效,另一个超过配额(。如果我更改了地址的一个字母,它会正确地报告邮箱不存在。我正在尝试实现相同的功能,但根据我收到的错误消息,我似乎需要在谷歌上拥有一个帐户。

然而,这对我来说没有意义。任何其他SMTP服务器如何连接到GMail(或者就此而言,其他任何SMTP服务器(来传递邮件?每个服务器不可能在其他服务器上都有一个帐户。我只是想让SMTP协议达到DATA元素(因为我不想发送实际的电子邮件(。

如有任何信息或帮助,我们将不胜感激。我在下面包含了我当前的代码。请注意,此代码目前尚未优化,我计划在协议工作后对其进行改进。此外,我一直使用的MAIL FROM地址实际上并不是test@example(我尝试过使用GMail、AIM和其他地址,但都导致了相同的错误(。

using (var client = new TcpClient())
{
host = "smtp.gmail.com";
var port = 465;
client.Connect(host, port);
// as gmail requires ssl we should use sslstream
// if your smtp server doesn't support ssl you can
// work directly with the underlying stream
using (var stream = client.GetStream())
using (var sslstream = new SslStream(stream))
{
sslstream.AuthenticateAsClient(host);
using (var writer = new StreamWriter(sslstream))
using (var reader = new StreamReader(sslstream))
{
string read = "";
if (stream.DataAvailable)
{
read = reader.ReadLine();
}
if (!read.StartsWith("220"))
{
return false;
}
writer.WriteLine("EHLO " + host);
writer.Flush();
do
{
read = reader.ReadLine();
} while (read.StartsWith("250-"));

if (read.StartsWith("220") || read.StartsWith("250"))
{
writer.WriteLine("mail from:<test@example.com>rn");
writer.Flush();
read = reader.ReadLine();
if (read.StartsWith("530"))
{
do
{
read = reader.ReadLine();
} while (read.StartsWith("530-"));
}
if (read.StartsWith("250"))
{
writer.WriteLine("rcpt to:<" + _emailAddress + ">");
writer.Flush();
read = reader.ReadLine();
if (read.StartsWith("250"))
{
writer.WriteLine("quit");
writer.Flush();
read = reader.ReadLine();
if (read.StartsWith("221"))
{
return true;
}
}
else
{
writer.WriteLine("quit");
writer.Flush();
}
}
else
{
writer.WriteLine("quit");
writer.Flush();
}
}
else
{
writer.WriteLine("quit");
writer.Flush();
}
// gmail responds with: 220 mx.google.com esmtp
}
}
}

如果你查看gmail.com的接收服务器,对我来说,响应是

host -t mx gmail.com
gmail.com mail is handled by 30 alt3.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 20 alt2.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 5 gmail-smtp-in.l.google.com.
gmail.com mail is handled by 40 alt4.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 10 alt1.gmail-smtp-in.l.google.com.

至于什么是smtp.gmail.com

host smtp.gmail.com
smtp.gmail.com is an alias for gmail-smtp-msa.l.google.com.
gmail-smtp-msa.l.google.com has address 108.177.127.109
gmail-smtp-msa.l.google.com has address 108.177.127.108
gmail-smtp-msa.l.google.com has IPv6 address 2a00:1450:4013:c00::6d

正如你所看到的,这不是一回事。如果你想将邮件发送到gmail.com地址,你需要使用MX记录中定义的服务器。正如你从名字中看到的,名字中有"gmail-smtp-in",这表明它们是用于接收电子邮件的。你使用的服务器是通过谷歌服务器发送电子邮件的。

因此,请将服务器更改为实际接收有关域的邮件的服务器,然后重试。

最新更新