本文整理汇总了Golang中net/smtp.NewClient函数的典型用法代码示例。如果您正苦于以下问题:Golang NewClient函数的具体用法?Golang NewClient怎么用?Golang NewClient使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了NewClient函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Golang代码示例。
示例1: TestTimeout
func TestTimeout(t *testing.T) {
s, err := NewServer(&Config{
Addr: "127.0.0.1:0",
ReadTimeout: 50 * time.Millisecond,
})
if err != nil {
t.Fatal(err)
}
// Connect to the server using its address
conn, err := net.Dial("tcp", s.listener.Addr().String())
if err != nil {
t.Fatal(err)
}
c, err := smtp.NewClient(conn, "localhost")
if err != nil {
t.Fatal(err)
}
// Hang for 100ms to trigger the timeout
time.Sleep(100 * time.Millisecond)
conn.SetDeadline(time.Now().Add(100 * time.Millisecond))
if err := c.Hello("localhost"); err == nil {
t.Fatal(errors.New("timeout expected"))
}
s.Close(false)
}
示例2: sendMail
func sendMail(t *testing.T, unixSocket, testData string) {
conn, err := net.Dial("unix", unixSocket)
if err != nil {
t.Error(err)
}
c, err := smtp.NewClient(conn, "localhost")
defer c.Quit()
if err != nil {
t.Error(err)
}
c.Mail("ignored")
c.Rcpt("ignored")
w, err := c.Data()
defer w.Close()
if err != nil {
t.Error(err)
}
data, err := ioutil.ReadFile(testData)
if err != nil {
t.Error(err)
}
w.Write(data)
}
示例3: newSMTPClient
func newSMTPClient(conn net.Conn, config *model.Config) (*smtp.Client, *model.AppError) {
c, err := smtp.NewClient(conn, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
if err != nil {
l4g.Error("Failed to open a connection to SMTP server %v", err)
return nil, model.NewAppError("SendMail", "Failed to open TLS connection", err.Error())
}
// GO does not support plain auth over a non encrypted connection.
// so if not tls then no auth
auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
if err = c.Auth(auth); err != nil {
return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
}
} else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS {
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: config.EmailSettings.SMTPServer,
}
c.StartTLS(tlsconfig)
if err = c.Auth(auth); err != nil {
return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
}
}
return c, nil
}
示例4: SendTestMessage
// Send the given email messages using this Mailer.
func (m *Mailer) SendTestMessage(basicServer string, messages ...*Message) (actualcmds string, err error) {
if m.Auth == nil {
m.Auth = smtp.PlainAuth(m.UserName, m.UserName, m.Password, m.Server)
}
server := strings.Join(strings.Split(basicServer, "\n"), "\r\n")
var cmdbuf bytes.Buffer
bcmdbuf := bufio.NewWriter(&cmdbuf)
var fake faker
fake.ReadWriter = bufio.NewReadWriter(bufio.NewReader(strings.NewReader(server)), bcmdbuf)
defer func() {
bcmdbuf.Flush()
actualcmds = cmdbuf.String()
}()
c, err := smtp.NewClient(fake, "fake.host")
if err != nil {
return
}
defer c.Quit()
for _, message := range messages {
m.fillDefault(message)
if err = Send(c, message); err != nil {
return
}
}
return
}
示例5: newSMTPClient
func newSMTPClient(conn net.Conn) (*smtp.Client, *model.AppError) {
host, _, _ := net.SplitHostPort(Cfg.EmailSettings.SMTPServer)
c, err := smtp.NewClient(conn, host)
if err != nil {
l4g.Error("Failed to open a connection to SMTP server %v", err)
return nil, model.NewAppError("SendMail", "Failed to open TLS connection", err.Error())
}
// GO does not support plain auth over a non encrypted connection.
// so if not tls then no auth
auth := smtp.PlainAuth("", Cfg.EmailSettings.SMTPUsername, Cfg.EmailSettings.SMTPPassword, host)
if Cfg.EmailSettings.UseTLS {
if err = c.Auth(auth); err != nil {
return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
}
} else if Cfg.EmailSettings.UseStartTLS {
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: host,
}
c.StartTLS(tlsconfig)
if err = c.Auth(auth); err != nil {
return nil, model.NewAppError("SendMail", "Failed to authenticate on SMTP server", err.Error())
}
}
return c, nil
}
示例6: newSMTPClient
func newSMTPClient(conn net.Conn, config *model.Config) (*smtp.Client, *model.AppError) {
c, err := smtp.NewClient(conn, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
if err != nil {
l4g.Error(T("utils.mail.new_client.open.error"), err)
return nil, model.NewLocAppError("SendMail", "utils.mail.connect_smtp.open_tls.app_error", nil, err.Error())
}
auth := smtp.PlainAuth("", config.EmailSettings.SMTPUsername, config.EmailSettings.SMTPPassword, config.EmailSettings.SMTPServer+":"+config.EmailSettings.SMTPPort)
if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_TLS {
if err = c.Auth(auth); err != nil {
return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
}
} else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_STARTTLS {
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: config.EmailSettings.SMTPServer,
}
c.StartTLS(tlsconfig)
if err = c.Auth(auth); err != nil {
return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
}
} else if config.EmailSettings.ConnectionSecurity == model.CONN_SECURITY_PLAIN {
// note: go library only supports PLAIN auth over non-tls connections
if err = c.Auth(auth); err != nil {
return nil, model.NewLocAppError("SendMail", "utils.mail.new_client.auth.app_error", nil, err.Error())
}
}
return c, nil
}
示例7: dial
// dial connects to the smtp server with the request encryption type
func dial(host string, port string, encryption encryption, config *tls.Config) (*smtp.Client, error) {
var conn net.Conn
var err error
address := host + ":" + port
// do the actual dial
switch encryption {
case EncryptionSSL:
conn, err = tls.Dial("tcp", address, config)
default:
conn, err = net.Dial("tcp", address)
}
if err != nil {
return nil, errors.New("Mail Error on dailing with encryption type " + encryption.String() + ": " + err.Error())
}
c, err := smtp.NewClient(conn, host)
if err != nil {
return nil, errors.New("Mail Error on smtp dial: " + err.Error())
}
return c, err
}
示例8: SendAlert
func (e *EmailAlerter) SendAlert(errors map[string][]string) error {
log.Println("dialing:", e.Smtp)
conn, err := net.DialTimeout("tcp", e.Smtp, 15*time.Second)
if err != nil {
return err
}
defer conn.Close()
c, err := smtp.NewClient(conn, e.Smtp)
if err != nil {
return err
}
defer c.Close()
c.Mail(e.From)
c.Rcpt(e.To)
wc, err := c.Data()
if err != nil {
return err
}
defer wc.Close()
buf := bytes.NewBufferString(fmt.Sprintf("%v", errors))
if _, err = buf.WriteTo(wc); err != nil {
return err
}
return nil
}
示例9: Dial
func Dial(addr string) (*smtp.Client, error) {
conn, err := tls.Dial("tcp", addr, nil) //smtp包中net.Dial()当使用ssl连接时会卡住
if err != nil {
fmt.Println(err.Error())
return nil, err
}
host, _, _ := net.SplitHostPort(addr)
return smtp.NewClient(conn, host)
}
示例10: Dial
//return a smtp client
func Dial(addr string) (*smtp.Client, error) {
conn, err := tls.Dial("tcp", addr, nil)
if err != nil {
log.Println("Dialing Error:", err)
return nil, err
}
//分解主机端口字符串
host, _, _ := net.SplitHostPort(addr)
return smtp.NewClient(conn, host)
}
示例11: sendSSL
// SSL方式连接服务器 (与STARTTLS方式不同 适用于465端口)
func (this *SMTPClient) sendSSL(to []string, title, context string) (err error) {
defer util.Catch(&err)
host := this.Server[:strings.Index(this.Server, ":")]
conn, err := tls.Dial("tcp", this.Server, nil)
if err != nil {
return err
}
c, err := smtp.NewClient(conn, host)
if err != nil {
return err
}
*(*bool)(unsafe.Pointer(reflect.ValueOf(c).Elem().FieldByName("tls").UnsafeAddr())) = true // set tls to true for AUTH
if this.auth != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(this.auth); err != nil {
return err
}
}
}
if err = c.Mail(this.User); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
body := []byte(strings.Join([]string{
"From: " + this.User,
"To: " + strings.Join(to, ","),
"Subject: " + title,
"Content-Type: text/html; charset=utf-8;",
"",
context,
}, "\r\n"))
_, err = w.Write(body)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}
示例12: sendMail
// sendMail connects to the server at addr, switches to TLS if possible (using the given config),
// authenticates with mechanism a if possible, and then sends an email from
// address from, to addresses to, with message msg.
//
// If msg is nil, then quits, this testing the recipients and the server
func sendMail(addr string, a smtp.Auth, from string, to []string, msg []byte, timeout time.Duration, tlsConfig *tls.Config) error {
//c, err := Dial(addr)
conn, err := net.DialTimeout("tcp", addr, timeout)
if err != nil {
return err
}
host, _, _ := net.SplitHostPort(addr)
c, err := smtp.NewClient(conn, host)
if err != nil {
return err
}
//if err := c.hello(); err != nil {
// return err
//}
if err := c.Hello("localhost"); err != nil {
return err
}
if ok, _ := c.Extension("STARTTLS"); ok {
if err = c.StartTLS(tlsConfig); err != nil {
return err
}
}
if a != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
return err
}
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
if msg != nil {
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
}
return c.Quit()
}
示例13: send_digest
// copy & past https://gist.github.com/chrisgillis/10888032
func send_digest(smtp_conn smtp_conn_type, msg []byte) error {
servername := strings.Join([]string{smtp_conn.Host, smtp_conn.Port}, ":")
auth := smtp.PlainAuth("", smtp_conn.Login, smtp_conn.Password, smtp_conn.Host)
// TLS config
tlsconfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: smtp_conn.Host,
}
conn, err := tls.Dial("tcp", ""+servername, tlsconfig)
if err != nil {
return err
}
c, err := smtp.NewClient(conn, smtp_conn.Host)
if err != nil {
return err
}
if err = c.Auth(auth); err != nil {
return err
}
if err = c.Mail(smtp_conn.Login); err != nil {
return err
}
if err = c.Rcpt(email); err != nil {
return err
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
c.Quit()
return nil
}
示例14: sendEmail
func sendEmail(recipientAddress string, subject string, messageBody string) {
if !credentialsHaveBeenLoaded {
log.Panic("Outgoing email credentials have not been set. Cannot send message.")
}
from := mail.Address{credentials.NoReplyAddressName, credentials.NoReplyAddress}
headers := make(map[string]string)
headers["From"] = from.String()
headers["To"] = recipientAddress
headers["Subject"] = subject
message := ""
for headerName, headerValue := range headers {
message += fmt.Sprintf("%s: %s\r\n", headerName, headerValue)
}
message += "\r\n" + messageBody
mailAuth := smtp.PlainAuth("", credentials.NoReplyAddress, credentials.NoReplyPassword, credentials.Host)
tlsConfig := &tls.Config{
InsecureSkipVerify: true,
ServerName: credentials.Host,
}
tcpConnection, error := tls.Dial("tcp", credentials.Host+":"+credentials.Port, tlsConfig)
panicOnError(error)
smtpClient, error := smtp.NewClient(tcpConnection, credentials.Host)
panicOnError(error)
error = smtpClient.Auth(mailAuth)
panicOnError(error)
error = smtpClient.Mail(credentials.NoReplyAddress)
panicOnError(error)
error = smtpClient.Rcpt(recipientAddress)
panicOnError(error)
emailStream, error := smtpClient.Data()
panicOnError(error)
_, error = emailStream.Write([]byte(message))
panicOnError(error)
error = emailStream.Close()
panicOnError(error)
smtpClient.Quit()
}
示例15: SendMailSSL
// SendMailSSL envoie un email par SSL
func SendMailSSL(addr string, a smtp.Auth, from string, to []string, msg []byte) error {
conn, err := tls.Dial("tcp", addr, &tls.Config{InsecureSkipVerify: true}) //TODO: Not secure
if err != nil {
log.Println("Error Dialing", err)
return err
}
h, _, _ := net.SplitHostPort(addr)
c, err := smtp.NewClient(conn, h)
if err != nil {
log.Println("Error SMTP connection", err)
return err
}
defer c.Close()
if a != nil {
if ok, _ := c.Extension("AUTH"); ok {
if err = c.Auth(a); err != nil {
return err
}
}
}
if err = c.Mail(from); err != nil {
return err
}
for _, addr := range to {
if err = c.Rcpt(addr); err != nil {
return err
}
}
w, err := c.Data()
if err != nil {
return err
}
_, err = w.Write(msg)
if err != nil {
return err
}
err = w.Close()
if err != nil {
return err
}
return c.Quit()
}