【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语言中,短变量声明是一种简洁的变量声明方式,使用:=运算符,可以自动推断变量类型,下面就来具体介绍一下如何使用,感兴趣的可以了解一下... 目录基本语法功能特点与var的区别适用场景注意事项基本语法variableName := value功能特点1、自动类型推

GO语言中函数命名返回值的使用

《GO语言中函数命名返回值的使用》在Go语言中,函数可以为其返回值指定名称,这被称为命名返回值或命名返回参数,这种特性可以使代码更清晰,特别是在返回多个值时,感兴趣的可以了解一下... 目录基本语法函数命名返回特点代码示例命名特点基本语法func functionName(parameters) (nam

基于Python实现自动化邮件发送系统的完整指南

《基于Python实现自动化邮件发送系统的完整指南》在现代软件开发和自动化流程中,邮件通知是一个常见且实用的功能,无论是用于发送报告、告警信息还是用户提醒,通过Python实现自动化的邮件发送功能都能... 目录一、前言:二、项目概述三、配置文件 `.env` 解析四、代码结构解析1. 导入模块2. 加载环

使用Python的requests库来发送HTTP请求的操作指南

《使用Python的requests库来发送HTTP请求的操作指南》使用Python的requests库发送HTTP请求是非常简单和直观的,requests库提供了丰富的API,可以发送各种类型的HT... 目录前言1. 安装 requests 库2. 发送 GET 请求3. 发送 POST 请求4. 发送

Go之errors.New和fmt.Errorf 的区别小结

《Go之errors.New和fmt.Errorf的区别小结》本文主要介绍了Go之errors.New和fmt.Errorf的区别,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考... 目录error的基本用法1. 获取错误信息2. 在条件判断中使用基本区别1.函数签名2.使用场景详细对

Go语言连接MySQL数据库执行基本的增删改查

《Go语言连接MySQL数据库执行基本的增删改查》在后端开发中,MySQL是最常用的关系型数据库之一,本文主要为大家详细介绍了如何使用Go连接MySQL数据库并执行基本的增删改查吧... 目录Go语言连接mysql数据库准备工作安装 MySQL 驱动代码实现运行结果注意事项Go语言执行基本的增删改查准备工作

Go中select多路复用的实现示例

《Go中select多路复用的实现示例》Go的select用于多通道通信,实现多路复用,支持随机选择、超时控制及非阻塞操作,建议合理使用以避免协程泄漏和死循环,感兴趣的可以了解一下... 目录一、什么是select基本语法:二、select 使用示例示例1:监听多个通道输入三、select的特性四、使用se

Go语言使用Gin处理路由参数和查询参数

《Go语言使用Gin处理路由参数和查询参数》在WebAPI开发中,处理路由参数(PathParameter)和查询参数(QueryParameter)是非常常见的需求,下面我们就来看看Go语言... 目录一、路由参数 vs 查询参数二、Gin 获取路由参数和查询参数三、示例代码四、运行与测试1. 测试编程路

基于Python编写自动化邮件发送程序(进阶版)

《基于Python编写自动化邮件发送程序(进阶版)》在数字化时代,自动化邮件发送功能已成为企业和个人提升工作效率的重要工具,本文将使用Python编写一个简单的自动化邮件发送程序,希望对大家有所帮助... 目录理解SMTP协议基础配置开发环境构建邮件发送函数核心逻辑实现完整发送流程添加附件支持功能实现htm

Go语言使用net/http构建一个RESTful API的示例代码

《Go语言使用net/http构建一个RESTfulAPI的示例代码》Go的标准库net/http提供了构建Web服务所需的强大功能,虽然众多第三方框架(如Gin、Echo)已经封装了很多功能,但... 目录引言一、什么是 RESTful API?二、实战目标:用户信息管理 API三、代码实现1. 用户数据