Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
golang send mail net/smtp SMTP
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.163.com"
auth := smtp.PlainAuth(
"",
"fledna@163.com",
"password*******",
smtpServer,
)
from := mail.Address{"监控中心", "fledna@163.com"}
to := mail.Address{"收件人", "name@139.com"}
title := "当前时段统计报表"
body := "报表内容一切正常";
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)
}
}
@bigFishZhang

This comment has been minimized.

Copy link

commented May 7, 2014

very good, thx

@momaek

This comment has been minimized.

Copy link

commented May 20, 2014

Can you give me some idea about how to receive email with golang

@emilgpa

This comment has been minimized.

Copy link

commented Jun 20, 2014

nice!! thanks you!!

@alexcesaro

This comment has been minimized.

Copy link

commented Jun 26, 2014

I created Gomail, a package to easily send emails if you are interested.

@ritoon

This comment has been minimized.

Copy link

commented Oct 2, 2014

Thank's !

@astromahi

This comment has been minimized.

Copy link

commented Jan 22, 2015

Nice work. Thanks!.

@dotNetDR

This comment has been minimized.

Copy link

commented Apr 27, 2015

is working, thank you!

@cention-mujibur-rahman

This comment has been minimized.

Copy link

commented May 19, 2015

Not working for me. I am getting error like:
2015/05/19 17:38:49 x509: certificate has expired or is not yet valid
exit status 1

@zgordan-vv

This comment has been minimized.

Copy link

commented Sep 6, 2015

Thank you!

@goldalworming

This comment has been minimized.

Copy link

commented Nov 26, 2015

I got this error for using gmail 2015/11/26 14:56:43 x509: certificate signed by unknown authority

@dmitry-udod

This comment has been minimized.

Copy link

commented Jul 7, 2016

Like a charm 👍

@houssemFat

This comment has been minimized.

Copy link

commented Nov 21, 2016

Than you !

@fabienthetis

This comment has been minimized.

Copy link

commented Aug 1, 2017

+1

@JiaoZiLang

This comment has been minimized.

Copy link

commented Sep 7, 2017

thank you

@hatuan

This comment has been minimized.

Copy link

commented Jan 19, 2018

Thank you

@lanzay

This comment has been minimized.

Copy link

commented Feb 3, 2018

Thank you
Little bit correct it: return strings.Trim(addr.String(), " <@>")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.