LoginSignup
23
25

More than 5 years have passed since last update.

windowsでコマンド、CUIベースでメールする方法

Last updated at Posted at 2015-10-26

windowsでバッチ処理でメールを送りたいというときに、どんな方法があるか調べてみた。

SMTPサーバ

smtpサーバは既に準備されていれば良いが、ない場合は自前で用意する必要がある。
Windows Serverの場合はIIS6マネージャを使って構築可能。
 http://blog.suz-lab.com/2013/08/windows-2008-r2-sp1ec2smtp.html
 
 gmailのsmtpが使えそうな気がするけれども試していない。
参考
 http://kitaney-google.blogspot.jp/2014/08/gmailsmtpgmail.html
 https://support.google.com/a/answer/2956491?hl=ja

 smtpサーバを準備するのが面倒なので、ダミーのSMTPサーバsmtp4devを使用した。 
 http://www.atmarkit.co.jp/ait/articles/1410/23/news017.html
 stmpの代わりに設置して、実際にメールを送信しなくても送ったメールの内容がわかるのでテストに便利。

telnet

WindowsにTelnetクライアントは標準で入っていないので、追加でインストールする必要がある。これは試してない。



参考URL
http://memorva.jp/memo/dev/windows_telnet_smtp_pop.php
http://d.hatena.ne.jp/shima111/20051019/p1

WSH(VBScript)

mail.vbs
 Set oMsg = CreateObject("CDO.Message")
oMsg.From = "mailsender@example.co.jp"
oMsg.To = "user@example.co.jp"
oMsg.Subject = "Test"
oMsg.TextBody = "テストメッセージです" & vbCrLf & Now
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") =  "mail.example.co.jp"
 oMsg.Configuration.Fields.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
 oMsg.Configuration.Fields.Update
 oMsg.Send

cscript mail.vbs

参考URL
http://www.atmarkit.co.jp/ait/articles/0405/22/news017.html

PowerShell

これが一番良さそう。ポートの指定や認証方式など細かく設定できる。詳しくは参考サイトで。
あと、コメント欄のarachan@githubさんのコメントも参考になります。

Send-MailMessageを使った場合

mail.ps1

send-mailmessage -to "User01 <user01@example.com>" -from "User02 <user02@example.com>" -subject "Test mail" -SmtpServer localhost

.NETのSmtpClientを使った場合

mail.ps1
$EmailFrom = “test@example.com” 
$EmailTo ="test@example.com" 
$Subject = "タイトル" 
$Body = "本文"  
$SMTPServer = "localhost" 
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 25) 
$SMTPClient.Send($EmailFrom, $EmailTo, $Subject, $Body)

参考URL
http://qiita.com/arachan@github/items/e93c2fec8fe5f7f66bfa
http://blogs.technet.com/b/junichia/archive/2011/12/08/3469710.aspx
https://technet.microsoft.com/en-us/library/hh849925(v=wps.620).aspx

Go

公式サイトで紹介されている方法。

mail.go
package main

import (
    "log"
    "net/smtp"
)

func main() {
    // Set up authentication information.
    auth := smtp.PlainAuth("", "user@example.com", "password", "mail.example.com")

    // Connect to the server, authenticate, set the sender and recipient,
    // and send the email all in one step.
    to := []string{"recipient@example.net"}
    msg := []byte("To: recipient@example.net\r\n" +
        "Subject: discount Gophers!\r\n" +
        "\r\n" +
        "This is the email body.\r\n")
    err := smtp.SendMail("mail.example.com:25", auth, "sender@example.org", to, msg)
    if err != nil {
        log.Fatal(err)
    }
}

utf8で送りたい場合は、go で utf8メールを送信を使用。

テストではlocalhost:25でSSLなしでダミーのSMTPを用意していたので
Go言語でSSLを使わずにSMTPでメールを送るコード片を使用。

mail.go

package main

import (
  "net/smtp"
  "log"
  "os"
  "net"
)

func main() {
  conn, err := net.Dial("tcp", "localhost:25")
  if err != nil {
    log.Println(err)
    os.Exit(1);
  }
  client, _ := smtp.NewClient(conn, "localhost")
  if err = client.Mail("from@example.com"); err != nil {
    log.Println(err)
    os.Exit(1);
  }
  if err = client.Rcpt("to@example.com"); err != nil {
    log.Println(err)
    os.Exit(1);
  }
  w, err := client.Data()
  if err != nil {
    log.Println(err)
    os.Exit(1);
  }
  _, err = w.Write([]byte("hello"))
  w.Close()
  result := client.Quit()

  log.Println(result)
}

参考URL
https://golang.org/pkg/net/smtp/
http://qiita.com/yamasaki-masahide/items/a9f8b43eeeaddbfb6b44
http://nisenabe.hatenablog.com/entry/2012/08/04/042934

PHP

php.iniにsmtpサーバを記載する必要あり。
extension=php_mbstring.dllを有効にする。

mail.php
<?php
mb_language("Japanese");
mb_internal_encoding("utf8");
$to = "test@example.com";
$subject = "けんめい";
$body = "ほんぶん";
$from = "test@example.com";
 mb_send_mail($to,$subject,$body,"From:".$from);

参考URL
http://techblog.ecstudio.jp/tech-tips/mail-japanese-basics.html
http://www.phpbook.jp/tutorial/mailini/index1.html

23
25
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
23
25