Hi @SKannapiran ,
For validating configuration of SMTP user feed, need to send a demo mail to some user then only we can find. In 2 ways we achieve it.
- Need to set the configuration 1st in dummy mail thing and try to send demo mail with any user. If mail not sent means we don't need to create the new thing & can break the code by showing valid messages.
- After creating the new thing we can try to send mail to user. If mail not sent means in catch we can able to delete the entity and we can show validation fail message to user.
Kindly select the case based on your requirement. Kindly find the 2nd way updated code in below.
try {
if (IP_Server == null || IP_Server == undefined || IP_Server == "" || IP_Server == "undefined") {
throw {
"message": "Please Enter Value for Server"
};
}
if (IP_PortNumber == null || IP_PortNumber == undefined || IP_PortNumber == "" || IP_PortNumber == "undefined") {
throw {
"message": "Please Enter Value for Port Number"
};
}
if (IP_Email == null || IP_Email == undefined || IP_Email == "" || IP_Email == "undefined") {
throw {
"message": "Please Enter Value for Email"
};
} else {
var email = '';
function isValidEmail(email) {
var emailRegex = /^[A-Za-z0-9._%-]+@[A-Za-z0-9.-]+\.[A-Za-z]{2,4}$/;
return emailRegex.test(email);
}
if (isValidEmail(IP_Email)) {
IP_Email = IP_Email;
} else {
throw {
"message": "Please Enter Vaild Email Address"
};
}
}
if (IP_Password == null || IP_Password == undefined || IP_Password == "" || IP_Password == "undefined") {
throw {
"message": "Please Enter Value for Password"
};
}
let emailThingName = "PTCCommunity.Email.TH";
// Create a thing with Unique entity name based on Templete of MailServer
try {
let params2 = {
name: emailThingName /* STRING */ ,
description: undefined /* STRING */ ,
thingTemplateName: "MailServer" /* THINGTEMPLATENAME */ ,
projectName: "PTCDefaultProject" /* PROJECTNAME */ ,
tags: "PTC:DEMO" /* TAGS */
};
// no return
Resources["EntityServices"].CreateThing(params2);
// Enable the new thing for updating the configuration
Things[emailThingName].EnableThing();
// Encrypt the Password from String Input
let params1 = {
data: IP_Password /* STRING */
};
let encryptPassword = Resources["EncryptionServices"].EncryptPropertyValue(params1);
// Get Configuration infotable from new email server thing
let connectionInfoConfig = Things[emailThingName].GetConfigurationTable({
tableName: "ConnectionInfo" /* STRING */
});
// Update the configuration info based on user input
connectionInfoConfig.smtpServer = IP_Server; // SMTP Server - STRING
connectionInfoConfig.smtpPort = IP_PortNumber; // SMTP Server Port - NUMBER
connectionInfoConfig.accountId = IP_Email; // Mail Account User - STRING
connectionInfoConfig.accountPassword = encryptPassword; // Mail Account Password - STRING
connectionInfoConfig.useSSL = IP_SSL; // SSL - BOOLEAN
connectionInfoConfig.useTLS = IP_TLS; // TLS - BOOLEAN
// Update the created mail thing configuration table
Things[emailThingName].SetConfigurationTable({
configurationTable: connectionInfoConfig /* INFOTABLE */ ,
persistent: true /* BOOLEAN {"defaultValue":true} */ ,
tableName: "ConnectionInfo" /* STRING */
});
// Finally restart the newly created thing
Things[emailThingName].RestartThing();
} catch (e) {
throw {
"message": "Entity is already Created!!"
};
}
try {
// Send demo mail for the validation
Things[emailThingName].SendMessageWithNoReplyTo({
subject: 'Demo Mail Setup' /* STRING */ ,
to: "demomailuser@gmail.com" /* STRING */ ,
body: '<p>Hi,</p><p>This mail is triggerd for SMTP Configuration Validation. Kindly ingore this mail.</p><p>Thanks.</p>' /* HTML */
});
result = "Email thing was created and configured successfully!";
} catch (e) {
// If demo email is fail delete the entity
let params = {
name: emailThingName /* THINGNAME */
};
Resources["EntityServices"].DeleteThing(params);
// Validation Message
throw {
"message": "Issue in SMTP configuration Kindly Validate it!"
};
}
} catch (e) {
result = e.message;
}
Thanks & Regards,
Arun C