【go语言发送电子邮件】go语言版发送电子邮件

2024-09-07 07:38

本文主要是介绍【go语言发送电子邮件】go语言版发送电子邮件,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、实现功能
用go语言发送一封邮件

二、实现源代码

package main
import ("net/smtp""fmt""strings"
)/**  user : example@example.com login smtp server user*  password: xxxxx login smtp server password*  host: smtp.example.com:port   smtp.163.com:25*  to: example@example.com;example1@163.com;example2@sina.com.cn;...*  subject:The subject of mail*  body: The content of mail*  mailtyoe: mail type html or text*/func SendMail(user, password, host, to, subject, body, mailtype string) error{hp := strings.Split(host, ":")auth := smtp.PlainAuth("", user, password, hp[0])var content_type stringif mailtype == "html" {content_type = "Content-Type: text/"+ mailtype + "; charset=UTF-8"}else{content_type = "Content-Type: text/plain" + "; charset=UTF-8"}msg := []byte("To: " + to + "\r\nFrom: " + user + "<"+ user +">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)send_to := strings.Split(to, ";")err := smtp.SendMail(host, auth, user, send_to, msg)return err
}func main() {user := "此处填写qq邮箱账号"password := "此处填写qq邮箱授权码"host := "smtp.qq.com:587"to := "18720081236m@sina.cn"subject := "Test send email by golang"body := `
`fmt.Println("send email")err := SendMail(user, password, host, to, subject, body, "text")if err != nil {fmt.Println("send mail error!")fmt.Println(err)}else{fmt.Println("send mail success!")}}

运行效果

"D:\Program Files (x86)\JetBrains\Gogland 171.3780.106\bin\runnerw.exe" D:/Go\bin\go.exe run D:/Go/code/src/awesomeProject/go_email.go
send email
send mail success!Process finished with exit code 0

版本2(发送多个人 ,HTML格式):

package main
import ("net/smtp""fmt""strings"
)/**  user : example@example.com login smtp server user*  password: xxxxx login smtp server password*  host: smtp.example.com:port   smtp.163.com:25*  to: example@example.com;example1@163.com;example2@sina.com.cn;...*  subject:The subject of mail*  body: The content of mail*  mailtyoe: mail type html or text*/func SendMail(user, password, host, to, subject, body, mailtype string) error{hp := strings.Split(host, ":")auth := smtp.PlainAuth("", user, password, hp[0])var content_type stringif mailtype == "html" {content_type = "Content-Type: text/"+ mailtype + "; charset=UTF-8"}else{content_type = "Content-Type: text/plain" + "; charset=UTF-8"}msg := []byte("To: " + to + "\r\nFrom: " + user + "<"+ user +">\r\nSubject: " + subject + "\r\n" + content_type + "\r\n\r\n" + body)send_to := strings.Split(to, ";")err := smtp.SendMail(host, auth, user, send_to, msg)return err
}func main() {user := "1973536419@qq.com"password := "XXXXXXXX"host := "smtp.qq.com:587"to := "defa.lai@cgtz.com;fang.chen@lg-finance.com";subject := "Test send email by golang"body := `<!DOCTYPE html><html><body><div style="text-align:center;width:78.78%;padding: 8px; line-height: 1.42857; vertical-align: top; border-top-width: 1px; border-top-color: rgb(221, 221, 221); background-color: #28a745;color:#fff"><strong>用户工资数据报表</strong></div><table border="1" style="width:80%;"><tr style="background-color:pink;text-align:center;"><th>月份</th><th>存款</th><th>工资</th><th>年薪</th></tr><tr  style="text-align:center;"><td>一月</td><td>1000 元</td><td>3000 元</td><td>12000元</td></tr><tr  style="text-align:center;"><td>二月</td><td>1500 元</td><td>4000 元</td><td>16000 元</td></tr></table></body></html>`fmt.Println("send email")err := SendMail(user, password, host, to, subject, body, "html")if err != nil {fmt.Println("send mail error!")fmt.Println(err)}else{fmt.Println("send mail success!")}}

这里写图片描述

这篇关于【go语言发送电子邮件】go语言版发送电子邮件的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1144474

相关文章

Go异常处理、泛型和文件操作实例代码

《Go异常处理、泛型和文件操作实例代码》Go语言的异常处理机制与传统的面向对象语言(如Java、C#)所使用的try-catch结构有所不同,它采用了自己独特的设计理念和方法,:本文主要介绍Go异... 目录一:异常处理常见的异常处理向上抛中断程序恢复程序二:泛型泛型函数泛型结构体泛型切片泛型 map三:文

C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解

《C++,C#,Rust,Go,Java,Python,JavaScript的性能对比全面讲解》:本文主要介绍C++,C#,Rust,Go,Java,Python,JavaScript性能对比全面... 目录编程语言性能对比、核心优势与最佳使用场景性能对比表格C++C#RustGoJavapythonjav

C语言逗号运算符和逗号表达式的使用小结

《C语言逗号运算符和逗号表达式的使用小结》本文详细介绍了C语言中的逗号运算符和逗号表达式,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习... 在C语言中逗号“,”也是一种运算符,称为逗号运算符。 其功能是把两个表达式连接其一般形式为:表达

Go语言实现桥接模式

《Go语言实现桥接模式》桥接模式是一种结构型设计模式,它将抽象部分与实现部分分离,使它们可以独立地变化,本文就来介绍一下了Go语言实现桥接模式,感兴趣的可以了解一下... 目录简介核心概念为什么使用桥接模式?应用场景案例分析步骤一:定义实现接口步骤二:创建具体实现类步骤三:定义抽象类步骤四:创建扩展抽象类步

GO语言实现串口简单通讯

《GO语言实现串口简单通讯》本文分享了使用Go语言进行串口通讯的实践过程,详细介绍了串口配置、数据发送与接收的代码实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要... 目录背景串口通讯代码代码块分解解析完整代码运行结果背景最近再学习 go 语言,在某宝用5块钱买了个

Go 使用环境变量的实现小结

《Go使用环境变量的实现小结》作为软件开发人员,在项目中管理配置变量的重要性,本文主要介绍在Golang中处理环境变量的强大工具github.com/joho/godotenv包,利用这个包,你可以... 目录步js骤 1:安装步骤 2:制作 .env 文件步骤android 3:加载环境变量步骤 4:利用

GO语言zap日志库理解和使用方法示例

《GO语言zap日志库理解和使用方法示例》Zap是一个高性能、结构化日志库,专为Go语言设计,它由Uber开源,并且在Go社区中非常受欢迎,:本文主要介绍GO语言zap日志库理解和使用方法的相关资... 目录1. zap日志库介绍2.安装zap库3.配置日志记录器3.1 Logger3.2 Sugared

Go语言中如何进行数据库查询操作

《Go语言中如何进行数据库查询操作》在Go语言中,与数据库交互通常通过使用数据库驱动来实现,Go语言支持多种数据库,如MySQL、PostgreSQL、SQLite等,每种数据库都有其对应的官方或第三... 查询函数QueryRow和Query详细对比特性QueryRowQuery返回值数量1个:*sql

深入理解Go之==的使用

《深入理解Go之==的使用》本文主要介绍了深入理解Go之==的使用,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录概述类型基本类型复合类型引用类型接口类型使用type定义的类型不可比较性谈谈map总结概述相信==判等操作,大

GO语言中gox交叉编译的实现

《GO语言中gox交叉编译的实现》本文主要介绍了GO语言中gox交叉编译的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧... 目录一、安装二、使用三、遇到的问题1、开启CGO2、修改环境变量最近在工作中使用GO语言进行编码开发,因