Go发送Email
使用Go发送邮件,目前官网GO 1.12的版本的文档中,包 "net/smtp" 仅支持支持两种SMTP的认证方式。CRAM-MD5和PLAIN 认证方式。
CRAM-MD5 是基于Keyed-MD5的认证方式
PLAIN 是一种明文的验证方式
我们公司使用的邮件服务器SMTP支持两种认证方式:NTLM 和 LOGIN
查看邮件服务器支持SMTP的认证方式的方法:

在这种情况下:
1、直接使用Go官网"net/smtp"的认证方式发送邮件回报错
报错:x509: certificate signed by unknown authority
理解: 这个是GO的客户端对服务器传过来数字证书进行校验,可以进行关闭,不进行校验。
代码:
if ok, _ := c.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: c.serverName, InsecureSkipVerify: true}
if testHookStartTLS != nil {
testHookStartTLS(config)
}
if err = c.StartTLS(config); err != nil {
return err
}
}
报错:504 5.7.4 Unrecognized authentication type
理解:GO官网包 "net/smtp" 仅支持 CRAM-MD5和PLAIN 而我们公司邮件服务器支持认证方式 NTLM 和 LOGIN
2、支持SMTP的LOGIN认证方式的代码
type LoginAuth struct {
username string
password string
}
func NewLoginAuth(username, password string) smtp.Auth {
return &LoginAuth{username, password}
}
func (a *LoginAuth) Start(server *smtp.ServerInfo) (string, []byte, error) {
return "LOGIN", []byte{}, nil
}
func (a *LoginAuth) Next(fromServer []byte, more bool) ([]byte, error) {
if more {
switch string(fromServer) {
case "Username:":
return []byte(a.username), nil
case "Password:":
return []byte(a.password), nil
default:
return nil, errors.New("Unknown fromServer")
}
}
return nil, nil
}
func SendMail(addr string, a smtp.Auth, subject string, from string, to []string, msg []byte) error {
c, err := smtp.Dial(addr)
host, _, _ := net.SplitHostPort(addr)
if err != nil {
fmt.Println("call dial")
return err
}
defer c.Close()
if ok, _ := c.Extension("STARTTLS"); ok {
config := &tls.Config{ServerName: host, InsecureSkipVerify: true}
if err = c.StartTLS(config); err != nil {
fmt.Println("call start tls")
return err
}
}
if a != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
fmt.Println("check auth with err:", err)
return err
}
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
header := make(map[string]string)
header["Subject"] = subject
header["MIME-Version"] = "1.0"
header["Content-Type"] = "text/plain; charset=\"utf-8\""
header["Content-Transfer-Encoding"] = "base64"
message := ""
for k, v := range header {
message += fmt.Sprintf("%s: %s\r\n", k, v)
}
message += "\r\n" + base64.StdEncoding.EncodeToString(msg)
_, err = w.Write([]byte(message))
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
Go发送Email的更多相关文章
- java发送email
package com.assess.util; import java.io.File; import java.util.ArrayList; import java.util.List; imp ...
- Spring 发送 Email
本文转自:http://zl198751.iteye.com/blog/757617 看到了本文,收获颇丰,感谢之至! 首先介绍下Email的发送流程: 需要选中smtp邮件服务器,Yahoo不提供免 ...
- 使用PHP发送email进行账号激活或者密码修改操作
使用PHPMailer编写发送邮件 PHPMailer需PHP的socket扩展支持,而PHPMailer链接qq域名邮箱时需要ssl加密方式(qq邮箱最近做了限制,新开域名邮箱不再允许通过smtp协 ...
- 使用python原生的方法实现发送email
使用python原生的方法实现发送email import smtplib from email.mime.text import MIMEText from email.mime.multipart ...
- C#发送Email邮件(实例:QQ邮箱和Gmail邮箱)
下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用: using System.Net.Mail; using System.Text; using System.Net; ...
- 【WinForm】C# 发送Email
发送Email 的条件 1.SmtpClient SMTP 协议 即 Host 处理事务的主机或IP地址 //smtp.163.com UseDefaultCredentia ...
- [转]C#发送Email邮件 (实例:QQ邮箱和Gmail邮箱)
下面用到的邮件账号和密码都不是真实的,需要测试就换成自己的邮件账号. 需要引用:using System.Net.Mail;using System.Text;using System.Net; 程序 ...
- asp.net发送E-mail
发送电子邮件也是项目开发当中经常用到的功能,这里我整理了一个发送电子邮件(带附件,支持多用户发送,主送.抄送)的类库,供大家参考. 先上两个实体类,用于封装成Mail对象. /// <summa ...
- 使用spring 并加载模板发送Email 发邮件 java 模板
以下例子是使用spring发送email,然后加载到固定的模板,挺好的,大家可以试试 需要使用到spring-context 包 和 com.springsource.org.apache.veloc ...
- [Python] 发送email的几种方式
python发送email还是比較简单的,能够通过登录邮件服务来发送,linux下也能够使用调用sendmail命令来发送,还能够使用本地或者是远程的smtp服务来发送邮件,无论是单个,群发,还是抄送 ...
随机推荐
- [LeetCode] 685. Redundant Connection II 冗余的连接之二
In this problem, a rooted tree is a directed graph such that, there is exactly one node (the root) f ...
- [LeetCode] 633. Sum of Square Numbers 平方数之和
Given a non-negative integer c, your task is to decide whether there're two integers a and b such th ...
- [LeetCode] 253. Meeting Rooms II 会议室之二
Given an array of meeting time intervals consisting of start and end times [[s1,e1],[s2,e2],...] (si ...
- [LeetCode] 137. Single Number II 单独的数字之二
Given a non-empty array of integers, every element appears three times except for one, which appears ...
- [LeetCode] 81. Search in Rotated Sorted Array II 在旋转有序数组中搜索之二
Suppose an array sorted in ascending order is rotated at some pivot unknown to you beforehand. (i.e. ...
- 微信小程序登录那些事
最近团队在开发一款小程序,都是新手,一边看文档,一边开发.在开发中会遇到各种问题,今天把小程序登录这块的流程整理下,做个记录. 小程序的登录跟平时自己APP这种登录验证还不太一样,多了一个角色,那就是 ...
- java --后缀符号
public class Sample { public static void main(String[] args) { , num2 = ; num1--; System.out.println ...
- swagger案例Swagger案例
pom <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework ...
- nginx服务器图片防盗链的方法
nginx服务器图片防盗链的方法<pre> location ~* \.(gif|jpg|png|jpeg)$ { expires 30d; valid_referers *.shuche ...
- 学习《Linux网络安全技术与实现》,正文为1、2章的笔记,后面的等待更新
博客地址:http://www.cnblogs.com/zengjianrong/p/3280276.html