golang發郵件

來源:互聯網
上載者:User
這是一個建立於 的文章,其中的資訊可能已經有所發展或是發生改變。
01 package main
02 import (
03     "net/smtp"
04     "fmt"
05     "strings"
06 )
07  
08 /*
09  *  user : example@example.com login smtp server user
10  *  password: xxxxx login smtp server password
11  *  host: smtp.example.com:port   smtp.163.com:25
12  *  to: example@example.com;example1@163.com;example2@sina.com.cn;...
13  *  subject:The subject of mail
14  *  body: The content of mail
15  *  mailtyoe: mail type html or text
16  */
17  
18  
19 func SendMail(user, password, host, to, subject, body, mailtype string) error{
20     hp := strings.Split(host,":")
21     auth := smtp.PlainAuth("", user, password, hp[0])
22     var content_type string
23     ifmailtype == "html" {
24         content_type ="Content-Type: text/"+ mailtype +"; charset=UTF-8"
25     }else{
26         content_type ="Content-Type: text/plain" + "; charset=UTF-8"
27     }
28  
29     msg := []byte("To: "+ to + "\r\nFrom: " + user + "<"+ user +">\r\nSubject: "+ subject + "\r\n" + content_type + "\r\n\r\n" + body)
30     send_to := strings.Split(to,";")
31     err := smtp.SendMail(host, auth, user, send_to, msg)
32     returnerr
33 }
34  
35 func main() {
36     user := "xxxx@163.com"
37     password :="xxxx"
38     host := "smtp.163.com:25"
39     to := "xxxx@gmail.com;ssssss@gmail.com"
40  
41     subject :="Test send email by golang"
42  
43     body := `
44     <html>
45     <body>
46     <h3>
47     "Test send email by golang"
48     </h3>
49     </body>
50     </html>
51     `
52     fmt.Println("send email")
53     err := SendMail(user, password, host, to, subject, body,"html")
54     iferr != nil {
55         fmt.Println("send mail error!")
56         fmt.Println(err)
57     }else{
58         fmt.Println("send mail success!")
59     }
60  
61 }
相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.