[转]emailjs-smtp-client
本文转自:https://github.com/emailjs/emailjs-smtp-client/blob/master/README.md
SMTP Client
SMTP Client allows you to connect to and stream data to a SMTP server in the browser.
StringEncoding API
This module requires TextEncoder and TextDecoder to exist as part of the StringEncoding API (see: MDN whatwg.org). Firefox 19+ is basically the only browser that supports this at the time of writing, while Chromium in canary, not stable. Luckily, there is a polyfill!
Depending on your browser, you might need this polyfill for ArrayBuffer #slice, e.g. phantomjs.
TCPSocket API
There is a shim that brings Mozilla-flavored version of the Raw Socket API to other platforms.
If you are on a platform that uses forge instead of a native TLS implementation (e.g. chrome.socket), you have to set the .oncert(pemEncodedCertificate) handler that passes the TLS certificate that the server presents. It can be used on a trust-on-first-use basis for subsequent connection.
If forge is used to handle TLS traffic, you may choose to handle the TLS-related load in a Web Worker. Please use tlsWorkerPath to point to tcp-socket-tls-worker.js!
Please take a look at the tcp-socket documentation for more information!
Installation
npm:
npm install --save emailjs-smtp-client
Quirks
STARTTLSis currently not supported- Only
PLAIN,USERandXOAUTH2authentication mechanisms are supported.XOAUTH2expects a ready to use access token, no tokens are generated automatically.
Usage
AMD
Require emailjs-smtp-client.js as emailjs-smtp-client
Global context
Include files emailjs-smtp-client-response-parser.js and emailjs-smtp-client.js on the page.
<script src="emailjs-smtp-client-response-parser.js"></script>
<script src="emailjs-smtp-client.js"></script>
This exposes global variable emailjs-smtp-client
API
Create SmtpClient object with:
var client = new SmtpClient(host, port, options)
where
- host is the hostname to connect to (defaults to "localhost")
- port is the port to connect to
- options is an optional options object (see below)
Connection options
The following connection options can be used with simplesmtp.connect:
- useSecureTransport Boolean Set to true, to use encrypted connection
- name String Client hostname for introducing itself to the server
- auth Object Authentication options. Depends on the preferred authentication method
- user is the username for the user (also applies to OAuth2)
- pass is the password for the user if plain auth is used
- xoauth2 is the OAuth2 access token to be used instead of password. If both password and xoauth2 token are set, the token is preferred.
- authMethod String Force specific authentication method (eg.
"PLAIN"for usingAUTH PLAINor"XOAUTH2"forAUTH XOAUTH2) - ca (optional) (only in conjunction with this TCPSocket shim) if you use TLS with forge, pin a PEM-encoded certificate as a string. Please refer to the tcp-socket documentation for more information!
- tlsWorkerPath (optional) (only in conjunction with this TCPSocket shim) if you use TLS with forge, this path indicates where the file for the TLS Web Worker is located. Please refer to the tcp-socket documentation for more information!
- disableEscaping Boolean If set to true, do not escape dots on the beginning of the lines
- logLength Number How many messages between the client and the server to log. Set to false to disable logging. Defaults to 6
- ignoreTLS – if set to true, do not issue STARTTLS even if the server supports it
- requireTLS – if set to true, always use STARTTLS before authentication even if the host does not advertise it. If STARTTLS fails, do not try to authenticate the user
- lmtp - if set to true use LMTP commands instead of SMTP commands
Default STARTTLS support is opportunistic – if the server advertises STARTTLS in EHLO response, the client tries to use it. If STARTTLS is not advertised, the clients sends passwords in the plain. You can use ignoreTLS and requireTLS to change this behavior by explicitly enabling or disabling STARTTLS usage.
XOAUTH2
To authenticate using XOAUTH2, use the following authentication config
var config = {
auth: {
user: 'username',
xoauth2: 'access_token'
}
};
See XOAUTH2 docs for more info.
Connection events
Once a connection is set up the following events can be listened to:
- onidle - the connection to the SMTP server has been successfully set up and the client is waiting for an envelope. NB! this event is emitted multiple times - if an e-mail has been sent and the client has nothing to do,
onidleis emitted again. - onready
(failedRecipients)- the envelope is passed successfully to the server and a message stream can be started. The argument is an array of e-mail addresses not accepted as recipients by the server. If none of the recipient addresses is accepted,onerroris emitted instead. - ondone
(success)- the message was sent - onerror
(err)- An error occurred. The connection will be closed shortly afterwards, so expect anoncloseevent as well - onclose
(isError)- connection to the client is closed. IfisErroris true, the connection is closed because of an error
Example:
client.onidle = function(){
console.log("Connection has been established");
// this event will be called again once a message has been sent
// so do not just initiate a new message here, as infinite loops might occur
}
Sending an envelope
When an onidle event is emitted, an envelope object can be sent to the server. This includes a string from and a single string or an array of strings for to property.
Envelope can be sent with client.useEnvelope(envelope)
// run only once as 'idle' is emitted again after message delivery
var alreadySending = false; client.onidle = function(){
if(alreadySending){
return;
}
alreadySending = true;
client.useEnvelope({
from: "me@example.com",
to: ["receiver1@example.com", "receiver2@example.com"]
});
}
The to part of the envelope must include all recipients from To:, Cc: and Bcc: fields.
If envelope setup up fails, an error is emitted. If only some (not all) recipients are not accepted, the mail can still be sent. An onready event is emitted when the server has accepted the from and at least one to address.
client.onready = function(failedRecipients){
if(failedRecipients.length){
console.log("The following addresses were rejected: ", failedRecipients);
}
// start transfering the e-mail
}
Sending a message
When onready event is emitted, it is possible to start sending mail. To do this you can send the message with client.sendcalls (you also need to call client.end() once the message is completed).
send method returns the state of the downstream buffer - if it returns true, it is safe to send more data, otherwise you should (but don't have to) wait for the ondrain event before you send more data.
NB! you do not have to escape the dots in the beginning of the lines by yourself (unless you specificly define so with disableEscaping option).
client.onready = function(){
client.send("Subject: test\r\n");
client.send("\r\n");
client.send("Message body");
client.end();
}
Once the message is delivered an ondone event is emitted. The event has an parameter which indicates if the message was accepted by the server (true) or not (false).
client.ondone = function(success){
if(success){
console.log("The message was transmitted successfully with "+response);
}
}
Logging
At any time you can access the traffic log between the client and the server from the client.log array.
client.ondone = function(success){
// show the last message
console.log(client.log.slice(-1));
}
Closing the connection
Once you have done sending messages and do not want to keep the connection open, you can gracefully close the connection with client.quit() or non-gracefully (if you just want to shut down the connection and do not care for the server) with client.close().
If you run quit or close in the ondone event, then the next onidle is never called.
Get your hands dirty
git clone git@github.com:whiteout-io/smtpclient.git
cd smtpclient
npm install && npm test
To run the integration tests against a local smtp server
grunt smtp
add the test folder as a chrome app (chrome settings -> extensions -> check 'developer mode' -> load unpacked extension)
[转]emailjs-smtp-client的更多相关文章
- 【RL-TCPnet网络教程】第34章 RL-TCPnet之SMTP客户端
第34章 RL-TCPnet之SMTP客户端 本章节为大家讲解RL-TCPnet的SMTP应用,学习本章节前,务必要优先学习第33章的SMTP基础知识.有了这些基础知识之后,再搞本章节会有事 ...
- .net SMTP 发送邮件
using System.Net.Mail; public static bool SendMail(string messTo,string messBody) { MailMessage mess ...
- C#发送电子邮件(SMTP)及outlook.com账号之概要
这是关于c#发送电子邮件(SMTP)的技术笔记,以”简报“形式呈现. 因为最后成功通过outlook.com发送了邮件,所以,我觉得还是有必要 记录一下其中的要点. 一.技术核心 .net Frame ...
- C#发送Outlook邮件(仅SMTP版本)
先表明Outlook的参数:网址:https://support.office.com/zh-cn/article/Outlook-com-%E7%9A%84-POP%E3%80%81IMAP-%E5 ...
- Go语言实战 - 使用SendCloud群发邮件
山坡网需要能够每周给注册用户发送一封名为"本周最热书籍"的邮件,而之前一直使用的腾讯企业邮箱罢工了,提示说发送请求太多太密集. 一番寻找之后发现了大家口碑不错的搜狐SendClou ...
- error-2016-1-18
SSL 连接出错 错误: "System.Net.Mail.SmtpException"类型的未经处理的异常在 System.dll 中发生 其他信息: SMTP 服务器要求安全连 ...
- shell脚本常规技巧
邮件相关 发送邮件: #!/usr/bin/python import sys; import smtplib; from email.MIMEText import MIMEText mail_ho ...
- SSIS Send Mail
在SSIS中Send Mail的方法主要有三种,使用Send Mail Task,使用Script Task和使用存储过程msdb.dbo.sp_send_dbmail. 一,使用Send Mail ...
- C#邮件发送问题(一)
邮件发送需考虑很多因素,包括发送邮件客户端(一般编码实现),发送和接收邮件服务器设置等.如果使用第三方邮件服务器作为发送服务器,就需要考虑该服务器的发送限制,(如发送邮件时间间隔,单位时间内发送邮件数 ...
随机推荐
- [UWP开发]处理手机后退事件
众所周知,uwp程序是一套代码,可以run在不同的平台上.但是不同的设备肯定有其独特之处,所以针对这些独特之处,必须用“独特的代码”来处理. 所以微软提供了一系列的拓展类库来实现这种特殊处理. 如上图 ...
- Linux磁盘及文件系统(三)Linux文件系统
一.文件系统的组成 Linux常见的文件系统类型有ReiserFS,ext2,ext3,ext4,vfat,XFS等,文件系统是对一个存储设备上数据和元数据进行组织的机制.他的最终目的是把大量数据有组 ...
- C#取得控制台应用程序的根目录方法
如有雷同,不胜荣幸,若转载,请注明 取得控制台应用程序的根目录方法1:Environment.CurrentDirectory 取得或设置当前工作目录的完整限定路径2:AppDomain.Curren ...
- AngularJS源码解析1:angular自启动过程
angularJS加载进来后,会有一个立即执行函数调用,在源代码的最下面是angular初始化的地方.代码展示: bindJQuery(); publishExternalAPI(angular); ...
- 如何无人值守安装linux系统(上)
如何开始 Linux 的无人值守安装 一.预备知识: I.什么是PXE PXE并不是一种安装方式,而是一种引导方式.进行PXE安装的必要条件是要安装的计算机中包含一个PXE支持的网卡(NIC),即网卡 ...
- python中type、class、object的区别
type 一. type可以用来返回一个对象的类型 例如: 二. 由于Python中一切皆对象,也就是说Python中的任何变量类型都是可以被修改的,这也是Python等动态编程语言的特点.type的 ...
- 5、Tensorflow基础(三)神经元函数及优化方法
1.激活函数 激活函数(activation function)运行时激活神经网络中某一部分神经元,将激活信息向后传入下一层的神经网络.神经网络之所以能解决非线性问题(如语音.图像识别),本质上就是激 ...
- net 反编译神器
文章地址:https://www.cnblogs.com/sheng-jie/p/10168411.html dnSpy官网下载 分享链接 .net core源码导航 https://www.cnb ...
- 进阶篇:4.3)DFA设计指南:宽松公差及人性装配及其他
本章目的:设计需要为装配考虑,给他们提供各种优待,装配才能做出好产品. 1.前言 机械贴合现实而软件远离现实. 越是学习机械设计的原则,越是感觉他们和一些做人做事的道理相同的. 如,机械设计原则都是有 ...
- order by中用子查询排序
今天有个需求是对一个列表排序,但是排序字段是在另一个表中,不想用关联查询,就想能否直接在order by中用子查询,后来找到一个还挺好使.记录如下. 排序语句如下: select * from mai ...