📜  在 Golang 中使用 Smtp 发送电子邮件

📅  最后修改于: 2021-10-24 14:18:50             🧑  作者: Mango

网络/smtp 是一个内置的 go 包并实现了 SMTP 协议。它提供了一种通过 smtp 服务器发送邮件的简单方法。这个包实现了简单邮件传输协议。

发送电子邮件的步骤:

1.从Host服务器获取认证,通过PlainAuth函数与host服务器建立TLS连接。

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

PlainAuth 接受四个字符串类型的参数,身份(应该是空字符串作为用户名),用户名(发件人邮件地址),密码(发件人邮件密码)和 SMTP 服务器的端口。 PlainAuth 返回一个 Auth,一个 SMTP 身份验证机制的实现。为了向主机进行身份验证,返回的 Auth 使用给定的用户名和密码并充当身份。

2.使用获取到的Auth通过SendMail函数发送邮件

func SendMail(addr string, a Auth, from string, to []string, msg []byte) error

SendMail函数接受五个参数。 addr 是字符串类型,包含服务器的地址和端口号(例如:“smtp.gmail.com:587”),a is Auth 我们从 PlainAuth函数, from is 类型字符串并包含发件人邮件地址, to 是包含接收者邮件地址的字符串片段,而 msg 是包含邮件正文的字节片段。

这里我们使用 Gmail 服务器发送邮件。您可以使用任何域的邮件地址,只需相应地更改主机即可。

Go
// Sending Email Using Smtp in Golang
package main
 
import (
    "fmt"
    "net/smtp"
    "os"
)
 
// Main function
func main() {
 
    // from is senders email address
     
    // we used environment variables to load the
    // email address and the password from the shell
    // you can also directly assign the email address
    // and the password
    from := os.Getenv("MAIL")
    password := os.Getenv("PASSWD")
 
    // toList is list of email address that email is to be sent.
    toList := []string{"example@gmail.com"}
 
    // host is address of server that the
    // sender's email address belongs,
    // in this case its gmail.
    // For e.g if your are using yahoo
    // mail change the address as smtp.mail.yahoo.com
    host := "smtp.gmail.com"
 
    // Its the default port of smtp server
    port := "587"
 
    // This is the message to send in the mail
    msg := "Hello geeks!!!"
 
    // We can't send strings directly in mail,
    // strings need to be converted into slice bytes
    body := []byte(msg)
 
    // PlainAuth uses the given username and password to
    // authenticate to host and act as identity.
    // Usually identity should be the empty string,
    // to act as username.
    auth := smtp.PlainAuth("", from, password, host)
 
    // SendMail uses TLS connection to send the mail
    // The email is sent to all address in the toList,
    // the body should be of type bytes, not strings
    // This returns error if any occured.
    err := smtp.SendMail(host+":"+port, auth, from, toList, body)
 
    // handling the errors
    if err != nil {
        fmt.Println(err)
        os.Exit(1)
    }
 
    fmt.Println("Successfully sent mail to all user in toList")
}


输出:

Successfully sent mail to all user in toList