golang發郵件

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。

由於go預設tls.Config{ServerName: host, InsecureSkipVerify: false},

func TestHtmlEmail(t *testing.T) {opt := &EmailOptions{User:     "",Password: "",To:       []string{""},From:     "",Addr:     "smtp.xxx.com:25",}auth := NewLoginAuth(opt.User, opt.Password)pic, err := ioutil.ReadFile(`C:\Pictures\example.png`)if err != nil {t.Log(err)}dest := make([]byte, base64.StdEncoding.EncodedLen(len(pic)))base64.StdEncoding.Encode(dest, pic)buf := bytes.Buffer{}fmt.Fprint(&buf, "<html><head></head><body><p>Test png</p><p><img src=\"data:image/png;base64,")_, err = buf.Write(dest)fmt.Fprint(&buf, "\"></p><body></html>")err = sendMail(opt.Addr, auth, opt.From, opt.To, buf.Bytes(), "subject")if err != nil {t.Log(err)}}type LoginAuth struct {username, password string}func NewLoginAuth(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), nilcase "Password:":return []byte(a.password), nildefault:return nil, errors.New("Unknown fromServer")}}return nil, nil}func sendMail(addr string, auth smtp.Auth, from string, to []string, msg []byte, subject string) error {conn, err := smtp.Dial(addr)host, _, _ := net.SplitHostPort(addr)if err != nil {return err}defer conn.Close()if ok, _ := conn.Extension("STARTTLS"); ok {config := &tls.Config{ServerName: host, InsecureSkipVerify: true}if err = conn.StartTLS(config); err != nil {return err}}if auth != nil {if ok, _ := conn.Extension("AUTH"); ok {if err = conn.Auth(auth); err != nil {return err}}}if err = conn.Mail(from); err != nil {return err}for _, addr := range to {if err = conn.Rcpt(addr); err != nil {return err}}w, err := conn.Data()if err != nil {return err}header := make(map[string]string)header["Subject"] = subjectheader["MIME-Version"] = "1.0"header["To"] = strings.Join(to, ",")header["From"] = fromheader["Content-Type"] = "text/HTML; 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(msg)_, err = w.Write([]byte(message))if err != nil {return err}err = w.Close()if err != nil {return err}return conn.Quit()}

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.