在localhost smtp上发送邮件不起作用

时间:2014-11-09 10:09:15

标签: email go

我正在尝试向localhost stmp服务器发送电子邮件。我正在使用fakesmtp程序接收来自localhost的电子邮件。

请查看以下代码段

package mail

import (
    "encoding/base64"
    "fmt"
    "log"
    "net/mail"
    "net/smtp"
    "strings"
)

func encodeRFC2047(String string) string {
    // use mail's rfc2047 to encode any string
    addr := mail.Address{String, ""}
    return strings.Trim(addr.String(), " <>")
}

func Send() {
    // Set up authentication information.

    smtpServer := "127.0.0.1:2525"
    auth := smtp.PlainAuth(
        "",
        "admin",
        "admin",
        smtpServer,
    )

    from := mail.Address{"example", "info@example.com"}
    to := mail.Address{"customer", "customer@example.com"}
    title := "Mail"

    body := "This is an email confirmation."

    header := make(map[string]string)
    header["From"] = from.String()
    header["To"] = to.String()
    header["Subject"] = encodeRFC2047(title)
    header["MIME-Version"] = "1.0"
    header["Content-Type"] = "text/plain; charset=\"utf-8\""
    header["Content-Transfer-Encoding"] = "base64"

    message := ""
    for k, v := range header {
        message += fmt.Sprintf("%s: %s\r\n", k, v)
    }
    message += "\r\n" + base64.StdEncoding.EncodeToString([]byte(body))

    // Connect to the server, authenticate, set the sender and recipient,
    // and send the email all in one step.
    err := smtp.SendMail(
        smtpServer,
        auth,
        from.Address,
        []string{to.Address},
        []byte(message),
        //[]byte("This is the email body."),
    )
    if err != nil {
        log.Fatal(err)
    }
}

当我执行发送功能时,我得到了未加密连接的错误。为什么呢?

1 个答案:

答案 0 :(得分:0)

服务器很可能不允许您对未加密的连接使用纯文本身份验证,这对于几乎所有MTA来说都是合理的默认设置。将身份验证信息更改为例如摘要,或在您的客户端代码中启用SSL / TLS。

请务必使用tcpdumpwireshark来检查实际传输的内容。