How to use SMTPConnection method in mountebank

Best JavaScript code snippet using mountebank

smtpConnectionHandler.js

Source:smtpConnectionHandler.js Github

copy

Full Screen

1function SMTPconnectionHandler(transport)2{3 try4 {5 var scriptLoader = Components.classes["@mozilla.org/moz/jssubscript-loader;1"].6 getService(Components.interfaces.mozIJSSubScriptLoader);7 scriptLoader.loadSubScript("chrome://web-mail/content/common/DebugLog.js");8 scriptLoader.loadSubScript("chrome://web-mail/content/common/base64.js");9 this.m_SMTPLog = new DebugLog("webmail.logging.comms",10 "{3c8e8390-2cf6-11d9-9669-0800200c9a66}",11 "SMTPConnectionlog");12 this.m_SMTPLog.Write("nsSMTPConnectionHander.js - START");13 var date = new Date();14 this.iID = date.getHours()+ "-" + date.getMinutes() + "-"+ date.getUTCMilliseconds();15 delete date;16 this.transport = transport;17 this.bAuth = false;18 this.iLoginReTryCount = 1;19 this.m_bData = false;20 this.m_szEmail = null;21 this.m_DomainHandler = null;22 //get streams23 this.ServerRequest = this.transport.openInputStream(0,0,-1);24 if (!this.ServerRequest) throw new Error("Error getting input stream.");25 this.ServerResponse = this.transport.openOutputStream(0,0,0);26 if (!this.ServerResponse) throw new Error("Error getting output stream.");27 //create stream watcher28 this.Pump = Components.classes["@mozilla.org/network/input-stream-pump;1"].29 createInstance(Components.interfaces.nsIInputStreamPump);30 this.Pump.init(this.ServerRequest, -1, -1, 0, 0, false);31 this.Pump.asyncRead(this,null);32 //connection made send ok33 var szOK = "220 WebMail ESMTP\r\n";34 this.m_SMTPLog.Write("nsSMTPConnectionHandler.js - "+ szOK);35 this.ServerResponse.write(szOK,szOK.length);36 this.m_SMTPLog.Write("nsSMTPConnectionHandler.js - END");37 }38 catch(e)39 {40 this.m_SMTPLog.DebugDump("nsSMTPConnectionManager.js: Constructor : Exception : "41 + e.name +42 ".\nError message: "43 + e.message +"\n"44 + e.lineNumber);45 }46}47SMTPconnectionHandler.prototype.bRunning = true;48SMTPconnectionHandler.prototype.iID = 0;49SMTPconnectionHandler.prototype.onDataAvailable = function(request, context, inputStream, offset, count)50{51 try52 {53 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - START "+ this.iID);54 var instream = Components.classes["@mozilla.org/scriptableinputstream;1"]55 .createInstance(Components.interfaces.nsIScriptableInputStream);56 instream.init(inputStream);57 var szStream = instream.read(count);58 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - STREAM "+this.iID+"\n"+ szStream);59 if (this.m_bData)60 {61 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - Data MSG Cont - START "+ this.iID);62 if (szStream.search(/^\.$/m)!=-1)63 {64 this.m_bData = false;65 var szEmail = (this.m_szEmail)? this.m_szEmail :"";66 szEmail += szStream;67 if (!this.m_DomainHandler.rawMSG(szEmail))68 throw new Error("msg upload failed");69 }70 else // there's more71 {72 (this.m_szEmail)? this.m_szEmail+= szStream : this.m_szEmail= szStream;73 }74 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - Data MSG Cont - END "+ this.iID);75 }76 else77 {78 //remove \n\r from request79 var aStream = szStream.split("\r\n"); //split string on return carrage line feed80 var aCommand = aStream[0].split(" "); //split string on space81 switch(aCommand[0].toLowerCase()) //first element is command82 {83 case "ehlo":84 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - ehlo - START "+ this.iID);85 var szResponse = "250-WebMail\r\n250 AUTH plain\r\n";86 this.ServerResponse.write(szResponse, szResponse.length);87 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - ehlo\n"+ szResponse);88 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - ehlo - END");89 break;90 case "auth":91 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - auth - START "+ this.iID);92 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - auth - "+ aCommand);93 switch(aCommand[1].toLowerCase())94 {95 case "plain":96 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - plain - START "+ this.iID);97 //decode base 64 encodec text98 var oBase64 = new base64();99 var aszDecoded = oBase64.decode(aCommand[2]).split("\0");100 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - decoded -" +aszDecoded);101 var aszUserName = aszDecoded[1].replace(/\s/,"").split("@"); 102 var szPassword = aszDecoded[2];103 if (this.getDomainHandler(aszUserName[0], aszUserName[1]))104 {105 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - got domain handler");106 this.m_DomainHandler.passWord = szPassword;107 this.m_DomainHandler.logIn();108 }109 else110 {111 var szTemp = "550 "+ aszUserName[1] + " is a unsupported domain\r\n"112 this.ServerResponse.write(szTemp,szTemp.length);113 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - unsuppoorted domain");114 }115 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - plain - END "+ this.iID);116 break;117 default:118 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - auth default - START "+ this.iID);119 var szErr = "504 Unrecognized authentication type\r\n";120 this.ServerResponse.write(szErr, szErr.length);121 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - auth default - END "+ this.iID);122 break;123 }124 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - auth - END "+ this.iID);125 break;126 //requires login127 case "mail":128 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - mail - START "+ this.iID);129 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - mail -\n"+ aCommand);130 var szTemp;131 try132 {133 szTemp = aCommand[1].match(/<(.*?)>/)[1];134 }135 catch(e)136 {137 szTemp = aCommand[2].match(/<(.*?)>/)[1];138 } 139 this.m_DomainHandler.from = szTemp;140 var szResponse = "250 OK\r\n";141 this.ServerResponse.write(szResponse, szResponse.length);142 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - mail - END "+ this.iID);143 break;144 case "rcpt":145 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - rcpt - START "+ this.iID);146 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - rcpt -\n"+ aCommand);147 var szTemp;148 try149 {150 szTemp = aCommand[1].match(/<(.*?)>/)[1];151 }152 catch(e)153 {154 szTemp = aCommand[2].match(/<(.*?)>/)[1];155 } 156 this.m_DomainHandler.to = szTemp;157 var szResponse = "250 OK\r\n";158 this.ServerResponse.write(szResponse, szResponse.length);159 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - rcpt - END "+ this.iID);160 break;161 case "data":162 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - data - START "+ this.iID);163 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - data -\n"+ aCommand);164 var szResponse = "354 OK\r\n";165 this.ServerResponse.write(szResponse, szResponse.length);166 this.m_bData = true;167 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - data - END "+ this.iID);168 break;169 case "rset":170 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - rset - START "+ this.iID);171 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - rset -\n"+ aCommand);172 var szResponse = "221 OK\r\n";173 this.ServerResponse.write(szResponse, szResponse.length);174 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - rset - END "+ this.iID);175 break;176 case "quit":177 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - quit - START "+ this.iID);178 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - quit -\n"+ aCommand);179 var szResponse = "221 OK\r\n";180 this.ServerResponse.write(szResponse, szResponse.length);181 this.ServerResponse.close();182 this.ServerRequest.close();183 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - quit - END "+ this.iID);184 break;185 //all unsupported commands186 default:187 var szErr = "502 negative vibes\r\n";188 this.ServerResponse.write(szErr, szErr.length);189 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - default "190 + szStream191 + szErr);192 break;193 }194 }195 this.m_SMTPLog.Write("SMTPconnectionHandler - onDataWritable - END "+ this.iID);196 }197 catch(e)198 {199 var szErr = "502 negative vibes\r\n";200 this.ServerResponse.write(szErr, szErr.length);201 this.m_SMTPLog.DebugDump("nsSMTPConnectionManager.js: onDataAvailable : Exception : "202 + e.name203 + ".\nError message: "204 + e.message+"\n"205 + e.lineNumber);206 }207}208SMTPconnectionHandler.prototype.onStopRequest = function(request, context, status)209{210 this.m_SMTPLog.Write("SMTPconnectionHandler - onStopRequest - START "+ this.iID);211 this.ServerResponse.close();212 this.ServerRequest.close();213 this.bRunning = false;214 delete this.m_DomainHandler;215 this.m_SMTPLog.Write("SMTPconnectionHandler - onStopRequest - END "+ this.iID);216}217SMTPconnectionHandler.prototype.onStartRequest = function(request, context)218{219 this.m_SMTPLog.Write("SMTPconnectionHandler - onStartRequest - START "+ this.iID);220 this.m_SMTPLog.Write("SMTPconnectionHandler - onStartRequest - END "+ this.iID);221}222SMTPconnectionHandler.prototype.getDomainHandler = function(szUserName, szDomain)223{224 try225 {226 this.m_SMTPLog.Write("POPconnectionHandler - getDomainHandler - START");227 this.m_SMTPLog.Write("POPconnectionHandler - getDomainHandler - "228 + szUserName229 + " "230 + szDomain);231 var DomainManager = Components.classes["@mozilla.org/DomainManager;1"];232 DomainManager = DomainManager.getService();233 DomainManager.QueryInterface(Components.interfaces.nsIDomainManager);234 var szContentID = new Object;235 236 //get domain handler contentid and interfaceid237 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - Domain "238 + szUserName239 + " "240 +szDomain);241 if (DomainManager.getDomainForProtocol(szDomain,"smtp",szContentID)!=1)242 {243 delete szContentID;244 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - DomainManger false");245 return false;246 }247 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - ContentID " + szContentID.value);248 //load domain handler249 if (typeof(Components.classes[szContentID.value]) == "undefined")250 {251 delete szContentID;252 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - DomainHandler does not exist");253 return false;254 }255 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - DomainHandler exist cID " +szContentID.value);256 this.m_DomainHandler = Components.classes[szContentID.value].createInstance();257 this.m_DomainHandler.QueryInterface(Components.interfaces.nsISMTPDomainHandler);258 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - DomainHandler created ");259 this.m_DomainHandler.ResponseStream = this.ServerResponse;260 this.m_DomainHandler.userName = szUserName + "@" + szDomain;261 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - " +this.m_DomainHandler.userName);262 delete szContentID;263 this.m_SMTPLog.Write("SMTPconnectionHandler - getDomainHandler - END");264 return true;265 }266 catch(e)267 {268 this.m_SMTPLog.DebugDump("SMTPconnectionHandler.js : getDomainHandler : Exception : "269 + e.name270 + ".\nError message: "271 + e.message+"\n"272 + e.lineNumber);273 return false;274 }...

Full Screen

Full Screen

google-user-validator.js

Source:google-user-validator.js Github

copy

Full Screen

...5 this._domain = domain;6 }7 // Public Methods8 validate(username, password, callback) {9 const smtpConnection = this._setupSMTPConnection();10 smtpConnection.connect(() => {11 const auth = {12 user: `${username}@${this._domain}`,13 pass: password14 };15 smtpConnection.login(auth, (error) => {16 if (error) {17 global.logger.error(`Failed to login to SMTP server with error: ${error}`);18 }19 callback(!!!error);20 smtpConnection.quit();21 });22 });23 }24 // Private Methods25 _setupSMTPConnection() {26 const smtpConnection = new SMTPConnection(this._options);27 smtpConnection.on('connect', () => {28 global.logger.info(`Connection established with SMTP server`);29 });30 smtpConnection.on('error', (error) => {31 global.logger.error(`SMTP server throw error: ${error}`);32 });33 smtpConnection.on('end', () => {34 global.logger.info(`Connection closed with SMTP server`);35 });36 return smtpConnection;37 }38}...

Full Screen

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var port = 2525;3var mbServer = mb.create({4});5mbServer.then(function () {6 var mbHelper = require('mountebank-helper');7 mb.post('/imposters', {8 {9 {10 is: {11 }12 }13 }14 }).then(function (response) {15 console.log(response.body);16 });17});

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var port = 2525;3var mbHost = 'localhost';4var mbPort = 2525;5var mbImposterPort = 2526;6var smtpConnection = mb.createSMTPConnection(port, function (request) {7 console.log('SMTP request received');8 request.reply(200, 'OK');9});10smtpConnection.then(function () {11 console.log('SMTP connection created');12 var imposter = mb.createImposter(mbPort, mbImposterPort, 'smtp', function (request) {13 console.log('Imposter request received');14 request.reply(200, 'OK');15 });16 imposter.then(function () {17 console.log('Imposter created');18 var client = mb.createSMTPClient(mbImposterUrl);19 client.sendMail({

Full Screen

Using AI Code Generation

copy

Full Screen

1var mb = require('mountebank');2var smtpServer = mb.create({3});4smtpServer.post('/imposters', {5 stubs: [{6 responses: [{7 is: {8 headers: {9 },10 }11 }]12 }]13});14smtpServer.start();15var SMTPConnection = require('smtp-connection');16var connection = new SMTPConnection({17});18connection.connect(function() {19 connection.send({

Full Screen

Automation Testing Tutorials

Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.

LambdaTest Learning Hubs:

YouTube

You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.

Run mountebank automation tests on LambdaTest cloud grid

Perform automation testing on 3000+ real desktop and mobile devices online.

Try LambdaTest Now !!

Get 100 minutes of automation test minutes FREE!!

Next-Gen App & Browser Testing Cloud

Was this article helpful?

Helpful

NotHelpful