1. 程式人生 > >利用smtp協議傳送帶附件的郵件

利用smtp協議傳送帶附件的郵件

     之前寫過一個發郵件的,不過沒帶附檔,今天再看了下smtp協議,做了個帶附檔的郵件傳送例子,也就這樣吧。

package main

/*
瞭解下smtp協議,並做了個小演示:
    利用Go自帶的net/smtp包,傳送帶附檔的郵件。


Author: XCL
Date: 2016-5-7
Blog: http://blog.csdn.net/xcl168
*/

import (
	"bytes"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"net"
	"net/smtp"
	"strings"
)

const (
	emlUser = "[email protected]
" emlPwd = "-------" emlSMTP = "smtp.xxx.com:25" ) func main() { err := eml() if err != nil { fmt.Println(" err:", err) } } // 發普通文字郵件 func eml() error { to := "[email protected]" cc := "[email protected]" sendTo := strings.Split(to, ";") subject := "這是一封演示用的郵件" boundary := "next message" //boundary 用於分割郵件內容,可自定義. 注意它的開始和結束格式 mime := bytes.NewBuffer(nil) //設定郵件 mime.WriteString(fmt.Sprintf("From: %s<%s>\r\nTo: %s\r\nCC: %s\r\nSubject: %s\r\nMIME-Version: 1.0\r\n", emlUser, emlUser, to, cc, subject)) mime.WriteString(fmt.Sprintf("Content-Type: multipart/mixed; boundary=%s\r\n", boundary)) mime.WriteString("Content-Description: 這是一封帶附檔的郵件\r\n") //郵件普通Text正文 mime.WriteString(fmt.Sprintf("--%s\r\n", boundary)) mime.WriteString("Content-Type: text/plain; charset=utf-8\r\n") mime.WriteString("This is a multipart message in MIME format.") //郵件HTML正文 mime.WriteString(fmt.Sprintf("\n--%s\r\n", boundary)) boundaryHtml := "boundaryHtml" mime.WriteString(fmt.Sprintf("Content-Type: multipart/alternative; boundary=%s\r\n", boundaryHtml)) mime.WriteString("Content-Description: Message in alternative text and HTML forms\r\n") mime.WriteString(fmt.Sprintf("\n--%s\r\n", boundaryHtml)) mime.WriteString(fmt.Sprintf("Content-Type: %s; charset=utf-8\r\n", "text/html")) mime.WriteString(`<html><body> <p><img src="https://golang.org/doc/gopher/doc.png"></p><br/> <h1>最近有點消沉,也有點想家了.</h1> </body></html>`) mime.WriteString(fmt.Sprintf("\n--%s--\r\n\r\n", boundaryHtml)) // 第一個附件 attaFile := "/Users/xcl/xclcode/tconv.go" attaFileName := "tconv.go" mime.WriteString(fmt.Sprintf("\n--%s\r\n", boundary)) mime.WriteString("Content-Type: application/octet-stream\r\n") mime.WriteString("Content-Description: 附一個Go檔案\r\n") mime.WriteString("Content-Transfer-Encoding: base64\r\n") mime.WriteString("Content-Disposition: attachment; filename=\"" + attaFileName + "\"\r\n\r\n") //讀取並編碼檔案內容 attaData, err := ioutil.ReadFile(attaFile) if err != nil { return err } b := make([]byte, base64.StdEncoding.EncodedLen(len(attaData))) base64.StdEncoding.Encode(b, attaData) mime.Write(b) //第二個附件 mime.WriteString(fmt.Sprintf("\r\n--%s\r\n", boundary)) mime.WriteString("Content-Type: text/plain\r\n") mime.WriteString("Content-Description: 附一個Text檔案\r\n") mime.WriteString("Content-Disposition: attachment; filename=\"test.txt\"\r\n\r\n") mime.WriteString("this is the attachment text") //郵件結束 mime.WriteString("\r\n--" + boundary + "--\r\n\r\n") fmt.Println(mime.String()) //傳送相關 smtpHost, _, err := net.SplitHostPort(emlSMTP) if err != nil { return err } auth := smtp.PlainAuth("", emlUser, emlPwd, smtpHost) return smtp.SendMail(emlSMTP, auth, emlUser, sendTo, mime.Bytes()) } /* 郵件內容: From:
[email protected]
<[email protected]> To: [email protected] CC: [email protected] Subject: 這是一封演示用的郵件 MIME-Version: 1.0 Content-Type: multipart/mixed; boundary=next message Content-Description: 這是一封帶附檔的郵件 --next message Content-Type: text/plain; charset=utf-8 This is a multipart message in MIME format. --next message Content-Type: multipart/alternative; boundary=boundaryHtml Content-Description: Message in alternative text and HTML forms --boundaryHtml Content-Type: text/html; charset=utf-8 <html><body> <p><img src="https://golang.org/doc/gopher/doc.png"></p><br/> <h1>最近有點消沉,也有點想家了.</h1> </body></html> --boundaryHtml-- --next message Content-Type: application/octet-stream Content-Description: 附一個Go檔案 Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="tconv.go" cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImZtdCIKCSJzdHJjb252IgoJInN0cmluZ3MiCikKCmZ1bmMgbWFpbigpIHsKCXMgOj0gIi0xIgoKCXYsIGVyciA6PSBzdHJjb252LkF0b2kocykKCglpZiBlcnIgIT0gbmlsIHsKCQlmbXQuUHJpbnRsbigiIGVycjoiLCBlcnIpCgl9IGVsc2UgewoJCWZtdC5QcmludGxuKCIgdjoiLCB2KQoJfQoKCWFjdGxpc3QgOj0gImEsYixjIgoKCXh4IDo9IHN0cmluZ3MuU3BsaXQoYWN0bGlzdCwgIiwiKQoJZm9yIF8sIHYgOj0gcmFuZ2UgeHggewoJCWZtdC5QcmludGxuKCIgdjoiLCB2KQoJfQoKfQo= --next message Content-Type: text/plain Content-Description: 附一個Text檔案 Content-Disposition: attachment; filename="test.txt" this is the attachment text --next message-- ➜ emlatt : */


收到的郵件: