我正在尝试通过 SmtpClient 连接到 iCloud
我使用的设置如下:
服务器名称:smtp.mail.me.com
需要 SSL:是
如果您在使用 SSL 时看到错误消息,请尝试改用 TLS 或 STARTTLS。
端口:587
需要 SMTP 身份验证:是 - 使用相关的用户名和密码
如果我使用 SSL,我会收到“由于意外的数据包格式导致握手失败”
如果我不使用 SSL,visual studio 调试器会在连接时挂起。
我认为问题是我没有告诉 SmtpClient 使用 tls,但我找不到关于如何执行此操作的文档。
代码如下:
using (var client = new SmtpClient()) {
client.Timeout = 1000 * 20;
//client.Capabilities.
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Connect("SMTP.mail.me.com", 587, false); //dies here
//client.Connect(servername, port, useSsl);
//can I set tls or starttls here??
client.Authenticate(username, password);
client.Send(FormatOptions.Default, message);
}
我可以手动设置 TLS 或 StartTLS 吗?我确实尝试过的一件事是以下但它似乎没有用
client.Connect(new Uri("smtp://" + servername + ":" + port + "/?starttls=true"));
感谢您对此的任何帮助。
最佳答案
您使用的 Connect()
方法只允许启用/禁用 SSL 包装的连接,这与 StartTLS 不同。
由于困惑,我实现了一个单独的 Connect()
方法,它使正在发生的事情更加明显:
using (var client = new SmtpClient()) {
// Note: don't set a timeout unless you REALLY know what you are doing.
//client.Timeout = 1000 * 20;
client.Connect ("smtp.mail.me.com", 587, SecureSocketOptions.StartTls);
client.Authenticate (username, password);
client.Send (message);
}
试试吧。
关于c# - Mailkit SMTP - StartTLS 和 TLS 标志,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31552536/