• GoLang邮件发送Demo(继上篇msmtp)


      GoLang越来越被看好,流行只是时间的问题了,闲暇时间玩玩Go。

      下面是发送邮件的demo,替换自己的邮箱,完全可以正常跑起来,用的是net/smtp包:

    package main
    
    import (
    	"fmt"
    	"net/smtp"
    	"strings"
    )
    
    func SendToMail(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 + "
    From: " + user + "
    Subject: " + subject + "
    " + content_type + "
    
    " + body)
    	err := smtp.SendMail(host, auth, user, []string{to}, msg)
    	return err
    }
    
    func main() {
    	user := "591592159@qq.com"
    	password := "********"
    	host := "smtp.qq.com:587"
    	to := "591592159@qq.com"
    
    	subject := "使用Golang发送邮件"
    
    	body := `
    <html>
    <body>
    <h3>
    Test send to emailTest send to email
    </h3>
    </body>
    </html>
    `
    	fmt.Println("send email")
    	err := SendToMail(user, password, host, to, subject, body, "html")
    	if err != nil {
    		fmt.Println("Send mail error!")
    		fmt.Println(err)
    	} else {
    		fmt.Println("Send mail success!")
    	}
    
    }
    

      

      GoLang的基础包还是挺多的,也有github上的各种库,使用很方便!

  • 相关阅读:
    VBA_headers_mapping
    pandas_知识总结_基础
    python_super()及继承顺序
    python_端口扫描
    python_socket登陆验证_明文
    python_sys.argv的使用
    python_os_shutil_获取文件夹下所有文件的大小
    python_面向对象_组合
    魔术球问题 【网络流24题】【建图技巧】
    牛客&科大讯飞杯&SHU、L动物森友会【二分】【网络流】
  • 原文地址:https://www.cnblogs.com/purelightme/p/7795736.html
Copyright © 2020-2023  润新知