package main
import (
"bytes"
"crypto/tls"
"encoding/base64"
"errors"
"fmt"
"io"
"mime"
"mime/multipart"
"mime/quotedprintable"
"net/smtp"
"net/textproto"
"os"
"path/filepath"
"strings"
)const memmaxsize = (1
<< 20) * 10
func main()
size, err := x.len()
iferr != nil
var file io.readwriter
if size >= memmaxsize
defer os.remove(temp)
} else
x.writer(file)
auth := smtp.plainauth("", "test", "123456", "smtp.163.com")
err = send(*x, "smtp.163.com:25", auth, file)
iferr != nil
if c, ok := file.(io.closer); ok
}//傳送訊息
func send(msg email, addr string, auth smtp.auth, body io.reader) error
client, err := smtp.dial(addr)
iferr != nil
defer client.close()
host := strings.split(addr, ":")[0]
iferr = client.hello(host); err != nil
if ok, _ := client.extension("starttls"); ok
iferr = client.starttls(config); err != nil }if
err = client.auth(auth); err != nil
iferr = client.mail(msg.from); err != nil
for _, addr := range to
}w, err := client.data()
iferr != nil
if value, ok := body.(io.seeker); ok
_, err = io.copy(w, body)
iferr != nil
err = w.close()
iferr != nil
return client.quit()
}//新增附件
func attach(w *multipart.writer, filename string) (err
error) else
basename := filepath.base(filename)
header.set("content-disposition", fmt.sprintf("attachment;\r\n filename=\"%s\"", basename))
header.set("content-id", fmt.sprintf("<%s>", basename))
header.set("content-transfer-encoding", "base64")
file, err := os.open(filename)
iferr != nil
defer file.close()
mw, err := w.createpart(header)
iferr != nil
return base64wrap(mw, file)
}type email struct
//返回基礎的頭資訊
func (e *email) headers() (textproto.mimeheader, error)
if _, ok := res["subject"]; !ok && e.subject != ""
if _, ok := res["from"]; !ok
return res, nil
}//編碼郵件內容
func (e *email) writer(datawriter io.writer) error
w := multipart.newwriter(datawriter)
headers.set("content-type", "multipart/mixed;\r\n boundary="+w.boundary())
headertobytes(datawriter, headers)
io.writestring(datawriter, "\r\n")
fmt.fprintf(datawriter, "--%s\r\n", w.boundary())
header := textproto.mimeheader{}
if e.content != "" || e.contentpath != ""
qp := quotedprintable.newwriter(datawriter)
if _, err := qp.write(byte(e.content)); err != nil
iferr := qp.close(); err != nil
} else
qp := quotedprintable.newwriter(datawriter)
file, err := os.open(e.contentpath)
iferr != nil
defer file.close()
_, err = io.copy(qp, file)
iferr != nil
iferr := qp.close(); err != nil }if
err := subwriter.close(); err != nil
}if e.attachments != "" }}
return nil
}//檢視一下傳送的內容大小,如果過超過一定大小則,使用磁碟檔案做臨時
func (e *email) len() (int64, error) else
l += stat.size()
}if e.attachments != ""
l += stat.size()}}
return l, nil
}//根據頭資訊建立附件
func headertobytes(w io.writer, header textproto.mimeheader)
io.writestring(w, "\r\n")}}
}//編碼成每行固定長度的base64訊息
func base64wrap(w io.writer, r io.reader) error
return err
}if n == maxraw else
}}
八 傳送郵件
傳送郵件 如下 from email.mime.text import mimetext from email.header import header import smtplib import datetime from poseidon.myutil import myutil import ...
5 傳送模板訊息
詳情參看 傳送模板訊息 介面呼叫請求說明 http請求方式 post post資料示例如下 data keynote1 keynote2 keynote3 remark createmunu url,params string url send template url.replace access...
SprongBoot傳送郵件(三)傳送附件郵件
還是在 sprongboot傳送郵件 一 傳送簡單文字郵件helloworld 的配置基礎上新增如下 param to param subject param content param filepath 檔案路徑 throws messagingexception public void send...