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

I am trying to send an email to the localhost stmp server. I am using fakesmtp program to receive email from localhost.

Look at following code snippet

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
", k, v)
    }
    message += "
" + 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)
    }
}

When I executed the send function, I've got the error unencrypted connection. Why?

Most likely the server does not allow you to use plain-text authentication over an unencrypted connection, which is a sensible default for almost any MTA out there. Either change authentication info to e.g. digest, or enable SSL/TLS in you client code.

Remember to use tcpdump or wireshark to check what is actually transmitted.