Home  >  Article  >  Backend Development  >  Detailed explanation of sending emails in golang (qq mailbox)

Detailed explanation of sending emails in golang (qq mailbox)

藏色散人
藏色散人forward
2021-03-23 17:50:213242browse

The following tutorial column of golang will introduce to you the method of sending emails (qq mailbox) in golang. I hope it will be helpful to friends in need!

Detailed explanation of sending emails in golang (qq mailbox)

golang sends email (qq mailbox)

Preliminary preparation

Golang sends email function, today we introduce golang to send qq email, I believe everyone has a qq mailbox, which is relatively convenient to operate. It is even simpler to use golang to send emails. I believe everyone can get started and learn it after reading this.

Sending emails through encoding means actually calling the interface provided by qq mailbox to send emails. Before writing this function, we need to get the authorization code of the POP3/SMTP service of qq mailbox

    ##Enter your qq mailbox->Settings->Account->Scroll down

    Enable the POP3/SMTP service of your qq mailbox, and get the authorization code of your qq mailbox (supports temporary generation). The authorization code is valid for a long time and it is recommended to save it

Code writing
package mainimport (
   "fmt"
   "gopkg.in/gomail.v2")func main() {
   m := gomail.NewMessage()

   //发送人
   m.SetHeader("From", "xxx@qq.com")
   //接收人
   m.SetHeader("To", "xxx@qq.com")
   //抄送人
   //m.SetAddressHeader("Cc", "xxx@qq.com", "xiaozhujiao")
   //主题
   m.SetHeader("Subject", "小佩奇")
   //内容
   m.SetBody("text/html", "<h1>新年快乐</h1>")
   //附件
   //m.Attach("./myIpPic.png")

   //拿到token,并进行连接,第4个参数是填授权码
   d := gomail.NewDialer("smtp.qq.com", 587, "xxx@qq.com", "xxxxxx")

   // 发送邮件
   if err := d.DialAndSend(m); err != nil {
      fmt.Printf("DialAndSend err %v:", err)
      panic(err)
   }
   fmt.Printf("send mail success\n")}

Effect display

If you have interesting ideas to implement using golang, you can give us feedback and we can try it together and learning.

The above is the detailed content of Detailed explanation of sending emails in golang (qq mailbox). For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete