Sending messages

To send a message, we have to package up the data in a raw format to send through the API. We'll re-use the ToGMailEncoding() function from the Post example in Chapter 12, Concurrency, Networking, and Cloud Services. Before encoding the email, we set an appropriate "From" email address (be sure to use the email address of the account you are signed in with or a registered alias) and the current date for the time of sending. After encoding, we set the data to the Raw field of a gmail.Message type and pass it to the Gmail Send() function:

func (g *gMailServer) Send(email *EmailMessage) {
email.From = "YOUR EMAIL ADDRESS"
email.Date = time.Now()

data := email.ToGMailEncoding()
msg := &gmail.Message{Raw:data}

srv.Users.Messages.Send(user, msg).Do()
}

This minimal code will be enough to implement sending a message. All of the hard work has been done by the earlier setup code—which provided the srv object.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.12.123.189