golang发送阿里云邮件

package main

import (
	"crypto/hmac"
	"crypto/sha1"
	"encoding/base64"
	"fmt"
	"io/ioutil"
	"math/rand"
	"net/http"
	"net/url"
	"sort"
	"strings"
	"time"
)

const AccessKeyID = "0000"
const AccessKeySecret = "1111"
const AccountName = "admin@sendmail.aliyun.com"

func UrlEncode(in string) string {
	r := strings.NewReplacer("+", "%20", "*", "%2A", "%7E", "~")
	return r.Replace(url.QueryEscape(in))
}

func Sign(stringToSign string) string {
	h := hmac.New(sha1.New, []byte(fmt.Sprintf("%s&", AccessKeySecret)))
	h.Write([]byte(stringToSign))
	return UrlEncode(base64.StdEncoding.EncodeToString(h.Sum(nil)))
}

func Keys(data map[string]string) []string {
	var keys []string
	for k := range data {
		keys = append(keys, k)
	}
	sort.Strings(keys)
	return keys
}

func SortedString(data map[string]string) string {
	var sortQueryString string
	for _, v := range Keys(data) {
		sortQueryString = fmt.Sprintf("%s&%s=%s", sortQueryString, v, UrlEncode(data[v]))
	}
	return sortQueryString
}

func AliyunSendMail(email string, subject string, body string) string {

	rand.Seed(time.Now().UnixNano())

	data := map[string]string{
		"SignatureMethod":  "HMAC-SHA1",
		"SignatureNonce":   fmt.Sprintf("%s", rand.Int63()),
		"AccessKeyId":      AccessKeyID,
		"SignatureVersion": "1.0",
		"Timestamp":        time.Now().UTC().Format("2006-01-02T15:04:05Z"),
		"Format":           "JSON",

		"Action":         "SingleSendMail",
		"Version":        "2015-11-23",
		"AccountName":    AccountName,
		"ReplyToAddress": "true",
		"AddressType":    "1",
		"ToAddress":      email,
		"Subject":        subject,
		"TextBody":       body,
	}

	sortQueryString := SortedString(data)
	stringToSign := fmt.Sprintf("GET&%s&%s", UrlEncode("/"), UrlEncode(sortQueryString[1:]))
	url := fmt.Sprintf("http://dm.aliyuncs.com/?Signature=%s%s", Sign(stringToSign), sortQueryString)

	r, err := http.Get(url)
	if err != nil {
		return ""
	}
	defer r.Body.Close()

	b, err := ioutil.ReadAll(r.Body)
	if err != nil {
		return ""
	}

	return string(b)
}

func main() {
	html := AliyunSendMail("123456@qq.com", "Database Backup Email", "Database Backup Completed")
	fmt.Println(html)
}