Skip to content

send email

Jianwei Mao edited this page Jul 7, 2022 · 1 revision

package main

import ( "errors" "log" "net/smtp" )

type loginAuth struct { username, password string }

func LoginAuth(username, password string) smtp.Auth { return &loginAuth{username, password} }

func (a *loginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) { return "LOGIN", []byte{}, nil }

func (a *loginAuth) Next(fromServer []byte, more bool) ([]byte, error) { if more { switch string(fromServer) { case "Username:": return []byte(a.username), nil case "Password:": return []byte(a.password), nil default: return nil, errors.New("Unkown fromServer") } } return nil, nil }

func main() {

// Set up authentication information.
auth := LoginAuth("", "")

// Connect to the server, authenticate, set the sender and recipient,
// and send the email all in one step.
to := []string{"@.com"}
msg := []byte("To: @.com\r\n" +
	"Subject: MaoReport: beijing tower\r\n" +
	"\r\n" +
	"This is the email body.\r\n")
err := smtp.SendMail("..com:25", auth, "@.com", to, msg)
if err != nil {
	log.Fatal(err)
}

}

Clone this wiki locally