Mailkit SMTP - 启动TLS和TLS标志
问题描述:
我试图通过SmtpClient连接到iCloudMailkit SMTP - 启动TLS和TLS标志
我使用的设置如下:
服务器名称:smtp.mail.me.com
要求SSL :是
如果您在使用SSL时看到错误消息,请尝试使用TLS或STARTTLS。
端口:587
SMTP需要身份验证:是 - 与相关用户名和密码
如果我使用SSL我得到“握手失败,原因是意外的数据包格式”
如果我不使用SSL视觉工作室调试器挂在连接上。
我认为问题是我不告诉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或启动TLS。我尝试过的一件事是以下,但它似乎并没有工作
client.Connect(new Uri("smtp://" + servername + ":" + port + "/?starttls=true"));
感谢您的任何帮助。
答
您正在使用的Connect()
方法仅允许启用/禁用与StartTLS不同的SSL封装连接。
由于混乱,我已经实现了一个单独的Connect()
方法,使这更明显的是怎么回事:
using (var client = new SmtpClient()) {
// Note: don't set a timeout unless you REALLY know what you are doing.
//client.Timeout = 1000 * 20;
// Removing this here won't do anything because the AuthenticationMechanisms
// do not get populated until the client connects to the server.
//client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Connect ("smtp.mail.me.com", 587, SecureSocketOptions.StartTls);
client.AuthenticationMechanisms.Remove ("XOAUTH2");
client.Authenticate (username, password);
client.Send (message);
}
尝试。
+0
这个工程.. @ cavej03请接受这个安装程序 – om471987
答
您可以设置选项为 “SecureSocketOptions.Auto” 像这样
await client.ConnectAsync(mailService.Host, mailService.Port, SecureSocketOptions.Auto);
MailKit会自动决定使用SSL或TLS。
如果你说你需要'SSL',你为什么要为'useSSL'标志传递'false'? – Rob
@Rob我已经尝试使用true,并且由于意外的数据包格式而导致握手失败。 iCloud建议:“如果您在使用SSL时看到错误消息,请尝试使用TLS或STARTTLS。” – cavej03