I'm attempting to connect to SMTP servers through proxies to validate emails.
I've looked into http-proxy-agent, but as I understand, proxies cannot be used with a TCP connection?
My current connection setup:
const client = net.createConnection(25, mxServer, () => {
client.write("HELO @mail.com\r\n");
client.write("MAIL FROM:<email_address>\r\n");
client.write(`RCPT TO:<${email}>\r\n`);
});
You should use SOCKS protocol for SMTP connections rather than HTTP proxies since SOCKS work at TCP level. The following is a solution using Node.js with the SOCKS package:
const SocksClient = require('socks').SocksClient;
const net = require('net');
async function validateEmailThroughProxy(email, mxServer) {
const socksOptions = {
proxy: {
host: 'proxy.example.com', // Your SOCKS proxy host
port: 1080, // SOCKS proxy port
type: 5 // SOCKS5
},
command: 'connect',
destination: {
host: mxServer,
port: 25
}
};
try {
const info = await SocksClient.createConnection(socksOptions);
const socket = info.socket;
// Set encoding for the responses
socket.setEncoding('utf8');
// Handle responses
socket.on('data', (data) => {
console.log('Received:', data);
});
// Send SMTP commands
socket.write("HELO mail.com\r\n");
socket.write(`MAIL FROM:<sender@example.com>\r\n`);
socket.write(`RCPT TO:<${email}>\r\n`);
// Don't forget to handle closing
socket.on('close', () => {
console.log('Connection closed');
});
} catch (err) {
console.error('Error:', err);
}
}
Note the following:
Try the above and see if it solves the problem :-)