以编程方式将邮件发送到MailHog

时间:2015-11-11 00:01:57

标签: email go

我正试图在Go中使用MailHog捕获电子邮件。但是,他们的API并未演示如何在Go中向其发送电子邮件。我想知道是否有人有关于如何做的样本。

2 个答案:

答案 0 :(得分:1)

我在我的机器上尝试过运行邮件猪。这是代码 {

            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);

}

答案 1 :(得分:0)

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)
    }
}