golang:从smtp.SendMail读取多行错误响应

huangapple go评论76阅读模式
英文:

golang: Read multiline error response from smtp.SendMail

问题

我正在使用这段代码:

err := smtp.SendMail(
    smtpHostPort,
    auth,
    sender,
    []string{recipient},
    []byte(message),
)
if err != nil {
    log.Printf("sendSmtp: failure: %q", strings.Split(err.Error(), "\n"))
}

然而,多行错误响应似乎被截断了:

2013/02/06 11:54:41 sendSmtp: failure: ["530 5.5.1 Authentication Required. Learn more at"]

如何获取完整的多行错误响应?

英文:

I am using this code:

err := smtp.SendMail(
    smtpHostPort,
    auth,
    sender,
    []string{recipient},
    []byte(message),
)
if err != nil {
    log.Printf("sendSmtp: failure: %q", strings.Split(err.Error(), "\n"))
}

However the multiline error response seems truncated:

 2013/02/06 11:54:41 sendSmtp: failure: ["530 5.5.1 Authentication Required. Learn more at"]

How can I get the full multiline error response?

答案1

得分: 1

这个问题已经被修复:

https://code.google.com/p/go/issues/detail?id=5700

英文:

For the records, this bug has been fixed:

https://code.google.com/p/go/issues/detail?id=5700

答案2

得分: 0

错误不是多行字符串。

package main

import (
	"errors"
	"log"
	"strings"
)

func main() {
	err := errors.New("530 5.5.1 需要身份验证。了解更多信息,请访问")
	log.Printf("sendSmtp: 失败:%q", strings.Split(err.Error(), "\n"))
	err = errors.New("530 5.5.1 需要身份验证。了解更多信息,请访问\nstackoverflow.com")
	log.Printf("sendSmtp: 失败:%q", strings.Split(err.Error(), "\n"))
}

输出:

2013/02/06 13:30:19 sendSmtp: 失败:["530 5.5.1 需要身份验证。了解更多信息,请访问"]
2013/02/06 13:30:19 sendSmtp: 失败:["530 5.5.1 需要身份验证。了解更多信息,请访问" "stackoverflow.com"]
英文:

The error is not a multi-line string.

package main

import (
	"errors"
	"log"
	"strings"
)

func main() {
	err := errors.New("530 5.5.1 Authentication Required. Learn more at")
	log.Printf("sendSmtp: failure: %q", strings.Split(err.Error(), "\n"))
	err = errors.New("530 5.5.1 Authentication Required. Learn more at\nstackoverflow.com")
	log.Printf("sendSmtp: failure: %q", strings.Split(err.Error(), "\n"))
}

Output:

2013/02/06 13:30:19 sendSmtp: failure: ["530 5.5.1 Authentication Required. Learn more at"]
2013/02/06 13:30:19 sendSmtp: failure: ["530 5.5.1 Authentication Required. Learn more at" "stackoverflow.com"]

huangapple
  • 本文由 发表于 2013年2月7日 01:19:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/14734999.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定