
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
- Log into WCS Admin Console

- Select Site

- Select Configuration > Transports

- Select Email, and click the Change Status button to make it Active.

- For testing, enter the following values:
Host: localhost
Port: 25
Protocol: smtp
Send Partial: false

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.
- Download the latest version of FakeSMTP
- Unzip the contents to c:fakesmtp
- Double click the Jar file to run it (or run “java -jar fakeSMTP.jar”)
- The port should be 25, the same port you defined in WCS Admin Console

- Start the server

Create the New Notification View
- Create a new JSP. For this example, we will name it MyNewEmailNotify.jsp
- 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>
- Add access control polices for the new view.
<Policies> <Action Name="MyNewEmailNotifyView" CommandName="MyNewEmailNotifyView" /> <ActionGroup Name="AllSiteUsersViews" OwnerID="RootOrganization"> <ActionGroupAction Name="MyNewEmailNotifyView"/> </ActionGroup> </Policies>
- Run ACPLoad
- 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');
- 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.
Leave a Reply