【问题标题】:Luasec - send email via gmail using SMTPLuasec - 使用 SMTP 通过 gmail 发送电子邮件
【发布时间】:2016-01-16 09:47:51
【问题描述】:

我尝试使用 luasec 连接到我的 gmail 帐户并通过 SMTP 发送电子邮件,虽然过了一段时间我能够允许不安全的应用程序连接,但我想了解如何通过 gmail 实现安全连接.

我使用了以下代码,我在这里找到了,其中套接字连接在连接到 gmail 之前由 ssl 包装,但谷歌仍然说连接不安全。

local socket = require 'socket'
local smtp = require 'socket.smtp'
local ssl = require 'ssl'
function sslCreate()
    local sock = socket.tcp()
    return setmetatable({
        connect = function(_, host, port)
            local r, e = sock:connect(host, port)
            if not r then return r, e end
            sock = ssl.wrap(sock, {mode='client', protocol='tlsv1'})
            return sock:dohandshake()
        end
    }, {
        __index = function(t,n)
            return function(_, ...)
                return sock[n](sock, ...)
            end
        end
    })
end

function sendMessage(subject, body)
    local msg = {
        headers = {
            to = 'Your Target <target email>',
            subject = subject
        },
        body = body
    }

    local ok, err = smtp.send {
        from = '<your email>',
        rcpt = '<target email>',
        source = smtp.message(msg),
        user = 'username',
        password = 'password',
        server = 'smtp.gmail.com',
        port = 465,
        create = sslCreate
    }
    if not ok then
        print("Mail send failed", err) -- better error handling required
    end
end

我什至创建了一个自签名证书并将其用作 ssl 包装中的变量,但 gmail 仍然将连接识别为不安全。我们需要更改协议还是需要更新 luasec 库?

关于那个说明,我也没有无法通过 hotmail / outlook.com 发送电子邮件

【问题讨论】:

    标签: lua smtp luasec


    【解决方案1】:

    关于 Gmail:

    看看here。从本质上讲,谷歌自己说,“嘿,我们想让每个帐户都安全,所以我们拒绝让不太安全的应用程序访问我们用户的 Gmail 帐户!”谢天谢地,您可以将其关闭,否则我将永远无法使用 Fossamail 作为我的电子邮件客户端。

    Hotmail/Outlook 可能是同样的情况,虽然我自己不使用它们。

    【讨论】:

    • 感谢您的评论。我实际上看到了该链接,但是例如上面的代码不太安全的概念是我的问题的前提,即安全应用程序使用什么协议? (上面有 TLS 和 SSL,仍然被认为是不安全的!)是的,降低安全性的应用程序访问是一种选择(尽管不是一个理想的选择)。在旁注(但仍然与问题相关)我遇到了这个 support.google.com/a/answer/176600?hl=en 。问题是,什么协议被认为是安全的 ap 使用?
    • 在这种情况下,Google 希望用户使用 OAuth 2.0。那些不使用 OAuth 2.0 的人将被视为“不太安全”。
    猜你喜欢
    • 2017-05-12
    • 2011-08-11
    • 2011-02-08
    • 2012-05-29
    • 2020-02-21
    • 1970-01-01
    • 2021-11-16
    • 2020-05-30
    • 2014-01-04
    相关资源
    最近更新 更多