Subscribed unsubscribe Subscribe Subscribe

枕を欹てて聴く

香炉峰の雪は簾を撥げて看る

FirefoxからSMTPでメールを送る

Simple Mail を参考に.
responseのチェックもしてないし, error処理も書いてないけど要はこの流れでできるということで.
使うときには, error処理とかも書かないと.
命令先は, Gmailsmtpサーバーで.
socketつないで, inputStreamを読みながら, outputstreamに命令を書き出してけばいける.
まんまterminalと同じ感じ?

/*
 *  SMTP Send Mail Sample
 */
var Cc = Components.classes;
var Ci = Components.interfaces;
var SocketService = Cc["@mozilla.org/network/socket-transport-service;1"].getService(Ci.nsISocketTransportService);
var PrefService   = Cc["@mozilla.org/preferences-service;1"].getService(Ci.nsIPrefService);
var ProxyService = Cc["@mozilla.org/network/protocol-proxy-service;1"].getService(Ci.nsIProtocolProxyService);
var DNSService = Cc["@mozilla.org/network/dns-service;1"].getService(Ci.nsIDNSService);


/*
 *  Config
 *  とりあえずGmail経由で送りつける
 *  Gmailのsmtpのportは25ではなく465
 */
var ssl = true;
var host = 'smtp.gmail.com';
var port = 465;
var login = '';// Gmailのアカウント.@gmail.com部分は不要
var password = '';// Gmailのpassword
// 送り元 <>でくるむのがsmtpのマナー
var from = '<foo_bar_hogehoge@gmail.com>';
// 送り先 <>でくるむのがsmtpのマナー
var to   = '<foo_bar_hogehoge@yahoo.co.jp>';

// 送るtext
var text = <![CDATA[
MIME-Version: 1.0
From: <foo_bar_hogehoge@gmail.com>
To: <foo_bar_hogehoge@yahoo.co.jp>
Subject: OKOK
Date: Thu, 28 Aug 2008 00:02:33 +0900
User-Agent: Simple Mail 2.69
Content-Type: text/html; charset=Shift_JIS
Content-Transfer-Encoding: 8bit

test<br>

]]>.toString();
// 使いやすさとか考えて, tasklistを途中でiteratorに変更

function simpleIter(arr, ost){
  var c = arr.length;
  return {
    next:function(response){
      if(!c) return;
      c--;
      [command, func] = arr.shift();
      func(response);
      function write() {
        try {
          // 書き込んで, その長さを取得
          var count = ost.write(command, command.length);
          // 全部書き込んでなかったらAsyncOutputStreamで続きを
          if (count < command.length) {
            command = command.substr(count);
            ost.QueryInterface(Ci.nsIAsyncOutputStream);
            ost.asyncWait({ onOutputStreamReady: function() {
                                    write();
                                  }}, 0, 0, null);
          } else{
          // 全部書き込んだらキャリッジリターン
            ost.write("\r\n", 2);
          }
        }
        catch(e) {  }
      }
      write();
    }
  }
}
// 作ってる最中は二番目のほうにlog入れてた
var tasklist = [
  ['EHLO ' + getHostName(), function(res){  }],
  ['AUTH LOGIN',            function(res){  }],
  [btoa(login),             function(res){  }],
  [btoa(password),          function(res){  }],
  ['MAIL FROM:' + from,     function(res){  }],
  ['RCPT TO:' + to,         function(res){  }],
  ['DATA',                  function(res){  }],
  [text + "\r\n.",          function(res){  }],
  ['QUIT',                  function(res){  }],
];
var data = '';

// Simple Mail から拝借
function getProxyInfo() {
  if (PrefService.getIntPref("network.proxy.type") == 1) {
    var proxyHost = PrefService.getCharPref("network.proxy.socks");
    var proxyPort = PrefService.getIntPref("network.proxy.socks_port");
    if (proxyHost && proxyPort) {
      return ProxyService.newProxyInfo("socks", proxyHost, proxyPort, 0, 30, null);
    }
  }
}

function getHostName() {
    return DNSService.myHostName;
}

// socketを開く
var socket = SocketService.createTransport(ssl ? ["ssl"] : null, ssl ? 1 : 0, host, port, getProxyInfo());

// socket I/O
// inputStreamからresponseをとるためにBinaryStreamも用意
var ostream = socket.openOutputStream(0, 1024*1024, 1);
var istream = socket.openInputStream(0, 1024*1024, 1);
var bstream = Cc["@mozilla.org/binaryinputstream;1"].createInstance(Ci.nsIBinaryInputStream);
bstream.setInputStream(istream);
var iterator = simpleIter(tasklist, ostream);

// pump
// listenerを独自定義

var listener = {
  onStartRequest: function(request, context){},
  onStopRequest: function(request, context, status){
    bstream.close();
    istream.close();
    ostream.close();
  },
  onDataAvailable: function(request, context, input, offset, count){
    data += bstream.readBytes(count);
    iterator.next(data);
  },
}

var pump = Cc["@mozilla.org/network/input-stream-pump;1"].createInstance(Ci.nsIInputStreamPump);
pump.init(istream, -1, -1, 0, 0, false);
pump.asyncRead(listener, null);