Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package main
- import (
- "log"
- "net/mail"
- "encoding/base64"
- "net/smtp"
- "fmt"
- "strings"
- )
- func encodeRFC2047(String string) string{
- // use mail's rfc2047 to encode any string
- addr := mail.Address{String, ""}
- return strings.Trim(addr.String(), " <>")
- }
- func main() {
- // Set up authentication information.
- smtpServer := "smtp.yandex.ru"
- auth := smtp.PlainAuth(
- "",
- "alpa.92@yandex.ru",
- "*****",
- smtpServer,
- )
- from := mail.Address{"Alex", "alpa.92@yandex.ru"}
- to := mail.Address{"Alex", "pastuhov.alexandr@gmail.com"}
- title := "Hello Alex"
- body := "Hello Alex, this is test mail"
- 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\r\n", k, v)
- }
- message += "\r\n" + 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 + ":25",
- auth,
- from.Address,
- []string{to.Address},
- []byte(message),
- //[]byte("This is the email body."),
- )
- if err != nil {
- log.Fatal(err)
- } else {
- fmt.Println("all is ok")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement