dsf4s5787 2018-11-29 20:29
浏览 298
已采纳

使用Golang通过G Suite发送电子邮件

I've got a G Suite account set up and I enabled the SMTP relay service. The relay service is currently configured to accept mail from my webservers IP address only, not requiring SMTP authentication or TLS encryption.
This link tells me that I should be able to send mail to smtp-relay.gmail.com on port 25 (or 465/587).

That's all well and good. I've got a very simple go script to send email here.

package main

import (
        "log"
        "net/smtp"
        "os"
)

func main() {
        args := os.Args[1:]
        to := args[0]
        body := args[1]
        from := "[my g-suite user account name]"
        msg := "From: " + from + "
" + "To: " + to + "
" + 
                "Subject: Hello Friend

" + body
        err := smtp.SendMail("smtp-relay.gmail.com:25",nil,
                from, []string{to}, []byte(msg))
        if err != nil {
                log.Printf("smtp err :%s
",err)
        } else {
                log.Printf("Email sent
")
        }

        log.Printf("email sent
")
}

When I run this, I get the following panic

2018/11/29 12:24:22 smtp err :dial tcp 74.125.195.28:25: connect: no route to host

I understand what this error is telling me but I cant for the life of me resolve it. I thought it was a problem with my firewall settings. I've tried flushing my iptables and disabling ufw but I get the same result every time.

I've also tried sending mail from this webserver using mutt and sendmail (with similar settings).

Any suggestions on what I should be looking for? I have done a bunch of searching and tried several other scripts but always have the same result.

EDIT
After trying a few different options like regular gmail with an existing account, using sendmail or ssmtp I looked at tcpdump while trying to send mail. Wireshark shows that the communication with the mail server as "Communication administratively filtered". I'm taking that to mean the hosting provider is filtering ports 25,465 and 587 since I've tried them all and I've disabled my rules.

  • 写回答

1条回答 默认 最新

  • dougui4325 2018-12-04 05:11
    关注

    Solved.
    My hosting provider was filtering SMTP ports. I opened a service ticket and had these filters adjusted. Mail works fine now

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看