Configure and Test Emails With SendMsgCmd and FakeSMTP

Configure and Test Emails With SendMsgCmd and FakeSMTP

To send emails in WebSphere Commerce, you first need to define and activate the email transport. Then the server defined needs to be configured and started to actually handle the incoming requests. Once that is set up, create a new JSP view, define it in Struts, and set the access control policies. Finally, the view can be used with the SendMsgCmd to send emails.

Configure WCS Email Transport

  1. Log into WCS Admin Console
    wcs admin console
  2. Select Site
    wcs admin console site store selection
  3. Select Configuration > Transports
    wcs admin console configuration transports
  4. Select Email, and click the Change Status button to make it Active.
    wcs admin console email transport
  5. For testing, enter the following values:
    Host: localhost
    Port: 25
    Protocol: smtp
    Send Partial: false
    wcs admin console email transport configuration parameters

Configure the Local Server

Above, we told WCS to use localhost and port 25 for the SMTP server, however there is no server currently running. To test with, we will be using FakeSMTP.

  1. Download the latest version of FakeSMTP
  2. Unzip the contents to c:fakesmtp
  3. Double click the Jar file to run it (or run “java -jar fakeSMTP.jar”)
  4. The port should be 25, the same port you defined in WCS Admin Console
    fake smtp server stopped
  5. Start the server
    fake smtp server started

Create the New Notification View

  1. Create a new JSP. For this example, we will name it MyNewEmailNotify.jsp
  2. Add the Struts entry
    <action path="/MyNewEmailNotifyView" type="com.ibm.commerce.struts.BaseAction">
    	<set-property property="authenticate" value="10001:1"/>
    	<set-property property="https" value="10001:1"/>
    </action>
    
    <forward name="MyNewEmailNotifyView/10001/" path="/thePathToTheFile/MyNewEmailNotify.jsp" className="com.ibm.commerce.struts.ECActionForward">
    	<set-property property="implClassName" value="com.ibm.commerce.messaging.viewcommands.MessagingViewCommandImpl"/>
    	<set-property property="interfaceName" value="com.ibm.commerce.messaging.viewcommands.MessagingViewCommand"/>
    </forward>
    
  3. Add access control polices for the new view.
    <Policies>
    	<Action Name="MyNewEmailNotifyView" CommandName="MyNewEmailNotifyView" />
    	<ActionGroup Name="AllSiteUsersViews" OwnerID="RootOrganization">
    		<ActionGroupAction Name="MyNewEmailNotifyView"/>
    	</ActionGroup>
    </Policies>
    
  4. Run ACPLoad
  5. Add a row into the MsgType database table. Be sure to switch the name and viewname to match the JSP and Struts entry.
    INSERT INTO MSGTYPES(MSGTYPE_ID, MSGTDIR, NAME, VIEWNAME, DESCRIPTION) VALUES((SELECT (MAX(MSGTYPE_ID)+1) FROM MSGTYPES) , 1, 'MyNewEmailNotify', 'MyNewEmailNotifyView', 'Email example used on JustinRPennington.com');
    
  6. Add a row into the Profile database table.
    INSERT INTO PROFILE(PROFILE_ID, STORE_ID, DEVICEFMT_ID, TRANSPORT_ID, MSGTYPE_ID, USERSVIEW, LOWPRIORITY, HIGHPRIORITY, ARCHIVEMSG) VALUES((SELECT (MAX(PROFILE_ID)+1) FROM PROFILE), 10001, (SELECT DEVICEFMT_ID FROM DEVICEFMT WHERE DEVICETYPE_ID='E-mail'), (SELECT TRANSPORT_ID FROM TRANSPORT WHERE name='EMailSender'), (SELECT MSGTYPE_ID from MSGTYPES WHERE name='MyNewEmailNotify'), 'N',0,0,0);
    

Code to Send the New Email with SendMsgCmd

SendMsgCmd sendMsgCmd = null;
try {
	sendMsgCmd = (SendMsgCmd)CommandFactory.createCommand(SendMsgCmd.NAME, getCommandContext().getStoreId());
	sendMsgCmd.setCommandContext(getCommandContext());
	sendMsgCmd.setStoreID(getCommandContext().getStoreId());
	//Value from the MsgType table
	sendMsgCmd.setMsgType("MyNewEmailNotify");
	sendMsgCmd.setConfigData("subject", "Whatever you want the subject to be, should probably come from a properties file");
	sendMsgCmd.setConfigData("recipient", "user@somewhere.com");
	sendMsgCmd.setConfigData("contentType", "text/html");

	//Set parameters that are used in the JSP
	final TypedProperty property = new TypedProperty();
	property.put("firstName","Justin");
	property.put("lastName","Pennington");
	property.put("website","www.justinrpennington.com");
	// Struts view
	sendMsgCmd.compose("MyNewEmailNotifyView", getCommandContext(), property);
	sendMsgCmd.sendImmediate();
	sendMsgCmd.execute();
} catch (Exception exception) {
	LOGGER.SEVERE("Can't send the email!");
}

After calling the command, you will see the email appear in FakeSMTP in the “Last message” table. Additionally, the email will be saved as an .eml file in the received-emails directory.


Comments

Leave a Reply

Your email address will not be published. Required fields are marked *