SmtpClientMk(config)
Creates an SMTP client, connects and logs in: new docly.SmtpClientMk(config). Use it to send several messages over one connection. Call disconnect() when you are done.
Methods:
send(message)— sends a message and returns { Success: true, MessageId }. Throws if sending fails.disconnect()— closes the connection. Returns true, or false when it was already closed.
Unlike smtp.send, the SendTo/SendFrom defaults in the configuration are not used: each message must have its own SendTo and SendFrom.
Parameters
configobject | stringrequiredSMTP configuration, the same as for smtp.send: SmtpServer, SmtpPort and SmtpSsl are required; SmtpUser and SmtpPass log in. SmtpSsl means STARTTLS. Set SmtpOauth2: true to log in with an OAuth2 access token in SmtpPass. Or pass the path to an "SMTP configuration" document, such as "#/My Smtp".
Returns
The client object with the methods above. Throws if the configuration is incomplete or the connection or login fails. The message object is the same as for smtp.send: SendTo, SendToCc, SendToBcc, SendFrom, ReplyTo, Subject, Message, IsHtml, Attachments, Headers and MessageId.
Example
let config = docly.getJson("#/Config").Smtp; // keep credentials out of the code
let smtp = new docly.SmtpClientMk(config);
try {
for (let to of ["a@example.com", "b@example.com"]) {
smtp.send({
"SendTo" : to,
"SendFrom" : "no-reply@example.com",
"Subject" : "Hello",
"Message" : "Hello there",
"IsHtml" : false
});
}
} finally {
smtp.disconnect();
}