Gakho
Gakho

Reputation: 673

Programmatically Send Mail to MailHog

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.

Upvotes: 1

Views: 2578

Answers (2)

Glitson George
Glitson George

Reputation: 53

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

            MailMessage mail = new MailMessage();
            mail.To.Add("[email protected]");

            mail.From = new MailAddress("[email protected]");
            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);

}

Upvotes: 1

Ale
Ale

Reputation: 2014

I advise you to use my library Gomail:

package main

import "gopkg.in/gomail.v2"

func main() {
    m := gomail.NewMessage()
    m.SetHeader("From", "[email protected]")
    m.SetHeader("To", "[email protected]")
    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)
    }
}

Upvotes: 1

Related Questions