Last active
October 3, 2017 04:17
-
-
Save kwmt/6699756 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"bytes" | |
"log" | |
"net/smtp" | |
) | |
func main() { | |
// Connect to the remote SMTP server. | |
c, err := smtp.Dial("localhost:25") | |
if err != nil { | |
log.Fatal(err) | |
} | |
// Set the sender and recipient. | |
c.Mail("sender@localhost") // メールの送り主を指定 | |
c.Rcpt("[email protected]") // 受信者を指定 | |
c.Rcpt("[email protected]") // Ccにしたい場合も同様に指定 | |
// Send the email body. | |
wc, err := c.Data() | |
if err != nil { | |
log.Fatal(err) | |
} | |
defer wc.Close() | |
//ToにするかCcにするかBccにするかはDATAメッセージ次第 | |
buf := bytes.NewBufferString("To:[email protected]") | |
buf.WriteString("\r\n") // DATA メッセージはCRLFのみ | |
buf.WriteString("Cc:[email protected]") | |
buf.WriteString("\r\n") | |
buf.WriteString("Subject:this is Subject") //件名 | |
buf.WriteString("\r\n") | |
buf.WriteString("This is the email body.") | |
if _, err = buf.WriteTo(wc); err != nil { | |
log.Fatal(err) | |
} | |
c.Quit() //メールセッションの終了 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
buf.WriteString("")に
From: xxxx
がないと弾かれるケースがあります。