以编程方式向MailHog发送邮件

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

Programmatically Send Mail to MailHog

问题

我正在尝试使用Go中的MailHog来捕获电子邮件。然而,他们的API没有展示如何在Go中向其发送电子邮件的示例。我想知道是否有人有一些关于如何实现的示例。

英文:

I'm trying to catch e-mails using MailHog in Go. However, their API doesn't demonstrate how to send an email to it in Go itself. I was wondering if anyone has some sample on how to.

答案1

得分: 1

我建议你使用我的库Gomail

package main

import "gopkg.in/gomail.v2"

func main() {
    m := gomail.NewMessage()
    m.SetHeader("From", "from@example.com")
    m.SetHeader("To", "to@example.com")
    m.SetHeader("Subject", "Hello!")
    m.SetBody("text/plain", "What's up?")

    d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")
    if err := d.DialAndSend(m); err != nil {
        panic(err)
    }
}
英文:

I advise you to use my library Gomail:

package main

import "gopkg.in/gomail.v2"

func main() {
	m := gomail.NewMessage()
	m.SetHeader("From", "from@example.com")
	m.SetHeader("To", "to@example.com")
	m.SetHeader("Subject", "Hello!")
	m.SetBody("text/plain", "What's up?")

	d := gomail.NewPlainDialer("smtp.example.com", 587, "user", "123456")
	if err := d.DialAndSend(m); err != nil {
		panic(err)
	}
}

答案2

得分: 1

我在我的机器上尝试运行Mail Hog。这是代码:

{
MailMessage mail = new MailMessage();
mail.To.Add("glitson@gmail.com");

mail.From = new MailAddress("priyesh@gmail.com");
mail.Subject = "使用Gmail发送电子邮件";
string Body = "你好";
mail.Body = Body;
SmtpClient smtp = new SmtpClient();
smtp.Host = "本地主机"; //或者你的SMTP服务器地址
smtp.Port = 1025;
smtp.UseDefaultCredentials = false;
smtp.Credentials = new System.Net.NetworkCredential("用户名", "密码");

//或者你的SMTP邮箱ID和密码
//smtp.EnableSsl = true;
smtp.Send(mail);

}

英文:

I Tried by run mail hog in my machine. This is the code
{

            MailMessage mail = new MailMessage();
            mail.To.Add("glitson@gmail.com");

            mail.From = new MailAddress("priyesh@gmail.com");
            mail.Subject = "Email using Gmail";
            string Body = "Hello";
            mail.Body = Body;
            SmtpClient smtp = new SmtpClient();
            smtp.Host = "Localhost"; //Or Your SMTP Server Address
            smtp.Port = 1025;
            smtp.UseDefaultCredentials = false;
            smtp.Credentials = new System.Net.NetworkCredential
            ("username", "password");

            //Or your Smtp Email ID and Password
            //smtp.EnableSsl = true;
            smtp.Send(mail);

}

huangapple
  • 本文由 发表于 2015年11月11日 08:01:57
  • 转载请务必保留本文链接:https://go.coder-hub.com/33641975.html
匿名

发表评论

匿名网友

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

确定