当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。