The following tutorial, i’ll create a spring portlet with sending email and attachment. Also, include using the application server setup the JNDI mail session.
The tutorial can be divide into 3 part, first part is configure email jndi, second part is upload file and the final part is using spring mail.
The source code and example can download from here.
Setup java mail session in glassfish
-
D:\Development\glassfish\bin > asadmin create-javamail-resource –mailhost smtp.loongest.com –mailuser no-reply –fromaddress no-reply@loongest.com mail/LiferayMailSession
-
-
Command create-javamail-resource executed successfully.
Create Service Interface
-
package com.loongest.mail.service;
-
-
public interface MailService {
-
-
-
}
Create Implementation
-
package com.loongest.mail.service;
-
import javax.mail.internet.MimeMessage;
-
import org.springframework.beans.factory.annotation.Autowired;
-
import org.springframework.mail.SimpleMailMessage;
-
import org.springframework.mail.javamail.JavaMailSender;
-
import org.springframework.mail.javamail.MimeMessageHelper;
-
import org.springframework.mail.javamail.MimeMessagePreparator;
-
import org.springframework.stereotype.Service;
-
-
@Service
-
public class MailServiceImpl implements MailService {
-
-
@Autowired
-
private JavaMailSender mailSender;
-
-
@Autowired
-
private SimpleMailMessage simpleMailMessage;
-
-
public void setMailSender(JavaMailSender mailSender) {
-
this.mailSender = mailSender;
-
}
-
-
public void setSimpleMailMessage(SimpleMailMessage simpleMailMessage) {
-
this.simpleMailMessage = simpleMailMessage;
-
}
-
-
-
MimeMessagePreparator preparator = new MimeMessagePreparator()
-
{
-
{
-
mimeMessage.addHeader("Content-Transfer-Encoding","base64");
-
mimeMessage.setHeader("Content-Disposition", "attachment");
-
-
MimeMessageHelper helper =
-
new MimeMessageHelper(mimeMessage,true, "UTF-8");
-
-
helper.setFrom(simpleMailMessage.getFrom());
-
helper.setTo(to);
-
helper.setSubject(simpleMailMessage.getSubject());
-
helper.setText(body,true);
-
}
-
};
-
mailSender.send(preparator);
-
}
-
}
Form Object
-
package com.loongest.mail.model;
-
-
private static final long serialVersionUID = 1L;
-
private String email;
-
private String subject;
-
private String content;
-
-
public MailForm() { }
-
-
return email;
-
}
-
-
this.email = email;
-
}
-
-
return subject;
-
}
-
-
this.subject = subject;
-
}
-
-
return content;
-
}
-
-
this.content = content;
-
}
-
}
Create Controller Class
-
package com.loongest.mail.controller;
-
import ….
-
-
@Controller
-
@RequestMapping("VIEW")
-
public class MailController {
-
-
@Autowired private MailService mailService;
-
-
@RenderMapping
-
return "view";
-
}
-
-
@ActionMapping(params = "action=sendmail")
-
public void doPost(@ModelAttribute("form") MailForm form,
-
ActionRequest request, ActionResponse response){
-
-
mailService.sendMail(form.getEmail(),
-
form.getSubject(),
-
form.getContent());
-
}
-
-
@ModelAttribute("form")
-
public MailForm getCommandObject() {
-
return new MailForm();
-
}
-
}
Optional
If you’d like to try out with standard way, you can replace with the following method on action mapping.
-
@ActionMapping(params = "action=sendmail")
-
public void doPost2(@ModelAttribute("form") MailForm form,
-
ActionRequest request, ActionResponse response)
-
throws NamingException, MessagingException
-
{
-
String name = "java:comp/env/mail/LiferayMailSession";
-
Session session = (Session)ic.lookup(name);
-
-
props.put("mail.from","no-reply@loongest.com");
-
Message msg = new MimeMessage(session);
-
msg.setSubject(form.getSubject());
-
msg.setFrom();
-
msg.setRecipients(Message.RecipientType.TO,
-
InternetAddress.parse(form.getEmail(), false));
-
msg.setText(form.getContent());
-
Transport.send(msg);
-
}
Create JSP View
-
-
<%@ include file="/WEB-INF/jsp/common/includes.jsp" %>
-
<%@ page import="javax.portlet.PortletURL"%>
-
<h2> Send mail example</h2>
-
-
<portlet:actionURL var="mailUrl">
-
<portlet:param name="action" value="sendmail" />
-
</portlet:actionURL>
-
<form:form name="mailForm" commandName="form" method="post" action="${mailUrl}">
-
<table align="left">
-
<tr>
-
<td>Send to :</td>
-
<td> <form:input path="email" /> </td>
-
</tr>
-
<tr>
-
<td>Subject :</td>
-
<td> <form:input path="subject" /> </td>
-
</tr>
-
<tr>
-
<td>Description :</td>
-
<td> <form:textarea path="content" cols="30" rows="5" /> </td>
-
</tr>
-
<tr>
-
<td colspan="2"><input type="submit" value="Send Mail 1" /></td>
-
</tr>
-
<tr>
-
<td colspan="2"> </td>
-
</tr>
-
</table>
-
</form:form>
-
ApplicationContext
-
-
<?xml version="1.0" encoding="UTF-8"?>
-
<beans xmlns="http://www.springframework.org/schema/beans"
-
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
-
……..">
-
-
<context:property-placeholder location="classpath:mail.properties" />
-
-
<context:component-scan base-package="com.loongest.mail">
-
<context:exclude-filter type="annotation"
-
expression="org.springframework.stereotype.Controller"/>
-
</context:component-scan>
-
-
<!–########### Mail Service ###########–>
-
-
<bean id="mailService" class="com.loongest.mail.service.MailServiceImpl">
-
<property name="mailSender" ref="mailSender"/>
-
<property name="simpleMailMessage" ref="simpleMailMessage"/>
-
</bean>
-
-
<!–########### Mail Sender ###########–>
-
<bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
-
<property name="session" ref="mailSession"/>
-
</bean>
-
-
<bean id="mailSession" class="org.springframework.jndi.JndiObjectFactoryBean">
-
<property name="jndiName" value="java:comp/env/mail/LiferayMailSession"/>
-
</bean>
-
-
<!–########### Template ###########–>
-
<bean id="simpleMailMessage" class="org.springframework.mail.SimpleMailMessage">
-
<property name="from" value="from@no-spam.com"/>
-
<property name="subject" value="No Subject"/>
-
<property name="text">
-
<value> <![CDATA[
-
Dear Reader,
-
-
Thank you for trying this example.
-
]]>
-
</value>
-
</property>
-
</bean>
-
</beans>
web.xml
-
-
<resource-ref>
-
<res-ref-name>mail/LiferayMailSession</res-ref-name>
-
<res-type>javax.mail.Session</res-type>
-
<res-auth>Container</res-auth>
-
<res-sharing-scope>Shareable</res-sharing-scope>
-
</resource-ref>
-
Troubleshooting
Remember add the above configuration into your web.xml otherwise you will get the error like
-
Related cause: org.springframework….BeanCreationException:
-
Error creating bean with name ‘mailSession’ defined in class path resource [applicationContext.xml]:
-
No object bound to name java:comp/env/mail/LiferayMailSession
Done !
Reference :
http://wheelersoftware.com/articles/spring-javamail.html
http://springtips.blogspot.com/2008/06/send-e-mail-using-spring-and-javamail.html
http://docs.sun.com/source/817-5449/djmail.html
http://docs.sun.com/app/docs/doc/820-4336/beanr?l=en&a=view
http://docs.sun.com/app/docs/doc/820-4335/ablky?l=en&a=view
http://forums.java.net/jive/message.jspa?messageID=366332
https://cejug-classifieds.dev.java.net/source/browse/cejug-classifieds/trunk/cejug-classifieds-login/src/main/java/net/java/dev/cejug/classifieds/login/jms/NotificationMailerBean.java?rev=1326&view=markup
http://picasaweb.google.com/pradyutb/OopPraddy?authkey=Gv1sRgCOKBjZTD9db1bA&feat=directlink#5312016739707868290
http://forums.sun.com/thread.jspa?threadID=668779
http://weblogs.java.net/blog/2008/03/11/resources-glassfish
package com.fsecure.web.ipc.mail;
public interface MailService {
public void sendMail(String from, String subject, String body);
}

Posted in 



Superb info, I’d say thanks to writer because i have read here many interesting knowledge. Im waiting for more posts. Bye!:)
Thanks For This post, was added to my bookmarks.