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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵