Required
Jars:
commons-fileupload-1.2.2.jar
commons-io-2.3.jar
commons-logging-1.1.1.jar
mail.jar
spring-beans-3.2.0.RELEASE.jar
spring-context-3.2.0.RELEASE.jar
spring-context-support-3.2.0.RELEASE.jar
spring-core-3.2.0.RELEASE.jar
spring-expression-3.2.0.RELEASE.jar
spring-web-3.2.0.RELEASE.jar
spring-webmvc-3.2.0.RELEASE.jar
web.xml
<?xml
version="1.0"
encoding="UTF-8"?>
<web-app
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID"
version="3.0">
<display-name>SendingMail</display-name>
<servlet>
<servlet-name>SpringController</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/spring-mvc.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>SpringController</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>EmailForm.jsp</welcome-file>
</welcome-file-list>
</web-app>
spring-mvc.xml
<?xml
version="1.0"
encoding="UTF-8"?>
<beans
xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
<context:component-scan
base-package="com.karthik.spring"
/>
<bean
id="mailSender"
class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property
name="host"
value="smtp.gmail.com"
/>
<property
name="port"
value="587"
/>
<property
name="username"
value="xxxx@gmail.com"
/>
<property
name="password"
value="xxxx"
/>
<property
name="javaMailProperties">
<props>
<prop
key="mail.transport.protocol">smtp</prop>
<prop
key="mail.smtp.auth">true</prop>
<prop
key="mail.smtp.starttls.enable">true</prop>
</props>
</property>
</bean>
<bean
id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--
max upload size in bytes -->
<property
name="maxUploadSize"
value="20971520"
/> <!--
20MB -->
<!--
max size of file in memory (in bytes) -->
<property
name="maxInMemorySize"
value="1048576"
/> <!--
1MB -->
</bean>
<bean
id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property
name="prefix"
value="/"
/>
<property
name="suffix"
value=".jsp"
/>
</bean>
<bean
class="org.springframework.web.servlet.handler.SimpleMappingExceptionResolver">
<property
name="exceptionMappings">
<props>
<prop
key="java.lang.Exception">Error</prop>
</props>
</property>
</bean>
</beans>
SendEmailWithAttachment
Controller:
package
com.karthik.spring;
import
java.io.IOException;
import
java.io.InputStream;
import
javax.mail.internet.MimeMessage;
import
javax.servlet.http.HttpServletRequest;
import
org.springframework.beans.factory.annotation.Autowired;
import
org.springframework.core.io.InputStreamSource;
import
org.springframework.mail.javamail.JavaMailSender;
import
org.springframework.mail.javamail.MimeMessageHelper;
import
org.springframework.mail.javamail.MimeMessagePreparator;
import
org.springframework.stereotype.Controller;
import
org.springframework.web.bind.annotation.RequestMapping;
import
org.springframework.web.bind.annotation.RequestMethod;
import
org.springframework.web.bind.annotation.RequestParam;
import
org.springframework.web.multipart.commons.CommonsMultipartFile;
@Controller
@RequestMapping("/sendEmail.do")
public
class
SendEmailWithAttachment {
@Autowired
private
JavaMailSender mailSender;
@RequestMapping(method
= RequestMethod.POST)
public
String sendEmail(HttpServletRequest request,
final
@RequestParam
CommonsMultipartFile attachFile) {
//
reads form input
final
String emailTo = request.getParameter("mailTo");
final
String subject = request.getParameter("subject");
final
String yourmailid = request.getParameter("yourmail");
final
String message = request.getParameter("message");
//
for logging
System.out.println("emailTo:
" + emailTo);
System.out.println("subject:
" + subject);
System.out.println("Your
mail id is: "+yourmailid);
System.out.println("message:
" + message);
System.out.println("attachFile:
" +
attachFile.getOriginalFilename());
mailSender.send(new
MimeMessagePreparator() {
@Override
public
void
prepare(MimeMessage mimeMessage) throws
Exception {
MimeMessageHelper
messageHelper = new
MimeMessageHelper(
mimeMessage,
true,
"UTF-8");
messageHelper.setTo(emailTo);
messageHelper.setSubject(subject);
messageHelper.setReplyTo(yourmailid);
messageHelper.setText(message);
//
determines if there is an upload file, attach it to the e-mail
String
attachName = attachFile.getOriginalFilename();
if
(!attachFile.equals(""))
{
messageHelper.addAttachment(attachName,
new
InputStreamSource() {
@Override
public
InputStream getInputStream() throws
IOException {
return
attachFile.getInputStream();
}
});
}
}
});
return
"Result";
}
}
EmailForm.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Spring
MVC - Email</title>
</head>
<body>
<center>
<h1>Send e-mail with attachment</h1>
<form
method="post"
action="sendEmail.do"
enctype="multipart/form-data">
<table
border="0"
width="80%">
<tr>
<td>Email
To:</td>
<td><input
type="text"
name="mailTo"
size="45"
/></td>
</tr>
<tr>
<td>Subject:</td>
<td><input
type="text"
name="subject"
size="45"
/></td>
</tr>
<tr>
<td>Your
mail ID:</td>
<td><input
type="text"
name="yourmail"
size="45"
/></td>
</tr>
<tr>
<td>Message:</td>
<td><textarea
cols="50"
rows="10"
name="message"></textarea></td>
</tr>
<tr>
<td>Attach
file:</td>
<td><input
type="file"
name="attachFile"
size="60"
/></td>
</tr>
<tr>
<td
colspan="2"
align="center">
<input
type="submit"
value="Send
E-mail" />
</td>
</tr>
</table>
</form>
</center>
</body>
</html>
Error.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Error</title>
</head>
<body>
<center>
<h2>Sorry,the email was not sent because of the following error</h2>
<h3>${exception.message}</h3>
</center>
</body>
</html>
Result.jsp
<%@
page
language="java"
contentType="text/html;
charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE
html PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta
http-equiv="Content-Type"
content="text/html;
charset=UTF-8">
<title>Send mail Result</title>
</head>
<body>
<center>
<h2>Success</h2>
</center>
</body>
</html>
This comment has been removed by the author.
ReplyDelete