cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can change your system assigned username to something more personal in your community settings. X

Dynamically create a thing of Base type "MailServer" with user given SMTP details

SKannapiran
11-Garnet

Dynamically create a thing of Base type "MailServer" with user given SMTP details

I have a Mashup having SMTP configuration fields which will take as a input from the user. On saving this configuration I need to dynamically create a thing of Base type "MailServer" with the user given configuration through ThingWorx service. Kindly assist with the steps. 

Below are the configuration fields in the Mashup

SMTP Server

SMTP Port

User Name

Password

SSL 

 

 

1 ACCEPTED SOLUTION

Accepted Solutions

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.

  1. 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.
  2. 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

View solution in original post

4 REPLIES 4

Hi @SKannapiran ,

 

Kindly find the below code for creating & configuring mail server thing with SMTP setup. It will hepls you.

 

// Declare or Create the unique name for entity(thing) creation
let emailThingName = "PTCCommunity.Email.TH";

// Validate the inputs based on the requirement
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"
		};
	}
	if (IP_Password == null || IP_Password == undefined || IP_Password == "" || IP_Password == "undefined") {
		throw {
			"message": "Please Enter Value for Password"
		};
	}
	try {
		// Create a thing with unique entity name based on Thing Templete of MailServer
		let params = {
			name: emailThingName /* STRING */ ,
			description: undefined /* STRING */ ,
			thingTemplateName: "MailServer" /* THINGTEMPLATENAME */ ,
			projectName: "PTCDefaultProject" /* PROJECTNAME */ ,
			tags: "PTC:DEMO" /* TAGS */
		};
		Resources["EntityServices"].CreateThing(params);
        
		// Enable the new thing for updating the configuration
		Things[emailThingName].EnableThing();

		// Get Configuration infotable from new email server thing
		let connectionInfoConfig = Things[emailThingName].GetConfigurationTable({
			tableName: "ConnectionInfo" /* STRING */
		});

		// Encrypt the Password from String Input
		let params1 = {
			data: IP_Password /* STRING */
		};
		let encryptPassword = Resources["EncryptionServices"].EncryptPropertyValue(params1);

		// 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 configuration table in newly created mail thing
		Things[emailThingName].SetConfigurationTable({
			configurationTable: connectionInfoConfig /* INFOTABLE */ ,
			persistent: true /* BOOLEAN {"defaultValue":true} */ ,
			tableName: "ConnectionInfo" /* STRING */
		});
        
		// Finally restart the newly created thing
		Things[emailThingName].RestartThing();

		result = "Email thing was created and configured successfully!";
	} catch (e) {
		throw {
			"message": "Email thing was already craeted / Issue in configuration!"
		};
	}
} catch (e) {
	result = e.message;
}

 

  If any queries kindly let me know.

 

Thanks & Regards,

Arun C

Hi @Arun_C ,

 

Thanks a lot for sharing your code, the above code works fine.

In addition, there is a need to validate user-fed SMTP configurations before creating a dynamic mail server.

This validation has to be performed within the ThingWorx script itself. 

The dynamic mail server thing can be created upon successful validation.

Could you please share code snippet for the same. Thanks in advance.  

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.

  1. 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.
  2. 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

Thanks a ton @Arun_C . This code is working perfectly.

Top Tags