dtv11049 2017-03-24 19:38
浏览 61
已采纳

GoLang:发送不带Mailjet库的Mailjet电子邮件

I am trying to send emails from my golang application using my Mailjet credentials, but I am trying to do it the normal golang way (yes, I know that their library is highly encouraged).

I have the emails working fine using the Mailjet library, but my boss made a really good point that we might not stay with Mailjet forever. If we switch to a different email solution, we don't want to have to rewrite all of our email code, we just want to change our hostname and credentials.

My printer sends emails just find through Mailjet using the same hostname and credentials, but for some reason my golang app won't!

My code was adopted from the golang smtp library SendEmail example.

Here it is (without my credentials, of course):

import (
    "bytes"
    "fmt"
    "net/smtp"
)
func SendTestEmail() (bool, error) {
    fmt.Println("Send Test Email: Enter")
    success := false
    hostname := "in-v3.mailjet.com"
    auth := smtp.PlainAuth("", username, password, hostname)
    to := []string{"me@example.com"}
    msg := []byte("To: me@example.com
" +
        "Subject: discount Gophers!
" +
        "
" +
        "This is the email body.
")
    fmt.Println("Send Test Email: Sending Email")
    err := smtp.SendMail(hostname+":587", auth, "sender@example.com", to, msg)
    if err == nil {
        fmt.Println("Send Test Email: Email successfully sent!")
        success = true
    } else {
        fmt.Println("Send Test Email: Email failed to send", err)
    }
    fmt.Println("Send Test Email: Exit")
    return success, err
}

Note that I am using port 587. I do not know if my printer is using 587 or 25, but it's working. I don't work when using port 25 either.

What is really weird is that smtp.SendEmail isn't returning any errors, but I still do not get any emails (yes, I am checking my junk folder)!

Also, when I log into Mailjet, I don't see that any emails were sent. I do see that an email was sent when I send something from the printer.

So, where is my email?!

Any help is greatly appreciated. Thanks!

  • 写回答

1条回答 默认 最新

  • 普通网友 2017-03-24 20:29
    关注

    First of all, thanks for choosing Mailjet as your email service provider! I'm leading the API Product and Developers Relations at Mailjet.

    When it comes to send, you're right with SMTP. It's standard, widely supported and easy to switch (even if I don't hope we'll get there!). Our Go library will become handy when it comes to deal with our API to manage business processes.

    I have several questions / feedback looking at your code:

    • I guess the "sender@example.com" from address used is not the one you use in your real code? Anyway, this email must have been validated on Mailjet side beforehands. See our dedicated guide
    • Seems you try to set some SMTP headers like Subject within the message, when it should be handled separately

    Here's a working code I'm using to work with SMTP:

    package main
    
    import (
      "log"
      "net/smtp"
      "fmt"
    )
    
    func main() {
        auth := smtp.PlainAuth(
            "",
            "MAILJET_API_KEY",
            "MAILJET_API_SECRET",
            "in-v3.mailjet.com",
        )
    
        email := "foobar@test.com"
    
        header := make(map[string]string)
        header["From"] = email
        header["To"] = email
        header["Subject"] = "Hello Mailjet World!"
        header["X-Mailjet-Campaign"] = "test"
    
        message := ""
        for k, v := range header {
            message += fmt.Sprintf("%s: %s
    ", k, v)
        }
        message += "
    Hi! Thanks for using Mailjet."
    
        err := smtp.SendMail(
            "in-v3.mailjet.com:587",
            auth,
            email,
            []string{email},
            []byte(message),
        )
        if err != nil {
          log.Printf("Error: %s", err)
        } else {
          log.Printf("Mail sent!")
        }
    }
    

    Hope it helps! hAPI sending with Mailjet

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

报告相同问题?

悬赏问题

  • ¥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 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?