SendingMail
Alexandre Cesaro edited this page Sep 8, 2015
·
5 revisions
Pages 128
- Home
- Articles
- Benchmarks
- Blogs
- Books
- BoundingResourceUse
- cgo
- ChromeOS
- CodeReview
- CodeReviewComments
- CodeTools
- Comments
- CommonMistakes
- CompilerOptimizations
- Conferences
- CoreDumpDebugging
- Courses
- CustomPprofProfiles
- Darwin
- DashboardBuilders
- DesignDocuments
- DevExp
- Diagnostics
- DragonFly BSD
- Errors
- ExperienceReports
- FileTreeDocumentation
- FreeBSD
- FromXToGo
- Gardening
- GccgoCrossCompilation
- GcToolchainTricks
- GerritAccess
- GithubAccess
- GitHubCodeLayout
- Go 1.6 release party
- Go 1.8 Release Party
- Go Release Cycle
- Go1point1Gotchas
- GoArm
- GoForCPPProgrammers
- GoGenerateTools
- GoGetProxyConfig
- GoGetTools
- Gomote
- GOPATH
- Gopher
- GoStrings
- GoTalks
- GoUserGroups
- GoUsers
- GoVsGenerics
- HandlingIssues
- Hashing
- heapdump13
- heapdump14
- heapdump15
- HostedContinuousIntegration
- How to ask for help
- HowToAsk
- HttpFetch
- HttpStaticFiles
- IDEsAndTextEditorPlugins
- InstallFromSource
- InstallTroubleshooting
- InterfaceSlice
- Iota
- IssueLabels
- Learn
- LearnConcurrency
- LearnErrorHandling
- LearnServerProgramming
- LearnTesting
- Linux
- LockOSThread
- MethodSets
- MinimumRequirements
- Mobile
- MultipleGoRoots
- MutexOrChannel
- NativeClient
- NetBSD
- NoMeToo
- NonEnglish
- OlderVersions
- OpenBSD
- PackageManagementTools
- PackagePublishing
- PanicAndRecover
- PerfDashboard
- Performance
- Plan9
- Podcasts
- PortingPolicy
- PriorDiscussion
- Projects
- ProviderIntegration
- Questions
- RaceDetector
- Range
- RateLimiting
- Rationales
- ResearchPapers
- Screencasts
- SendingMail
- Setting GOPATH
- SettingGOPATH
- SignalHandling
- SimultaneousAssignment
- SliceTricks
- Solaris
- SQLDrivers
- SQLInterface
- Style
- SubRepositories
- SuccessStories
- Switch
- TableDrivenTests
- Timeouts
- Training
- Ubuntu
- WebAccessibilityResourcesAndTips
- Well known struct tags
- WhyGo
- WindowsBuild
- WindowsCrossCompiling
- WindowsDLLs
- WindowsSupport
- Show 113 more pages…
Clone this wiki locally
Sending Mail
See also:
Streaming the body:
package main
import (
"bytes"
"log"
"net/smtp"
)
func main() {
// Connect to the remote SMTP server.
c, err := smtp.Dial("mail.example.com:25")
if err != nil {
log.Fatal(err)
}
defer c.Close()
// Set the sender and recipient.
c.Mail("sender@example.org")
c.Rcpt("recipient@example.net")
// Send the email body.
wc, err := c.Data()
if err != nil {
log.Fatal(err)
}
defer wc.Close()
buf := bytes.NewBufferString("This is the email body.")
if _, err = buf.WriteTo(wc); err != nil {
log.Fatal(err)
}
}Authenticated SMTP:
package main
import (
"log"
"net/smtp"
)
func main() {
// 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.
err := smtp.SendMail(
"mail.example.com:25",
auth,
"sender@example.org",
[]string{"recipient@example.net"},
[]byte("This is the email body."),
)
if err != nil {
log.Fatal(err)
}
}