Mailkit SMTP - StartTLS& TLS标志

时间:2015-07-22 01:47:10

标签: c# mailkit

我正在尝试通过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"));

感谢您提供任何帮助。

4 个答案:

答案 0 :(得分:13)

您使用的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;

    // 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);
}

试试。

答案 1 :(得分:0)

您可以将选项设置为“SecureSocketOptions.Auto” 像这样的东西

await client.ConnectAsync(mailService.Host, mailService.Port,   SecureSocketOptions.Auto);

MailKit将自动决定使用SSL或TLS。

答案 2 :(得分:0)

根据MailKit Doc

Connect(string host,int port,bool useSsl,CancellationToken cancellationToken = null)

useSsl参数仅控制客户端是否建立SSL包装的连接。换句话说,即使如果useSsl参数为false,如果邮件服务器支持STARTTLS扩展,则仍可以使用SSL / TLS

答案 3 :(得分:0)

这通过前面提到的SecureSocketOptions.Auto选项为我工作:

public function edit($id = null)
    {
        $product = $this->Products->get($id, [
            'contain' => [
                'Images',
                'Categories',
                'ChildProducts',
                'AttributeValues',
                'AttributeValues.Attributes'
            ]
        ]);

        if ($this->request->is(['patch', 'post', 'put'])) {
            if (!$this->request->getData('images')) {
                $this->request->data['images'] = [];
            }

            $product = $this->Products->patchEntity($product, $this->request->getData(), [
                'associated' => ['Images', 'AttributeValues']
            ]);

            if ($this->Products->save($product, ['associated' => ['Images', 'AttributeValues'] ])) {
                $this->Flash->success(__('The product has been saved.'));
                return $this->Redirecter->goBack();
            }
            $this->Flash->error(__('The product could not be saved. Please, try again.'));
        }

        $this->AttributesHandler->setAttributes(
            !empty($product->category_id) ? $product->category_id : null,
            $product->id
        );

        $categories = $this->Products->Categories->find('list');
        $this->set(compact('productType', 'product', 'categories'/*, 'attributes'*/));
        $this->set('_serialize', ['product']);
    }

相关问题