From 43a123c1ae6613b3efeed291fa552ecd909d3acf Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Tue, 16 Apr 2024 21:23:18 +0200 Subject: Adding upstream version 1.20.14. Signed-off-by: Daniel Baumann --- src/net/mail/example_test.go | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100644 src/net/mail/example_test.go (limited to 'src/net/mail/example_test.go') diff --git a/src/net/mail/example_test.go b/src/net/mail/example_test.go new file mode 100644 index 0000000..d325dc7 --- /dev/null +++ b/src/net/mail/example_test.go @@ -0,0 +1,77 @@ +// Copyright 2015 The Go Authors. All rights reserved. +// Use of this source code is governed by a BSD-style +// license that can be found in the LICENSE file. + +package mail_test + +import ( + "fmt" + "io" + "log" + "net/mail" + "strings" +) + +func ExampleParseAddressList() { + const list = "Alice , Bob , Eve " + emails, err := mail.ParseAddressList(list) + if err != nil { + log.Fatal(err) + } + + for _, v := range emails { + fmt.Println(v.Name, v.Address) + } + + // Output: + // Alice alice@example.com + // Bob bob@example.com + // Eve eve@example.com +} + +func ExampleParseAddress() { + e, err := mail.ParseAddress("Alice ") + if err != nil { + log.Fatal(err) + } + + fmt.Println(e.Name, e.Address) + + // Output: + // Alice alice@example.com +} + +func ExampleReadMessage() { + msg := `Date: Mon, 23 Jun 2015 11:40:36 -0400 +From: Gopher +To: Another Gopher +Subject: Gophers at Gophercon + +Message body +` + + r := strings.NewReader(msg) + m, err := mail.ReadMessage(r) + if err != nil { + log.Fatal(err) + } + + header := m.Header + fmt.Println("Date:", header.Get("Date")) + fmt.Println("From:", header.Get("From")) + fmt.Println("To:", header.Get("To")) + fmt.Println("Subject:", header.Get("Subject")) + + body, err := io.ReadAll(m.Body) + if err != nil { + log.Fatal(err) + } + fmt.Printf("%s", body) + + // Output: + // Date: Mon, 23 Jun 2015 11:40:36 -0400 + // From: Gopher + // To: Another Gopher + // Subject: Gophers at Gophercon + // Message body +} -- cgit v1.2.3