我一直在努力寻找这个问题,到目前为止一直在悲惨地失败.我最近的尝试是从这个堆栈代码中解除的: 通过带有C#的Gmail SMTP服务器发送电子邮件,但我已经尝试了我可以在堆栈和其他地方找到的所有语法.我的代码目前是:
var client = new SmtpClient("smtp.gmail.com", 587)
{
Credentials = new NetworkCredential("me@gmail.com", "mypass"),
EnableSsl = true
};
client.Send("me@gmail.com","me@gmail.com","Test", "test message");
Run Code Online (Sandbox Code Playgroud)
运行该代码给我一个立即异常"发送邮件失败",其中包含"无法连接到远程服务器"的内容.
如果我将端口更改为465(如gmail文档建议的那样),我每次都会超时.
我已经读过465不是一个好用的端口,所以我想知道这笔交易是什么让我无法连接.我的用户和通行证是对的.我已经读过我必须在我的Gmail帐户上设置POP服务,所以我这样做了.徒劳无功.
我原本试图让我的品牌GMail帐户工作,但遇到同样的问题,我认为我/我的常规Gmail帐户将更容易...到目前为止,情况并非如此.
Bry*_*red 15
我不久前也遇到过这个问题.问题是SmtpClient不支持隐式SSL连接,但支持显式连接(使用SSL的System.Net.Mail对端口465进行身份验证).上一类MailMessage(我相信.Net 1.0)确实支持这一点,但早已过时了.
我的回答是直接通过COM 调用CDO(协作数据对象)(http://support.microsoft.com/kb/310212),使用类似下面的内容:
/// <summary>
/// Send an electronic message using the Collaboration Data Objects (CDO).
/// </summary>
/// <remarks>http://support.microsoft.com/kb/310212</remarks>
private void SendTestCDOMessage()
{
try
{
string yourEmail = "YourUserName@gmail.com";
CDO.Message message = new CDO.Message();
CDO.IConfiguration configuration = message.Configuration;
ADODB.Fields fields = configuration.Fields;
Console.WriteLine(String.Format("Configuring CDO settings..."));
// Set configuration.
// sendusing: cdoSendUsingPort, value 2, for sending the message using the network.
// smtpauthenticate: Specifies the mechanism used when authenticating to an SMTP service over the network.
// Possible values are:
// - cdoAnonymous, value 0. Do not authenticate.
// - cdoBasic, value 1. Use basic clear-text authentication. (Hint: This requires the use of "sendusername" and "sendpassword" fields)
// - cdoNTLM, value 2. The current process security context is used to authenticate with the service.
ADODB.Field field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserver"];
field.Value = "smtp.gmail.com";
field = fields["http://schemas.microsoft.com/cdo/configuration/smtpserverport"];
field.Value = 465;
field = fields["http://schemas.microsoft.com/cdo/configuration/sendusing"];
field.Value = CDO.CdoSendUsing.cdoSendUsingPort;
field = fields["http://schemas.microsoft.com/cdo/configuration/smtpauthenticate"];
field.Value = CDO.CdoProtocolsAuthentication.cdoBasic;
field = fields["http://schemas.microsoft.com/cdo/configuration/sendusername"];
field.Value = yourEmail;
field = fields["http://schemas.microsoft.com/cdo/configuration/sendpassword"];
field.Value = "YourPassword";
field = fields["http://schemas.microsoft.com/cdo/configuration/smtpusessl"];
field.Value = "true";
fields.Update();
Console.WriteLine(String.Format("Building CDO Message..."));
message.From = yourEmail;
message.To = yourEmail;
message.Subject = "Test message.";
message.TextBody = "This is a test message. Please disregard.";
Console.WriteLine(String.Format("Attempting to connect to remote server..."));
// Send message.
message.Send();
Console.WriteLine("Message sent.");
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
Run Code Online (Sandbox Code Playgroud)
不要忘记浏览COM引用并添加"Microsoft CDO for Windows 200 Library",它应该添加两个引用:ADODB和CDO.
小智 14
我尝试了你的代码,它完全适用于端口587,但不适用于465.
你检查了防火墙吗?尝试从命令行"Telnet smtp.gmail.com 587"返回"220 mx.google.com ESMTP ....",然后端口打开.如果没有,它会阻止你打电话.
丹尼尔
小智 9
我在某个时候实施了一个电子邮件客户端,可以在587和465上与gmail交谈......
端口25是正常的未加密弹出端口; 不适用于Gmail.
另外两个端口有加密; 587使用TLS,465使用SSL.
要使用587,您应该设置SmtpClient.EnableSsl = true.
465不能使用SmtpClient,它将使用已弃用的类SmtpMail.