25

I have a big problem with setting up the nodemailer on my node.js server. Tried everthing I found on the internet but nothing works. The only thing that was easy to setup was the gmail service. but unfortunately I cannot use that one.

With secure set to true, i get an ssl error with the reason wrong version code.

[Error: 22468:error:1408F10B:SSL routines:ssl3_get_record:wrong version number:c:\ws\deps\openssl\openssl\ssl\record\ssl3_record.c:332:
] {
  library: 'SSL routines',
  function: 'ssl3_get_record',
  reason: 'wrong version number',
  code: 'ESOCKET',
  command: 'CONN'
}

But when I try to set secure to false, then I get an invalid greeting error.

Error: Invalid greeting. response=* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2016 Double Precision, Inc.  See COPYING for distribution information.: * OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2016 Double Precision, Inc.  See COPYING for distribution information.
    at SMTPConnection._actionGreeting (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:1189:27)
    at SMTPConnection._processResponse (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:932:20)
    at SMTPConnection._onData (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:739:14)
    at Socket.SMTPConnection._onSocketData (C:\Users\Motiondata\Documents\repos\rmn_app\server\rmn_server\node_modules\nodemailer\lib\smtp-connection\index.js:189:44)
    at Socket.emit (events.js:315:20)
    at addChunk (_stream_readable.js:309:12)
    at readableAddChunk (_stream_readable.js:284:9)
    at Socket.Readable.push (_stream_readable.js:223:10)
    at TCP.onStreamRead (internal/stream_base_commons.js:188:23) {
  code: 'EPROTOCOL',
  response: '* OK [CAPABILITY IMAP4rev1 UIDPLUS CHILDREN NAMESPACE THREAD=ORDEREDSUBJECT THREAD=REFERENCES SORT QUOTA ACL ACL2=UNION STARTTLS] Courier-IMAP ready. Copyright 1998-2016 Double Precision, Inc.  See COPYING for distribution information.',
  command: 'CONN'
}

My code is the following:

const transporter = nodemailer.createTransport({
  host: process.env.MAIL_HOST, // mx.example.com
  port: process.env.MAIL_PORT, // 143
  secure: true,
  auth: {
    user: process.env.MAIL_ADDRESS,
    pass: process.env.MAIL_PWD
  }
})

I checked the credentials a thousand time, they are definetly not the problem.

Hope anyone can help me. Thanks in advance.

Simon Schuster
  • 363
  • 1
  • 3
  • 5
  • I think it has to do with your provider (firebase, outlook or whatever), check what host & port your provider supports & see if there are some extra configuration that you need to do for this. – MiKr13 Feb 22 '21 at 14:11
  • But when I use the exact same host and port in outlook, everthing work fine. That's really strange to me. – Simon Schuster Feb 22 '21 at 14:25
  • Using the port 2525 worked for me. All the other ports were throwing this wrong version error. – Sapinder Singh Jun 18 '22 at 09:21

6 Answers6

48

Refering to this issue mentioned here: https://github.com/andris9/Nodemailer/issues/165

See if this helps, adding the tls.ciphers option to use SSLv3:

const transport = nodemailer.createTransport({
    host: process.env.MAIL_HOST, // mx.example.com
    port: process.env.MAIL_PORT, // 143
    secureConnection: false, // TLS requires secureConnection to be false
    auth: {
        user: process.env.MAIL_ADDRESS,
        pass: process.env.MAIL_PWD
    },
    tls: {
        ciphers:'SSLv3'
    }
});

For Outlook365, this should work:

service: "Outlook365",
auth: {
   user: '[YOUR_O365_EMAIL]',
   pass: '[YOUR_O365_PASSWORD]'
}, 

Refer here: https://stackoverflow.com/a/37725123/9360885

If you're using HotMail, then remove host and port, and just add service: "hotmail".

Rafael Tavares
  • 5,678
  • 4
  • 32
  • 48
MiKr13
  • 1,297
  • 13
  • 20
  • Just adding the secureConnection and tls doesn't work either. And the string 'SMTP' cannot be used in newer versions of nodemailer i guess, because its giving me an error if I put it there. – Simon Schuster Feb 22 '21 at 14:49
  • You're using outlook365? – MiKr13 Feb 22 '21 at 14:57
  • No it's actually a local mail server. But its not mine, it's the one a a company I know. I have just tested to add the mail address to outlook in order to confirm the credentials. – Simon Schuster Feb 22 '21 at 15:00
  • Okay, I've added one more link to stackoverflow answer, see if this helps: https://stackoverflow.com/a/62267653/9360885 – MiKr13 Feb 22 '21 at 15:04
  • For local server you won't need secure true unless you have secure certificate for that local server – MiKr13 Feb 22 '21 at 15:05
  • Tried that too actually. But when I set secure to false, or just dont add it to the config, I get the error thats shown in my first post. The one that says invalid greeting. – Simon Schuster Feb 22 '21 at 15:11
  • Try this link for secure false: https://stackoverflow.com/a/37725123/9360885 – MiKr13 Feb 22 '21 at 15:16
  • Thank u very much. It's in the junk mail folder but it works now. Do u know, how to get it out of there? That would be awesome too. – Simon Schuster Feb 22 '21 at 15:21
  • That you'll have to mark as not spam only as your service provider must be using filtering system based on data collected from various people, or send your email with priority: high - ```const message = { ..., headers: { 'priority': 'high' }, };``` – MiKr13 Feb 22 '21 at 15:34
  • And where do I set that? I cannot just add a header to my mailoptions object i pass to transporter.sendMail() method or can I? – Simon Schuster Feb 22 '21 at 15:46
  • Yeah you can, you must be having something like this already `const message = { from: "sender@server.com", to: "receiver@sender.com", subject: "Message title", text: "Plaintext version of the message", html: "

    HTML version of the message

    ", headers: { 'priority': 'high' } };` refer here: https://nodemailer.com/message/ Notice the headers key
    – MiKr13 Feb 22 '21 at 15:50
  • Ok with the header set, sadly the mail is also in the spam folder. – Simon Schuster Feb 22 '21 at 15:56
  • Yeah, sadly it doesn't actually guarantee it. Sometimes important emails can also go into the spam folder. I don't have much knowledge about this spam/junk email stuff. Sorry, I guess I can't be of help more on that :( – MiKr13 Feb 22 '21 at 16:02
11

use

secure: false, // true for 465, false for other ports

Example from nodemailer docs:

let transporter = nodemailer.createTransport({
    host: 'smtp.ethereal.email',
    port: 587,
    secure: false, // true for 465, false for other ports
    auth: {
        user: account.user, // generated ethereal user
        pass: account.pass  // generated ethereal password
    }
});

source: nodemailer examples

Victor Gorban
  • 1,159
  • 16
  • 30
2

I used port 465 with Gmail and that worked. Got the "wrong version number" error with 587. Example below is for a GCP service account in a Cloud Function.

  const transporter = nodemailer.createTransport({
    host: "smtp.gmail.com",
    port: 465,
    secure: true,
    auth: {
      type: "OAuth2",
      user: process.env.GMAIL_ADDRESS,
      serviceClient: process.env.CLIENT_ID,
      privateKey: process.env.PRIVATE_KEY.replace(/\\n/g, "\n"),
    },
  });
smoore4
  • 4,520
  • 3
  • 36
  • 55
2

change smtp port 587 to 465

smtp:{
    name:"noreply@gmail.com",
    host:process.env.SMTP_HOST, 
    secure: true,//true
    port: 465,//465
    auth: {
      user: process.env.SMTP_USER, 
      pass: process.env.SMTP_PASSWORD, 
    },  // here it goes
  },
1

I also ran in this problem. But I managed to fix it. It's quite simple to solve.

Solution: port: process.env.MAIL_PORT * 1

This will change your process.env.MAIL_PORT type from a string to integer.

Why there was an error?

NodeMail require a port with a type of integer, but you were passing a string. So that caused an error.

0

If you are still receiving the issue, fix by inputting this into your browser manually while logged in.

https://accounts.google.com/DisplayUnlockCaptcha

bad_coder
  • 11,289
  • 20
  • 44
  • 72
  • 2
    While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/29855477) – callmenikk Sep 18 '21 at 12:11