• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >Golang > Go语言通过smtp发送邮件的方法

Go语言通过smtp发送邮件的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了go语言方法,go语言,go语言环境搭建,go语言程序设计,let it go 25种语言等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了Go语言通过smtp发送邮件的方法。分享给大家供大家参考。具体实现方法如下:

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 string
 if 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 := "xxxx@163.com"
 password := "xxxx"
 host := "smtp.163.com:25"
 to := "xxxx@gmail.com;ssssss@gmail.com"

 subject := "Test send email by golang"

 body := `
 <html>
 <body>
 <h3>
 "Test send email by golang"
 </h3>
 </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!")
 }
}</div>

希望本文所述对大家的Go语言程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Go语言编程中字符串切割方法小结
  • Go语言中字符串的查找方法小结
  • go语言获取系统盘符的方法
  • go语言使用scp的方法实例分析
  • go语言使用RC4加密的方法
  • Go语言单链表实现方法
  • go语言中时间戳格式化的方法
  • go语言操作redis连接池的方法
  • go语言检测文件是否存在的方法
  • Go语言生成素数的方法

相关文章

  • Go语言MessageBox用法实例
  • Golang学习笔记(五):函数
  • Go语言实现的web爬虫实例
  • 在Golang中使用C语言代码实例
  • golang守护进程用法示例
  • Go语言中更优雅的错误处理
  • golang实现unicode转换为字符串string的方法
  • Go语言按字节截取字符串的方法
  • GO语言利用K近邻算法实现小说鉴黄
  • 简单了解Go语言中函数作为值以及函数闭包的使用

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • go语言在请求http时加入自定义http header的方法
    • Go语言struct类型详解
    • Golang算法问题之整数拆分实现方法分析
    • Golang学习笔记(五):函数
    • Go语言中使用 buffered channel 实现线程安全的 pool
    • Golang继承模拟实例详解
    • GO语言常用的文件读取方式
    • Go语言中io.Reader和io.Writer的详解与实现
    • golang实现通过smtp发送电子邮件的方法
    • Go并发编程实践

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有