Golang net/mail.Message type and ReadMessage() function example

package net/mail

Golang net/mail.Message type and ReadMessage() function usage example

 package main

 import (
  "bytes"
  "fmt"
  "net/mail"
 )

 var emailData = `From: Adam <adam@earth.com>
 To: Eve <eve@earth.com>
 Subject: Do not eat the apple from that tree!
 Date: Fri, 21 Nov 0000 09:55:06 -0600
 Message-ID: <1234@garden.earth>

 Don't listen to that snake. Don't eat the apple!
 `

 func main() {

  msg, err := mail.ReadMessage(bytes.NewBuffer([]byte(emailData)))

  if err != nil {
 panic(err)
  }

  fmt.Println(msg.Header)

  // convert io.Reader type to string

  buf := new(bytes.Buffer)
  buf.ReadFrom(msg.Body)
  s := buf.String()

  fmt.Println(s)

 }

Output :

map[Date:[Fri, 21 Nov 0000 09:55:06 -0600] Message-Id:[1234@garden.earth] From:[Adam adam@earth.com] To:[Eve eve@earth.com] Subject:[Do not

eat the apple from that tree!]]

Don't listen to that snake. Don't eat the apple!

References :

http://golang.org/pkg/net/mail/#Message

http://golang.org/pkg/net/mail/#ReadMessage

Advertisement