Original post

I’m having trouble using smtp. I’m a novice user and tried modifying the code in the documentation:

package main

import (
“log”
“net/smtp”
)

// variables to make ExamplePlainAuth compile, without adding
// unnecessary noise there.
var (
from = “MYEMAIL_at_charter.net”
msg = []byte(“dummy message”)
recipients = []string{“MY_EMAIL_at_gmail.com”}
)

func main() {
// hostname is used by PlainAuth to validate the TLS certificate.
hostname := “mobile.charter.net
auth := smtp.PlainAuth(“”, “MYEMAIL_at_charter.net”, “MY_CHARTER_PASSWD”, hostname)

    err := smtp.SendMail(hostname+":587", auth, from, recipients, msg)
if err != nil {
        log.Fatal(err)
}

}

This program never terminates or gives error messages.

I’m using the parameters that were successful in sending email sent via cURL:

curl –url smtps://mobile.charter.net:587 –ssl-reqd –mail-from MYEMAIL_at_charter.net –mail-rcpt MY_EMAIL_at_gmail.com –upload-file mail.txt –user MYEMAIL_at_charter.net:MY_CHARTER_PASSWD–insecure

Any help is appreciated.

can you perhaps use fmt.Println() calls to identify the place where it hangs?

Go newbie here, I’m not sure, but I believe adding this code

import (
        "os"
        "os/signal"
        "runtime/pprof"
        "syscall"
)

func main() {

c := make(chan os.Signal)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
        <-c
        pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
        os.Exit(0) // Is just there so that CTRL+C still quits the program.
}()

would allow you to hit CTRL+C and see where it blocks.

It fails here:

 err := smtp.SendMail(hostname+":587", auth, from, recipients, msg)

The code for processing CTRL+C produced lengthy output:
goroutine profile: total 3
1 @ 0x40b38f 0x449ff3 0x5c5a19 0x463651
# 0x449ff2 os/signal.signal_recv+0xa2 c:/go/src/runtime/sigqueue.go:147
# 0x5c5a18 os/signal.loop+0x28 c:/go/src/os/signal/signal_unix.go:23

1 @ 0x439245 0x430321 0x42f81c 0x4b585c 0x4b61f1 0x4b767c 0x53d736 0x5471f5 0x5bf5ca 0x5bfcb4 0x5bfefb 0x5c0fe3 0x5c1196 0x5c1189 0x5c1634 0x5c2e62 0x5c2da8 0x5c48bc 0x5dc7bf 0x438e8a 0x463651
#       0x42f81b        internal/poll.runtime_pollWait+0x5b             c:/go/src/runtime/netpoll.go:203
#       0x4b585b        internal/poll.(*pollDesc).wait+0x4b             c:/go/src/internal/poll/fd_poll_runtime.go:87
#       0x4b61f0        internal/poll.(*ioSrv).ExecIO+0x120             c:/go/src/internal/poll/fd_windows.go:228
#       0x4b767b        internal/poll.(*FD).Read+0x2fb                  c:/go/src/internal/poll/fd_windows.go:527
#       0x53d735        net.(*netFD).Read+0x55                          c:/go/src/net/fd_windows.go:152
#       0x5471f4        net.(*conn).Read+0x94                           c:/go/src/net/net.go:184
#       0x5bf5c9        bufio.(*Reader).fill+0x109                      c:/go/src/bufio/bufio.go:100
#       0x5bfcb3        bufio.(*Reader).ReadSlice+0x43                  c:/go/src/bufio/bufio.go:359
#       0x5bfefa        bufio.(*Reader).ReadLine+0x3a                   c:/go/src/bufio/bufio.go:388
#       0x5c0fe2        net/textproto.(*Reader).readLineSlice+0x72      c:/go/src/net/textproto/reader.go:58
#       0x5c1195        net/textproto.(*Reader).ReadLine+0x35           c:/go/src/net/textproto/reader.go:39
#       0x5c1188        net/textproto.(*Reader).readCodeLine+0x28       c:/go/src/net/textproto/reader.go:193
#       0x5c1633        net/textproto.(*Reader).ReadResponse+0x53       c:/go/src/net/textproto/reader.go:271
#       0x5c2e61        net/smtp.NewClient+0x81                         c:/go/src/net/smtp/smtp.go:64
#       0x5c2da7        net/smtp.Dial+0xc7                              c:/go/src/net/smtp/smtp.go:57
#       0x5c48bb        net/smtp.SendMail+0x10b                         c:/go/src/net/smtp/smtp.go:328
#       0x5dc7be        main.main+0x2de                                 c:/Windows/System32/elson/software/go/mailtest_charter_mod.go:36
#       0x438e89        runtime.main+0x1f9                              c:/go/src/runtime/proc.go:203

1 @ 0x5d4f1c 0x5d4d37 0x5d1a51 0x5dc937 0x463651
#       0x5d4f1b        runtime/pprof.writeRuntimeProfile+0x9b  c:/go/src/runtime/pprof/pprof.go:694
#       0x5d4d36        runtime/pprof.writeGoroutine+0xa6       c:/go/src/runtime/pprof/pprof.go:656
#       0x5d1a50        runtime/pprof.(*Profile).WriteTo+0x3e0  c:/go/src/runtime/pprof/pprof.go:329
#       0x5dc936        main.main.func1+0x86                    c:/Windows/System32/elson/software/go/mailtest_charter_mod.go:27

Not sure what to make of this but I suppose I could look at the smtp.go file. I was hoping someone had used this example to send an email and could offer advice on input parameters that work.

I have no network experience, but I guess it should be possible to printf debug or use wireshark to see why the smtp.SendMail wants to read more from the server than it gets. Or use (via exec.Command) a smtp client that supports a timeout, or change the mail service, or – if your mail actually gets sent – put the blocking thing into a goroutine and ignore that it’s not working perfectly.

With the Charter server, mail doesn’t go through. Changed server to gmail:

hostname := "smtp.gmail.com"
.
.
.
err := smtp.SendMail(hostname+":465", auth, from, recipients, msg)

Also changed the message to include To: and From:

Now, the program doesn’t hang, but no mail goes through. Here is the output:
2020/06/25 10:01:07 EOF
exit status 1

Tried it myself, for me Gmail worked with port 25. But only after I had set IMAP to on in Gmail account settings in the POP/IMAP tab and after using Thunderbird with the server settings.

was this fixed? in troubleshooting any mail sending app, one needs to isolate where the problem come from. chief among them is the OS… try sending mail first from the OS and see what happens. don’t forget to include the error message/s if any.

Made the change from port 465 to port 25 and it worked using gmail! Thanks for the tip. I also have the secure app only turned off. Works with Charter too

Oh, true, I had that off too.

Could have also just been a really long timeout setting. If it exists, I would check the docs as this may come up again.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.