mxrelay-consumer/examples/simple-usage.ts
2025-08-18 22:22:04 +07:00

61 lines
No EOL
1.9 KiB
TypeScript

#!/usr/bin/env ts-node
/**
* Simple usage example for SMTP over WebSocket client
*/
import { SMTPOverWSClient } from '../src/index';
async function simpleUsageExample() {
console.log('Simple SMTP WebSocket Client Example\n');
// Create client - that's it, everything else is automatic
const client = new SMTPOverWSClient({
url: 'ws://localhost:3000/smtp',
apiKey: 'your-api-key-here',
debug: true
});
try {
// Just send commands - client handles connection, queuing, everything automatically
console.log('Sending SMTP commands...');
const response1 = await client.sendSMTPCommand('EHLO example.com\r\n');
console.log('EHLO:', response1.trim());
const response2 = await client.sendSMTPCommand('MAIL FROM: <test@example.com>\r\n');
console.log('MAIL FROM:', response2.trim());
const response3 = await client.sendSMTPCommand('RCPT TO: <recipient@example.com>\r\n');
console.log('RCPT TO:', response3.trim());
const response4 = await client.sendSMTPCommand('DATA\r\n');
console.log('DATA:', response4.trim());
const response5 = await client.sendSMTPCommand('Subject: Test\r\n\r\nHello World!\r\n.\r\n');
console.log('Message:', response5.trim());
const response6 = await client.sendSMTPCommand('QUIT\r\n');
console.log('QUIT:', response6.trim());
console.log('\nAll done! Client automatically handled connection and cleanup.');
} catch (error) {
console.error('Error:', error);
} finally {
// Optional - client will clean up automatically when process exits
await client.shutdown();
}
}
// Run the example
if (require.main === module) {
simpleUsageExample()
.then(() => process.exit(0))
.catch((error) => {
console.error('Example failed:', error);
process.exit(1);
});
}
export { simpleUsageExample };