node. js Send mail email

Source: Internet
Author: User

Usually when we do the node project, maybe we will run into a simple email feedback, then we'll talk about it today, the various pits encountered.

To do this in general, we may need node third-party dependency modules to achieve what we want to achieve.

Here I recommend two modules: Https://github.com/pingfanren/Nodemailer

NPM Install Nodemailer   //This module is good, GitHub on the star is more, also often have maintenance, but pits are more

Another, Https://github.com/eleith/emailjs.

NPM Install Emailjs  

Here I use the Nodemailer module, after all, with more people, follow the mainstream it

It is characterized by:

    • Using Unicode encoding
    • Support for Windows systems, no need to install dependencies
    • Supports plain text and HTML formatting
    • Support for sending attachments (including large attachments)
    • Embed a picture in HTML
    • Support SSL/STARTTLS Security protocol
    • Different transfer methods can be used in the form of built-in or external plug-ins can be used
    • Provides custom plug-in support (such as adding dkim signatures, using markdown instead of HTML, etc.)
    • Support for XOAUTH2 login verification (and token feedback about updates)
Installation use
NPM Install Nodemailer--save

  To send mail using the built-in transport , you can view the support list: Https://github.com/andris9/nodemailer-wellknown#supported-services

varNodemailer = require ('Nodemailer');varTransporter =Nodemailer.createtransport ({//Https://github.com/andris9/nodemailer-wellknown#supported-services Support listService'QQ', Port:465,//SMTP PortSecureconnection:true,//using SSLAuth: {User:'76806[email protected]',        //This password is not the QQ password, is the SMTP password you setPass'*****'    }});//nb! No need to recreate the transporter object. You can use//The same Transporter object for all e-mails//Setup e-mail data with Unicode symbolsvarMailoptions = {     from:'[email protected]',//Sender AddressTo:'[email protected]',//List of received itemsSubject'Hello Sir',//title//both text and HTML support only oneText'Hello world?',//titleHtml:'<b>hello World?</b>' //HTML content};//send mail with defined transport objecttransporter.sendmail (mailoptions, function (error, info) {if(Error) {returnConsole.log (Error); } console.log ('Message Sent:'+info.response);});

We seldom have operations after sending a message, but there are very few cases where it is necessary to process some special information after success, when the info object will be able to play the heat. The Info object contains attributes such as MessageID, envelop, accepted, and response, which I don't see in detail.

Using a different transport plug-in Https://github.com/andris9/nodemailer-smtp-transport
NPM Install Nodemailer-smtp-transport  --save

The other code is similar, the difference is only created on the transport, so here I write part of the code:

varNodemailer = require ('Nodemailer');varSmtptransport = require ('Nodemailer-smtp-transport');//Open an SMTP connection poolvarTransport =Nodemailer.createtransport (Smtptransport ({host:"smtp.qq.com",//HostSecuretrue,//using SSLSecureconnection:true,//using SSLPort465,//SMTP PortAuth: {User:"[email protected]",//Account NumberPass"gaolu-15271922313" //Password  }})); //set up message contentvarMailoptions = {   from:"768065158<[email protected]>",//Sender AddressTo:"[email protected]",//List of received itemsSubject"Hello World",//titleText"Hello", HTML:"<b>thanks A for visiting!</b> world, Hello! " //HTML content} //Send mailtransport.sendmail (mailoptions, function (Error, response) {if(Error) {Console.error (error); } Else{console.log (response); } transport.close (); //if it doesn't, close the connection pool});

Here are some fields for sending mail:

    • From Sender Mailbox
    • Sender Sender Area Display information
    • To recipient mailbox
    • CC CC Email
    • BCC Secret Delivery Mailbox
    • Subject Mailbox Theme
    • Attachments Attachment Contents
    • watchhtml HTML version specified by Apple Watch
    • Text Textual information
    • HTML HTML content
    • Headers Additional header information
    • Encoding encoding format

The message content uses the UTF-8 format, and the attachment uses a binary stream.

Attachment

The Attachment object contains the following properties:

    • FileName Attachment Name
    • Content contents
    • Encoding encoding format
    • Path file paths
    • ContentType Attachment Content Type
Common errors

  1. The service is not set by the account

454 authentication failed, please open SMTP flag first! ]  'autherror',  '454 authentication failed, Please open SMTP flag first! ' ,   ' Auth ' }

  Solution:
Opening services, account, settings, QQ mailbox

  2. The sender account differs from the authentication account

 from 501  from  as  'sendererror'501 mail from address must is same as Authorization user'mail' }

  3. Login authentication failed, may be due to Smpt independent password error caused me in the QQ set up the time I met

535

  Solution:

QQ mailbox in the test SMTP mail server, one, in the QQ mailbox, settings, account settings. Open the next SMTP. Two, set up a separate password. Three, when configuring the SMTP server password, be sure to fill in the individual password you set. Do not use the mailbox login password. Otherwise, you will be prompted 535 Authentication failed error.

  

Data reference:

QQ Mailbox Smpt Set up a separate password: http://wap.yzmg.com/app/103426.html

Nodejs sending mail using Nodemailer

Nodejs Outgoing mail Component Nodemailer

Send mail with Nodemailer component

Some problems found during the use of Nodemailer

Nodeemail Wellkown

node. js Send mail email

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.