Can't send mails using deno (SmtpClient module)

Viewed 425

i just started learning deno and tried to send mails with it.

import { SmtpClient } from "./deps.js";

await client.connectTLS({
  host: "smtp.163.com",
  port: 465,
  username: "my email",
  password: "my password",
});

await client.send({
  from: "mailaddress@163.com",
  to: "t32n5nr@prowerl.com",
  subject: "Mail Title",
  content: "Mail Content,maybe HTML",
});

I did everything as said in SmtpClient docs

But i get an error

error: Uncaught ConnectionRefused: No connection could be made because the target machine actively refused it. (os error 10061)
    at unwrapResponse ($deno$/ops/dispatch_json.ts:43:11)
    at Object.sendAsync ($deno$/ops/dispatch_json.ts:98:10)
    at async Object.connectTls ($deno$/tls.ts:20:15)
    at async SmtpClient.connectTLS (https://deno.land/x/smtp/smtp.ts:36:18)
    at async file:///C:/.../controller.js:28:1
1 Answers

The "connection refused" message almost always resolve to one these two possibilities:

1. The server or service is unstable or unavailable.

In this situation there might be a few things going on the server:

Options 1.1 - Incorrect port / listener is down

The port you are using is correct but the listener (process on the servers) is down, meaning that you are connecting to the correct port, but there's no process to listen for your requests.

Possible solution is to try running telnet <address> <port> and check if the service is up. Example: telnet google.com 443.

Option 1.2 - Threshold limit

The port and the service are up, but you've reached the threshold that limits the configured TCP connectivity.

That might occur due to high traffic (peaks) to the endpoint. That is not something you can solve yourself. TCP listeners might reject the caller connection if the traffic is too high.

One way of testing these is to implement a load testing script that tests the connectivity over time. If you prove that the server is limiting the requests you can then report and ask them to increase the load capabilities (allow for a higher capacity for simultaneous requests).

2. The client cannot communicate with the server

Option 2.1 - Proxy

If you are connection from an enterprise network and there is a proxy in between, that might be why you are having such difficulties.

Solution: run your code from home or an external network to prove that you are able to connect from outside the corporate network.

Option 2.2 - Firewall

Just like the proxy, you're running your code behind a firewall that is either blocking or interfering with your communication with the external network.


I was able to run your code and connect to Google using my personal credentials. I had to perform a slight change due to a problem to the Deno library due to type definitions but it worked fine.

I strongly suspect that your problem is related to the infrastructure (local or remote), not to the Deno runtime or library.

import { SmtpClient } from "https://deno.land/x/smtp/mod.ts";
import { ConnectConfigWithAuthentication } from "https://raw.githubusercontent.com/manyuanrong/deno-smtp/master/config.ts";

const client = new SmtpClient();

const params = <ConnectConfigWithAuthentication>{
  hostname: "smtp.google.com",
  port: 465,
  username: "<google mail>",
  password: "<password>"
}

await client.connectTLS(params);

await client.send({
  from: "<from_email>", // Your Email address
  to: "<to_email>", // Email address of the destination
  subject: "Mail Title",
  content: "Mail Content,maybe HTML",
});

await client.close();

I tried to create a user on the smtp.163.com website but I couldn't understand the language, if you give me test credentials I try myself.

Related