How To send Email with attachment in java with spring

1-add Emailtemplate.vm to class path of java i mean src/main/java

<h3 >${subject} </h3>
<div>   ${messageBody} </div>
<h3>File ID=${FileID}    <br> Provider ID=${ProviderID}

2-then add dependency in pom file for volicity






3- add bean in root context for java mail sender and one also for volicity
<!–email sender –>
<bean id=”mailSender”
p:host=”” p:port=”27″>

<property name=”username” value=”shady.ragab” />
<property name=”password” value=”User2010″ />


<!– velocity template –>
<bean id=”velocityEngine”
<property name=”velocityProperties”>

4- service

public class MailServiceImpl implements MailService {

	JavaMailSender mailSender;
	VelocityEngine velocityEngine;

	public void send(final String from, final String[] to, final String[] cc, final String[] bcc,

			final String subject, final String messageBody,final NotificationBean notification)

			throws MessagingException {

		  final MimeMessagePreparator preparator = new MimeMessagePreparator()
		         public void prepare(MimeMessage mimeMessage) throws Exception {
		            MimeMessageHelper message = new MimeMessageHelper(mimeMessage,true);
		            message.setFrom(from); // could be parameterized...
		            Map model = new HashMap();

		            Listtemp= (List) notification.getNotificationParamsCollection();
		            for (NotificationParam param : temp) {
		            	System.out.println(param.getId().getKey()+"  "+ param.getValue());
				    if(param.getId().getKey().equals("File ID"))
					 if(param.getId().getKey().equals("Provider ID"))


		            String text = VelocityEngineUtils.mergeTemplateIntoString(
				               velocityEngine, new ClassPathResource("Emailtemplate.vm").getPath(), model);
		            message.setText(text, true);

		            byte[]attachdata="this is testing file".getBytes();
		            InputStreamSource source= new ByteArrayResource(attachdata);
		            message.addAttachment("test.txt", source,"text/plain");
		            		new ByteArrayDataSource(notification.getAttachment(), notification.getMimeType().getMimeType()));


		      new Thread(){
		    	  public void run(){



About lorddisk

WebCenter Content, WebCenter Portal, WebCenter Sites,Weblogic, Identity and Access Management (IAM),SSO,OAM,OIM,OAAM,OUD, OPAM,OID, OVD ,Oracle API Gateway ,OBIEE,OEDQ, Oracle ADF, Oracle SOA,J2EE, CackePHP ,PHP,J2SE,J2EE,Spring,Hibernate,JQuery,CSS,Java Script ,Joomla,Drupal,Worpress
This entry was posted in java, spring and tagged . Bookmark the permalink.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.