利用smtp协议发送带附件的邮件
来源:互联网 发布:网络摄像头容量计算 编辑:程序博客网 时间:2024/05/21 15:46
之前写过一个发邮件的,不过没带附档,今天再看了下smtp协议,做了个带附档的邮件发送例子,也就这样吧。
package main/*了解下smtp协议,并做了个小演示: 利用Go自带的net/smtp包,发送带附档的邮件。Author: XCLDate: 2016-5-7Blog: http://blog.csdn.net/xcl168*/import ("bytes""encoding/base64""fmt""io/ioutil""net""net/smtp""strings")const (emlUser = "xcl@xxx.com"emlPwd = "-------"emlSMTP = "smtp.xxx.com:25")func main() {err := eml()if err != nil {fmt.Println(" err:", err)}}// 发普通文本邮件func eml() error {to := "xcl@xxx.com"cc := "cc@xxx.com"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: alert@xxx.com<alert@xxx.com>To: xcl@xxx.comCC: cc@xxx.comSubject: 这是一封演示用的邮件MIME-Version: 1.0Content-Type: multipart/mixed; boundary=next messageContent-Description: 这是一封带附档的邮件--next messageContent-Type: text/plain; charset=utf-8This is a multipart message in MIME format.--next messageContent-Type: multipart/alternative; boundary=boundaryHtmlContent-Description: Message in alternative text and HTML forms--boundaryHtmlContent-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 messageContent-Type: application/octet-streamContent-Description: 附一个Go文件Content-Transfer-Encoding: base64Content-Disposition: attachment; filename="tconv.go"cGFja2FnZSBtYWluCgppbXBvcnQgKAoJImZtdCIKCSJzdHJjb252IgoJInN0cmluZ3MiCikKCmZ1bmMgbWFpbigpIHsKCXMgOj0gIi0xIgoKCXYsIGVyciA6PSBzdHJjb252LkF0b2kocykKCglpZiBlcnIgIT0gbmlsIHsKCQlmbXQuUHJpbnRsbigiIGVycjoiLCBlcnIpCgl9IGVsc2UgewoJCWZtdC5QcmludGxuKCIgdjoiLCB2KQoJfQoKCWFjdGxpc3QgOj0gImEsYixjIgoKCXh4IDo9IHN0cmluZ3MuU3BsaXQoYWN0bGlzdCwgIiwiKQoJZm9yIF8sIHYgOj0gcmFuZ2UgeHggewoJCWZtdC5QcmludGxuKCIgdjoiLCB2KQoJfQoKfQo=--next messageContent-Type: text/plainContent-Description: 附一个Text文件Content-Disposition: attachment; filename="test.txt"this is the attachment text--next message--➜ emlatt :*/
收到的邮件:
参考URL: https://msdn.microsoft.com/en-us/library/ms526560(v=exchg.10).aspx
BLOG: http://blog.csdn.net/xcl168
0 0
- 利用smtp协议发送带附件的邮件
- Python发送带附件的SMTP邮件
- 用 SMTP 发送带验证带附件的邮件
- ruby基于SMTP发送带附件的邮件
- 资源马克-20161124- php smtp 带附件的邮件发送
- 用Net::SMTP发送带附件的邮件
- C#使用smtp发送邮件带附件
- oracle 发送带附件邮件(smtp)
- oracle 发送带附件邮件(smtp)
- Python SMTP发送邮件 - 带附件
- SMTP协议发送邮件和附件
- SMTP协议发送邮件和附件
- SMTP邮件传输协议发送邮件和附件
- 发送带附件的邮件
- 带附件的邮件发送
- Oracle9中利用javamail发送带附件的邮件
- linux 下 利用mutt 发送 带附件的邮件
- 利用spring mail 发送带附件的邮件
- (OK) Fedora 23——CORE——docker——(2)——> install-quagga
- 文章标题
- 最小生成树Kruskal算法+并查集实现
- 多态
- (OK) Fedora 23——CORE——docker——(3)——> install-docker
- 利用smtp协议发送带附件的邮件
- backgroundColor的背景颜色alpha透明度设置
- (OK) Fedora 23——CORE——docker——(4)——> iptables_core.sh
- Android项目 植物大战僵尸
- openjdk7的编译
- Tensorflow 分布式部署简介
- 从头认识多线程-2.14 由同步的synchronized (newobject()) 引起的异步现象和脏读
- (OK) Fedora 23——CORE——docker——(5)——> install-core
- 不同单词个数