]> Cypherpunks repositories - gostls13.git/commitdiff
net/smtp: add examples
authorKamil Kisiel <kamil@kamilkisiel.net>
Thu, 16 Jan 2014 18:49:58 +0000 (10:49 -0800)
committerBrad Fitzpatrick <bradfitz@golang.org>
Thu, 16 Jan 2014 18:49:58 +0000 (10:49 -0800)
R=golang-codereviews, bradfitz
CC=golang-codereviews
https://golang.org/cl/8274046

src/pkg/net/smtp/example_test.go [new file with mode: 0644]

diff --git a/src/pkg/net/smtp/example_test.go b/src/pkg/net/smtp/example_test.go
new file mode 100644 (file)
index 0000000..d551e36
--- /dev/null
@@ -0,0 +1,61 @@
+// Copyright 2013 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 smtp_test
+
+import (
+       "fmt"
+       "log"
+       "net/smtp"
+)
+
+func Example() {
+       // Connect to the remote SMTP server.
+       c, err := smtp.Dial("mail.example.com:25")
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       // Set the sender and recipient first
+       if err := c.Mail("sender@example.org"); err != nil {
+               log.Fatal(err)
+       }
+       if err := c.Rcpt("recipient@example.net"); err != nil {
+               log.Fatal(err)
+       }
+
+       // Send the email body.
+       wc, err := c.Data()
+       if err != nil {
+               log.Fatal(err)
+       }
+       _, err = fmt.Fprintf(wc, "This is the email body")
+       if err != nil {
+               log.Fatal(err)
+       }
+       err = wc.Close()
+       if err != nil {
+               log.Fatal(err)
+       }
+
+       // Send the QUIT command and close the connection.
+       err = c.Quit()
+       if err != nil {
+               log.Fatal(err)
+       }
+}
+
+func ExamplePlainAuth() {
+       // Set up authentication information.
+       auth := smtp.PlainAuth("", "user@example.com", "password", "mail.example.com")
+
+       // Connect to the server, authenticate, set the sender and recipient,
+       // and send the email all in one step.
+       to := []string{"recipient@example.net"}
+       msg := []byte("This is the email body.")
+       err := smtp.SendMail("mail.example.com:25", auth, "sender@example.org", to, msg)
+       if err != nil {
+               log.Fatal(err)
+       }
+}