Answer by rickett (14292) | Apr 16, 2015 at 02:59 PM
The jsp file is the key in this solution/approach. In this case the native java classes in the WAS JVM are used. The html form is included in this technote in case you are more comfortable with the html forms, but you could code the input fields in the jsp itself without requiring the html form.
DISCLAIMER OF WARRANTIES:
This sample code is provided to you solely for the purpose of assisting
you in the development of your applications.
The code is provided "AS IS", without warranty of any kind. IBM shall
not be liable for any damages arising out of your use of the sample code,
even if they have been advised of the possibility of such damages.
Create an html component titled "MailHTMLForm" with these contents:
<HTML>
<BODY>
<FORM METHOD=POST ACTION="wcmmail.jsp">
Enter your name: <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
Enter your email address: <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
Enter comments: <INPUT TYPE=TEXT NAME=content SIZE=50><BR>
<P>
<INPUT TYPE=SUBMIT VALUE="Send email">
</FORM>
</BODY>
</HTML>
Create a jsp titled "wcmmail.jsp" and place in directory \installedApps\index\wcm.ear\ilwwcm.war\jsp\html
Here are the jsp contents:
//START JSP CONTENTS
<%@ page import="java.io.*, java.net.*, javax.mail.*, javax.mail.internet.*, java.util.Properties" %>
<HTML>
<BODY>
<%
String email = request.getParameter( "email" );
String content = request.getParameter( "content" );
%>
<% if ( email == null || email.equals( "" )) { %>
Please press back button and enter an email address.
<% } else {
Properties emailProperties = new Properties();
emailProperties.setProperty("mail.transport.protocol", "smtp");
emailProperties.setProperty("mail.host", "myMailServer.mycompany.com");
emailProperties.setProperty("mail.from", email);
Session mailSession = Session.getDefaultInstance(emailProperties, null);
Transport transport = mailSession.getTransport();
MimeMessage message = new MimeMessage(mailSession);
message.setSubject("WCM Javamail");
message.setContent( content , "text/html");
message.addRecipient(Message.RecipientType.TO, new InternetAddress(email));
message.setFrom(new InternetAddress(email));
transport.connect();
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
transport.close();
%>
<br/>
This name will be registered <%= request.getParameter( "username" ) %>
<br/>
Your email has been sent to <%= email %> with this timestamp <%= new java.util.Date() %>
<% } %>
</BODY>
</HTML>
//END JSP CONTENTS
Modify the jsp for your email server/environment. Create a WCM JSP component titled "SendMailJSPComponent" with this path: /jsp/html/wcmmail.jsp
Preview the html component MailHTMLForm using a url similar to this:
http://myPortalServer.myCompany.com:10040/wps/wcm/jsp/html/emailform.htm
Fill in the fields, For example, enter this in the "content" field:
Press "Send email" > email arrives at destination in HTML encoded format.
Is there a good example of WCM JSP Component usage? 3 Answers
How to send an email using the WCM Mail Module ? 1 Answer
Why we not able to add paramaters directly to URL after upgrading to Portal 8.5? 1 Answer
Can I create a link to a Portal page in WCM content that checks user access? 1 Answer
How can I send an email from a contact form in Web Content Manager? 1 Answer