當前位置: 首頁>>編程示例 >>用法及示例精選 >>正文


GO PlainAuth用法及代碼示例

GO語言"net/smtp"包中"PlainAuth"函數的用法及代碼示例。

用法:

func PlainAuth(identity, username, password, host string) Auth

PlainAuth 返回一個 Auth,它實現了 RFC 4616 中定義的 PLAIN 身份驗證機製。返回的 Auth 使用給定的用戶名和密碼來驗證主機並充當身份。通常身份應該是空字符串,作為用戶名。

PlainAuth 隻會在連接使用 TLS 或連接到 localhost 時發送憑據。否則身份驗證將失敗並出現錯誤,而不發送憑據。

例子:

package main

import (
    "log"
    "net/smtp"
)

// variables to make ExamplePlainAuth compile, without adding
// unnecessary noise there.
var (
    from       = "gopher@example.net"
    msg        = []byte("dummy message")
    recipients = []string{"foo@example.com"}
)

func main() {
    // hostname is used by PlainAuth to validate the TLS certificate.
    hostname := "mail.example.com"
    auth := smtp.PlainAuth("", "user@example.com", "password", hostname)

    err := smtp.SendMail(hostname+":25", auth, from, recipients, msg)
    if err != nil {
        log.Fatal(err)
    }
}

相關用法


注:本文由純淨天空篩選整理自golang.google.cn大神的英文原創作品 PlainAuth。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。