Go smtp发送邮件,带附件
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发送邮件,带附件的更多相关文章
- 使用System.Net.Mail中的SMTP发送邮件(带附件)
System.Net.Mail 使用简单邮件传输协议SMTP异步发送邮件 想要实现SMTP发送邮件,你需要了解这些类 SmtpClient :使用配置文件设置来初始化 SmtpClient类的新实例. ...
- Java发送邮件(带附件)
实现java发送邮件的过程大体有以下几步: 准备一个properties文件,该文件中存放SMTP服务器地址等参数. 利用properties创建一个Session对象 利用Session创建Mess ...
- centos 使用mutt发送邮件带附件
1.安装mutt工具 yum install -y mutt 2.使用mutt发邮件并带附件echo "统计日志" | /usr/bin/mutt -s "统计日志&qu ...
- python 发送邮件 带附件
# coding:utf-8 # __author__ = 'Mark sinoberg' # __date__ = '2016/5/26' # __Desc__ = 实现发送带有各种附件类型的邮件 ...
- java发送邮件带附件
package com.smtp; import java.util.Vector; public class MailBean { private String to; // 收件人 private ...
- smtp发送带附件的邮件(直接将string类型结果保存为附件)
该方式直接保存为HTML文件,也可以是文本文件,其它格式效果不是很好 MailMessage mmsg = new MailMessage(); mmsg.Subject = " ...
- Python发送邮件(带附件的)
有时候做自动化测试任务,任务完成后,需要将结果自动发送一封邮件,这里用到smtplib模块,直接导入就行,这里以163邮箱为例,需要用到授权码,我用类写一下: 如果是发送qq邮箱,要将smtp 改成s ...
- python3.7发送邮件带附件
代码: 1 # -*- coding: utf-8 -*- 2 3 import smtplib, ssl 4 from email.mime.text import MIMEText 5 from ...
- VC++ 使用ShellExecute函数调用邮箱客户端发送邮件(可以带附件)
之前写过一篇博文,通过MAPI实现调用邮箱客户端发送邮件带附件,当时对ShellExecute研究不深,以为ShellExecute不能带附件,因为项目需求原因(MAPI只能调用Foxmail和O ...
- php中PHPMailer发送带附件的电子邮件方法
摘要: 本文讲的是php中PHPMailer发送带附件的电子邮件方法, .首先到http://phpmailer.worxware.com/ 下载最新版本的程序包 2.下载完成后,找到class.ph ...
随机推荐
- 2018-2019-2 20165234 《网络对抗技术》 Exp3 免杀原理与实践
实验三 免杀原理与实践 实验内容 1.正确使用msf编码器,msfvenom生成如jar之类的其他文件,veil-evasion,自己利用shellcode编程等免杀工具或技巧:(1.5分) 2.通过 ...
- 基于Python的Webservice开发(一)-简介
之前为了解决Webservice的开发,直接用Python自带的CGI模块挂在IIS上. 但是该方式开发Soap的接口,需要大量的开发,而且安全方面也存在很多问题. 我推荐关于用Python开发Web ...
- Input子系统与多点触摸技术-3【转】
转自:https://blog.csdn.net/u012839187/article/details/77335941 版权声明:本文为博主原创文章,欢迎转载,转载请注明转载地址 https://b ...
- 【easy】198. House Robber 123总结……
题目一: 一个极其简单的动态规划. class Solution { public: int rob(vector<int>& nums) { ; // 表示没有选择当前house ...
- 【easy】257. Binary Tree Paths 二叉树找到所有路径
http://blog.csdn.net/crazy1235/article/details/51474128 花样做二叉树的题……居然还是不会么…… /** * Definition for a b ...
- SpingBoot+Mybaits+Vue,更新学习
1.DTO 2.实体类 3.Controller层,UpdatePrize/{id}为接口 4.Service层 5.Impl实现层 之后就可以更新数据了. 6.Vue链接接口 7.请求,解析,返回值 ...
- python课程分享2-伊嬛
2.4 模块 2.4.1 模块的概念 在计算机程序的开发过程中,随着程序代码越写越多,在一个文件里代码就会越来越长,越来越不容易维护. 为了编写可维护的代码,使用者可以把很多函数分组,分别放到不同 ...
- W3CSchool实战闯关笔记(JavaScript)
//handsome /** *ugly **/ 第一关注释 // 举例 var myName; // Define myName below this line 第二关声明变量 // Setup v ...
- 在ubuntu18 安装nginx过程,以及遇到的错误!
1.下载 nginx下载的地址:https://nginx.org/en/download.html 2.解压 指定一个目录下面放置下载的文件,然后解压文件 命令:tar zxvf nginx-1.6 ...
- 学习笔记_J2EE_Spring(一)_入门
3. Spring概述 3.1. Spring是什么 Spring是一个优秀的高可用的JavaEE轻量级开发框架.提供一站式开发解决方案. 3.2. Spring框架出现的背景 在世 ...