Sending Email with attachment using Struts2

This tutorial shows how do we send email with attachment. This tutorial uses one input form where a user gives input for ‘Email To Address’ where the intended email will be sent, ‘Email Subject’, ‘Email Message’ and uploads one file which will be sent as an attachment with the email. This email sending example uses only gmail smtp server to send the mail. If you need only file upload example then you can search in the search box you will get file upload examples in Struts2, JSF2, Codeigniter2.

For testing purpose you can also send the email to yourself and after sending open your mailbox, you will find an email has arrived.

Prerequisites

JDK 1.6
Eclipse
Tomcat v7
Gmail Account
List of jars

send email with attachment using struts2

1. We create a Dynamic Web Project in Eclipse. The required project structure gets created. Now open the deployment descriptor file and modify it as follows.

web.xml

 

2. Create one jsp file – sendemail.jsp – with the following inputs

To Address – where the email will be sent
Subject – subject of the email
Message – message to the email
File – file which will be attached to the email
Send Email – submit button for validating the input form and sending the email to the intended user

You must use enctype=”multipart/form-data” in the form tag otherwise you won’t be able to upload a file.

sendemail.jsp

 

3. Now create struts configuration file which holds differenet configurations like action mapping, action name, action class mapping, input form, redirect to output page, different interceptors, constants etc. The below struts config is straight forward.

struts.xml

 

4. Below class does the task of email sending process. It has one static void method sendMail which takes five arguments with meaningful name.

SendEmail.java

 

5. We will now look into the Action class which will do the actual upload of file and email sending process. We implement ServletContextAware to get the servlet related work done like uploading the file. The action class is just a simple POJO(Plain Old Java Object).

Note in this class, attribute names are same as on the input jsp form. So the mapping will be automatically done between form input names and java class attribute names.

SendEmailAction.java

 

When we run the application we get the output on the browser.

Input form

send email with attachment using struts2

Fill in input form

send email with attachment using struts2

Send the email with attachment

send email with attachment using struts2

Output to the console when email is successfully sent.

 

Thanks for your reading. Please do not forget to leave a comment.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.