Source file
src/net/smtp/example_test.go
1
2
3
4
5 package smtp_test
6
7 import (
8 "fmt"
9 "log"
10 "net/smtp"
11 )
12
13 func Example() {
14
15 c, err := smtp.Dial("mail.example.com:25")
16 if err != nil {
17 log.Fatal(err)
18 }
19
20
21 if err := c.Mail("sender@example.org"); err != nil {
22 log.Fatal(err)
23 }
24 if err := c.Rcpt("recipient@example.net"); err != nil {
25 log.Fatal(err)
26 }
27
28
29 wc, err := c.Data()
30 if err != nil {
31 log.Fatal(err)
32 }
33 _, err = fmt.Fprintf(wc, "This is the email body")
34 if err != nil {
35 log.Fatal(err)
36 }
37 err = wc.Close()
38 if err != nil {
39 log.Fatal(err)
40 }
41
42
43 err = c.Quit()
44 if err != nil {
45 log.Fatal(err)
46 }
47 }
48
49
50
51 var (
52 from = "gopher@example.net"
53 msg = []byte("dummy message")
54 recipients = []string{"foo@example.com"}
55 )
56
57 func ExamplePlainAuth() {
58
59 hostname := "mail.example.com"
60 auth := smtp.PlainAuth("", "user@example.com", "password", hostname)
61
62 err := smtp.SendMail(hostname+":25", auth, from, recipients, msg)
63 if err != nil {
64 log.Fatal(err)
65 }
66 }
67
68 func ExampleSendMail() {
69
70 auth := smtp.PlainAuth("", "user@example.com", "password", "mail.example.com")
71
72
73
74 to := []string{"recipient@example.net"}
75 msg := []byte("To: recipient@example.net\r\n" +
76 "Subject: discount Gophers!\r\n" +
77 "\r\n" +
78 "This is the email body.\r\n")
79 err := smtp.SendMail("mail.example.com:25", auth, "sender@example.org", to, msg)
80 if err != nil {
81 log.Fatal(err)
82 }
83 }
84
View as plain text