zoukankan      html  css  js  c++  java
  • golang 如何发送邮件?

    // SendMail connects to the server at addr, switches to TLS if
    // possible, authenticates with the optional mechanism a if possible,
    // and then sends an email from address from, to addresses to, with
    // message msg.
    // The addr must include a port, as in "mail.example.com:smtp".
    //
    // The addresses in the to parameter are the SMTP RCPT addresses.
    //
    // The msg parameter should be an RFC 822-style email with headers
    // first, a blank line, and then the message body. The lines of msg
    // should be CRLF terminated. The msg headers should usually include
    // fields such as "From", "To", "Subject", and "Cc".  Sending "Bcc"
    // messages is accomplished by including an email address in the to
    // parameter but not including it in the msg headers.
    //
    // The SendMail function and the net/smtp package are low-level
    // mechanisms and provide no support for DKIM signing, MIME
    // attachments (see the mime/multipart package), or other mail
    // functionality. Higher-level packages exist outside of the standard
    // library.
    函数原型
    func SendMail(addr string, a Auth, from string, to []string, msg []byte) error
    
    /*!
    username 发送者邮件
    password 授权码
    host 主机地址 smtp.qq.com:587 或 smtp.qq.com:25
    to 接收邮箱 多个接收邮箱使用 ; 隔开
    name 发送人名称
    subject 发送主题
    body 发送内容
    mailType 发送邮件内容类型
    */
    func SendMail(username, password, host, to, name,subject, body, mailType string) error {
    	hp := strings.Split(host, ":")
    	auth := smtp.PlainAuth("", username, password,hp[0])
    	var contentType string
    	if mailType == "html" {
    		contentType = "Content-Type: text/" + mailType + "; charset=UTF-8"
    	} else {
    		contentType = "Content-Type: text/plain" + "; charset=UTF-8"
    	}
    	msg := []byte("To: " + to + "
    From: " + name + "<" + username + ">
    Subject: " + subject + "
    " + contentType + "
    
    " + body)
    	sendTo := strings.Split(to, ";")
    	err := smtp.SendMail(host, auth, username, sendTo, msg)
    	return err
    }
    
    func main()  {
    	err := SendMail(
    		"zer******0115@foxmail.com",
    		"kcll******hjbbih",
    		"smtp.qq.com:587",
    		"**********@qq.com",
    		"学海无涯",
    		"测试",
    		"这是一封测试邮件",
    		"html",
    		)
    	if err != nil{
    		log.Fatal(err)
    	}
    }
    
  • 相关阅读:
    解决Centos7下中文显示乱码
    Pycharm2019.2.1永久激活
    window 共享打印机
    看着前车轮胎能出库
    计算之道 (置换的玩笑)搜索
    GIS+=地理信息+云计算技术——私有云架构设计(2)网络资源规划
    串的模式匹配
    1.Linux下libevent和memcached安装
    STL--H
    【数据结构与算法】(二) c 语言链表的简单操作
  • 原文地址:https://www.cnblogs.com/cheungxiongwei/p/12318310.html
Copyright © 2011-2022 走看看