本文整理匯總了TypeScript中smtp-server.SMTPServer.on方法的典型用法代碼示例。如果您正苦於以下問題:TypeScript SMTPServer.on方法的具體用法?TypeScript SMTPServer.on怎麽用?TypeScript SMTPServer.on使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類smtp-server.SMTPServer
的用法示例。
在下文中一共展示了SMTPServer.on方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的TypeScript代碼示例。
示例1: SMTPServerConnectTest
function SMTPServerConnectTest() {
let options: SMTPServerOptions = {
secure: false,
};
let server = new SMTPServer(options);
server.on('error', err => {
console.log('Error %s', err.message);
});
server.listen(2323);
}
示例2: callback
stream.on('data', (chunk: Buffer) => {
console.log(`[${session.id}] onData got data chunk ${chunk.length} bytes`);
messageLength += chunk.length;
});
stream.once('end', () => {
console.log(`[${session.id}] onData finished after reading ${messageLength} bytes`);
callback();
});
}
function onClose(session: SMTPServerSession) {
console.log(`[${session.id}] onClose`);
}
const server = new SMTPServer(options);
server.on('error', (err) => {
console.log(`Server got error:`, err);
});
server.on('close', () => {
console.log('Server closed');
});
server.listen(port, () => {
const address = server.server.address();
console.log(`Listening on [${address.address}]:${address.port}`);
});
示例3: test_with_handlers_as_options
function test_with_handlers_as_options() {
const options: SMTPServerOptions = {
hideSTARTTLS: true,
onConnect,
onMailFrom,
onRcptTo,
onData,
onClose,
};
const port = 2525;
function onConnect(session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onConnect`);
callback();
}
function onAuth(auth: SMTPServerAuthentication, session: SMTPServerSession, callback: (err: Error | undefined, response?: SMTPServerAuthenticationResponse) => void): void {
if (auth.method === 'PLAIN' && auth.username === 'username' && auth.password === 'password') {
callback(undefined, { user: auth.username });
} else {
callback(new Error('Invalid username or password'));
}
}
function onMailFrom(from: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onMailFrom ${from.address}`);
if (from.address.split('@')[1] === 'spammer.com') {
// code 421 disconnects SMTP session immediately
callback(Object.assign(new Error('we do not like spam!'), { responseCode: 421 }));
} else {
callback();
}
}
function onRcptTo(to: SMTPServerAddress, session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onRcptTo ${to.address}`);
callback();
}
function onData(stream: Readable, session: SMTPServerSession, callback: (err?: Error) => void): void {
console.log(`[${session.id}] onData started`);
let messageLength = 0;
stream.on('data', (chunk: Buffer) => {
console.log(`[${session.id}] onData got data chunk ${chunk.length} bytes`);
messageLength += chunk.length;
});
stream.once('end', () => {
console.log(`[${session.id}] onData finished after reading ${messageLength} bytes`);
callback();
});
}
function onClose(session: SMTPServerSession) {
console.log(`[${session.id}] onClose`);
}
const server = new SMTPServer(options);
server.on('error', (err) => {
console.log(`Server got error:`, err);
});
server.on('close', () => {
console.log('Server closed');
});
server.listen(port, () => {
const address = server.server.address() as AddressInfo;
console.log(`Listening on [${address.address}]:${address.port}`);
});
server.close(() => {
console.log('Server closed');
});
}