Golang send mail (QQ mailbox)

Time:2021-5-8
[TOC]

Golang send mail (QQ mailbox)

preparation in advance

Today, we will introduce the function of golang sending QQ mail. I believe everyone has a QQ mailbox. It’s easy to operate. It’s even easier to use golang to send mail. I believe you can learn how to operate it after reading it.

Send mail by encoding, that is, actually call the interface provided by QQ mailbox to send mail. Before writing this function, we need to get the authorization code of POP3 / SMTP service of QQ mailbox

Golang send mail (QQ mailbox)

  • Go to your QQ mailbox, settings, account, and slide down

Golang send mail (QQ mailbox)

  • Open the POP3 / SMTP service of your QQ mailbox and get the authorization code of your QQ mailbox (temporary generation is supported). The authorization code is valid for a long time. It is recommended to save it

Code writing

package main

import (
   "fmt"
   "gopkg.in/gomail.v2"
)

func main() {
   m := gomail.NewMessage()

   //Sender
   m.SetHeader("From", "[email protected]")
   //Receiver
   m.SetHeader("To", "[email protected]")
   //CC to
   //m.SetAddressHeader("Cc", "[email protected]", "xiaozhujiao")
   //Themes
   m. Setheader ("subject", "little page")
   //Content
   m. Setbody ("text / HTML", "< H1 > happy New Year < / H1 >")
   //Annex
   //m.Attach("./myIpPic.png")

   //Get the token and connect. The fourth parameter is to fill in the authorization code
   d := gomail.NewDialer("smtp.qq.com", 587, "[email protected]", "xxxxxx")

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

Effect display

Golang send mail (QQ mailbox)

If you have any interesting ideas, you can use golang to implement them. You can give feedback. We can try and learn together.

Author: Little Devil boy Nezha

This work adoptsCC agreementReprint must indicate the author and the link of this article