【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

相关文章

Python办公自动化实战之打造智能邮件发送工具

《Python办公自动化实战之打造智能邮件发送工具》在数字化办公场景中,邮件自动化是提升工作效率的关键技能,本文将演示如何使用Python的smtplib和email库构建一个支持图文混排,多附件,多... 目录前言一、基础配置:搭建邮件发送框架1.1 邮箱服务准备1.2 核心库导入1.3 基础发送函数二、

go中的时间处理过程

《go中的时间处理过程》:本文主要介绍go中的时间处理过程,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 获取当前时间2 获取当前时间戳3 获取当前时间的字符串格式4 相互转化4.1 时间戳转时间字符串 (int64 > string)4.2 时间字符串转时间

Go语言中make和new的区别及说明

《Go语言中make和new的区别及说明》:本文主要介绍Go语言中make和new的区别及说明,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录1 概述2 new 函数2.1 功能2.2 语法2.3 初始化案例3 make 函数3.1 功能3.2 语法3.3 初始化

Go语言中nil判断的注意事项(最新推荐)

《Go语言中nil判断的注意事项(最新推荐)》本文给大家介绍Go语言中nil判断的注意事项,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友参考下吧... 目录1.接口变量的特殊行为2.nil的合法类型3.nil值的实用行为4.自定义类型与nil5.反射判断nil6.函数返回的

Go语言数据库编程GORM 的基本使用详解

《Go语言数据库编程GORM的基本使用详解》GORM是Go语言流行的ORM框架,封装database/sql,支持自动迁移、关联、事务等,提供CRUD、条件查询、钩子函数、日志等功能,简化数据库操作... 目录一、安装与初始化1. 安装 GORM 及数据库驱动2. 建立数据库连接二、定义模型结构体三、自动迁

java向微信服务号发送消息的完整步骤实例

《java向微信服务号发送消息的完整步骤实例》:本文主要介绍java向微信服务号发送消息的相关资料,包括申请测试号获取appID/appsecret、关注公众号获取openID、配置消息模板及代码... 目录步骤1. 申请测试系统2. 公众号账号信息3. 关注测试号二维码4. 消息模板接口5. Java测试

Go语言代码格式化的技巧分享

《Go语言代码格式化的技巧分享》在Go语言的开发过程中,代码格式化是一个看似细微却至关重要的环节,良好的代码格式化不仅能提升代码的可读性,还能促进团队协作,减少因代码风格差异引发的问题,Go在代码格式... 目录一、Go 语言代码格式化的重要性二、Go 语言代码格式化工具:gofmt 与 go fmt(一)

Go学习记录之runtime包深入解析

《Go学习记录之runtime包深入解析》Go语言runtime包管理运行时环境,涵盖goroutine调度、内存分配、垃圾回收、类型信息等核心功能,:本文主要介绍Go学习记录之runtime包的... 目录前言:一、runtime包内容学习1、作用:① Goroutine和并发控制:② 垃圾回收:③ 栈和

Go语言中泄漏缓冲区的问题解决

《Go语言中泄漏缓冲区的问题解决》缓冲区是一种常见的数据结构,常被用于在不同的并发单元之间传递数据,然而,若缓冲区使用不当,就可能引发泄漏缓冲区问题,本文就来介绍一下问题的解决,感兴趣的可以了解一下... 目录引言泄漏缓冲区的基本概念代码示例:泄漏缓冲区的产生项目场景:Web 服务器中的请求缓冲场景描述代码

Go语言如何判断两张图片的相似度

《Go语言如何判断两张图片的相似度》这篇文章主要为大家详细介绍了Go语言如何中实现判断两张图片的相似度的两种方法,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 在介绍技术细节前,我们先来看看图片对比在哪些场景下可以用得到:图片去重:自动删除重复图片,为存储空间"瘦身"。想象你是一个