package main import (
"net/smtp"
"bytes"
"time"
"io/ioutil"
"encoding/base64"
"strings"
"log"
) // define email interface, and implemented auth and send method
type Mail interface {
Auth()
Send(message Message) error
} type SendMail struct {
user string
password string
host string
port string
auth smtp.Auth
} type Attachment struct {
name string
contentType string
withFile bool
} type Message struct {
from string
to []string
cc []string
bcc []string
subject string
body string
contentType string
attachment Attachment
} func main() {
var mail Mail
mail = &SendMail{user: "chunyunzeng@hotmail.com", password: "password", host: "smtp.mxhichina.com", port: "25"}
message := Message{from: "chunyunzeng@hotmail.com",
to: []string{"850808158@qq.com"},
cc: []string{},
bcc: []string{},
subject: "HELLO WORLD",
body: "",
contentType: "text/plain;charset=utf-8",
attachment: Attachment{
name: "test.jpg",
contentType: "image/jpg",
withFile: true,
},
}
mail.Send(message)
} func (mail *SendMail) Auth() {
mail.auth = smtp.PlainAuth("", mail.user, mail.password, mail.host)
} func (mail SendMail) Send(message Message) error {
mail.Auth()
buffer := bytes.NewBuffer(nil)
boundary := "GoBoundary"
Header := make(map[string]string)
Header["From"] = message.from
Header["To"] = strings.Join(message.to, ";")
Header["Cc"] = strings.Join(message.cc, ";")
Header["Bcc"] = strings.Join(message.bcc, ";")
Header["Subject"] = message.subject
Header["Content-Type"] = "multipart/mixed;boundary=" + boundary
Header["Mime-Version"] = "1.0"
Header["Date"] = time.Now().String()
mail.writeHeader(buffer, Header) body := "\r\n--" + boundary + "\r\n"
body += "Content-Type:" + message.contentType + "\r\n"
body += "\r\n" + message.body + "\r\n"
buffer.WriteString(body) if message.attachment.withFile {
attachment := "\r\n--" + boundary + "\r\n"
attachment += "Content-Transfer-Encoding:base64\r\n"
attachment += "Content-Disposition:attachment\r\n"
attachment += "Content-Type:" + message.attachment.contentType + ";name=\"" + message.attachment.name + "\"\r\n"
buffer.WriteString(attachment)
defer func() {
if err := recover(); err != nil {
log.Fatalln(err)
}
}()
mail.writeFile(buffer, message.attachment.name)
} buffer.WriteString("\r\n--" + boundary + "--")
smtp.SendMail(mail.host+":"+mail.port, mail.auth, message.from, message.to, buffer.Bytes())
return nil
} func (mail SendMail) writeHeader(buffer *bytes.Buffer, Header map[string]string) string {
header := ""
for key, value := range Header {
header += key + ":" + value + "\r\n"
}
header += "\r\n"
buffer.WriteString(header)
return header
} // read and write the file to buffer
func (mail SendMail) writeFile(buffer *bytes.Buffer, fileName string) {
file, err := ioutil.ReadFile(fileName)
if err != nil {
panic(err.Error())
}
payload := make([]byte, base64.StdEncoding.EncodedLen(len(file)))
base64.StdEncoding.Encode(payload, file)
buffer.WriteString("\r\n")
for index, line := 0, len(payload); index < line; index++ {
buffer.WriteByte(payload[index])
if (index+1)%76 == 0 {
buffer.WriteString("\r\n")
}
}
}

Go smtp发送邮件,带附件的更多相关文章

  1. 使用System.Net.Mail中的SMTP发送邮件(带附件)

    System.Net.Mail 使用简单邮件传输协议SMTP异步发送邮件 想要实现SMTP发送邮件,你需要了解这些类 SmtpClient :使用配置文件设置来初始化 SmtpClient类的新实例. ...

  2. Java发送邮件(带附件)

    实现java发送邮件的过程大体有以下几步: 准备一个properties文件,该文件中存放SMTP服务器地址等参数. 利用properties创建一个Session对象 利用Session创建Mess ...

  3. centos 使用mutt发送邮件带附件

    1.安装mutt工具 yum install -y mutt 2.使用mutt发邮件并带附件echo "统计日志" | /usr/bin/mutt -s "统计日志&qu ...

  4. python 发送邮件 带附件

    # coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/26' # __Desc__ = 实现发送带有各种附件类型的邮件 ...

  5. java发送邮件带附件

    package com.smtp; import java.util.Vector; public class MailBean { private String to; // 收件人 private ...

  6. smtp发送带附件的邮件(直接将string类型结果保存为附件)

    该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好    MailMessage mmsg = new MailMessage();    mmsg.Subject = " ...

  7. Python发送邮件(带附件的)

    有时候做自动化测试任务,任务完成后,需要将结果自动发送一封邮件,这里用到smtplib模块,直接导入就行,这里以163邮箱为例,需要用到授权码,我用类写一下: 如果是发送qq邮箱,要将smtp 改成s ...

  8. python3.7发送邮件带附件

    代码: 1 # -*- coding: utf-8 -*- 2 3 import smtplib, ssl 4 from email.mime.text import MIMEText 5 from ...

  9. VC++ 使用ShellExecute函数调用邮箱客户端发送邮件(可以带附件)

      之前写过一篇博文,通过MAPI实现调用邮箱客户端发送邮件带附件,当时对ShellExecute研究不深,以为ShellExecute不能带附件,因为项目需求原因(MAPI只能调用Foxmail和O ...

  10. php中PHPMailer发送带附件的电子邮件方法

    摘要: 本文讲的是php中PHPMailer发送带附件的电子邮件方法, .首先到http://phpmailer.worxware.com/ 下载最新版本的程序包 2.下载完成后,找到class.ph ...

随机推荐

  1. 分布式系列十三: nginx

    nginx偏运维, 不过作为开发应该了解它能做什么事情, 其作为技术架构的一部分必不可少 正向代理和反向代理 正向代理是代理的客户端, 反向代理是代理的服务端. nginx就是一款可以作反向代理的we ...

  2. 论文笔记:Image Smoothing via L0 Gradient Minimization

    今天要分享的这篇论文是我个人最喜欢的论文之一,它的思想简单.巧妙,而且效果还相当不错.这篇论文借助数学上的 \(L_0\) 范数工具对图像进行平滑,同时保留重要的边缘特征,可以实现类似水彩画的效果(见 ...

  3. 使用scrapy爬虫,爬取17k小说网的案例-方法二

    楼主准备爬取此页面的小说,此页面一共有125章 我们点击进去第一章和第一百二十五章发现了一个规律 我们看到此链接的  http://www.17k.com/chapter/271047/6336386 ...

  4. webpack 打包报错:One CLI for webpack must be installed. These are recommended choices, delivered as separate packages

    webpack 打包报错: One CLI for webpack must be installed. These are recommended choices, delivered as sep ...

  5. [NOIP2017提高组]小凯的疑惑-扩展欧几里得

    #include<bits/stdc++.h> using namespace std; long long a,b,x,y,ans,tmp; inline void ex_gcd(lon ...

  6. Java_File类

    File类以抽象的方式代表文件名和目录路径.该类主要用于文件和目录的创建.查找.删除等.先来看一下File的构造方法: // 通过将给定的路径名字符串转换为抽象路径名来创建新的 File实例 File ...

  7. adi i2s 提供的axi_lite接口说明

    总共定义了4个寄存器,位宽32位,也就是 偏移地址*4以下是PS写数据when 0 => I2S_RESET_REG <= wr_data; when 1 => I2S_CONTRO ...

  8. curl常用命令备忘

    #####(输出请求头信息) curl -I xxx-Pro:test xxx$ curl -I https://www.baidu.com/ HTTP/1.1 200 OK Accept-Range ...

  9. C# 高级编程05----常用修饰符

    常用修饰符: 1.访问可见性修饰符 修饰符 应用于 说明 public 类型或成员 任何代码都可访问 protected 类型或内嵌类型的成员 只有子类能访问 internal 类型或成员 只能在包含 ...

  10. EtherNet/IP 协议结构

    一.Ethernet/IP 协议 将标准的TCP/IP以太网延伸 到工业实时控制并和通用工业协议(CIP)结合,将很好地帮助用户获得更加开放集成的工业自动化和信息化的整体解决方案.EtherNet/I ...