英文:
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);
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论