Spring framework email sending with Velocity example


      Click here to attend Spring Framework 4.x and certification course with a discount

    
package nl.kameroom.emailservice;

import java.util.List;
import java.util.Locale;
import java.util.Map;

import javax.mail.internet.MimeMessage;
import javax.sql.DataSource;

import nl.kameroom.user.UserManager;

import org.apache.velocity.app.VelocityEngine;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
import org.springframework.context.support.ResourceBundleMessageSource;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.mail.javamail.MimeMessagePreparator;
import org.springframework.ui.velocity.VelocityEngineUtils;
import org.springframework.util.Assert;

public class EmailService {

	
    /* Email From param */
    public static final String FROM = "from";

    /* Email To param */
    public static final String TO = "to";

    /* Email Subject param */
    public static final String SUBJECT = "subject";

    /* Email CC param */
    public static final String CC_LIST = "ccList";
    public static final String KEY = "key";

    @Autowired
    private JavaMailSender mailSender;

    @Autowired
    private VelocityEngine velocityEngine;
        
    @Autowired
    private ReloadableResourceBundleMessageSource messageSourceVM = null;
    public void setResource(ReloadableResourceBundleMessageSource resource){
		this.messageSourceVM = resource;
}
    public boolean send(final String templateName, final Map<String, Object> model) {
        boolean r = false;
    	try {
    	
    	MimeMessagePreparator preparator = new MimeMessagePreparator() {
			
            @SuppressWarnings("unchecked")
            public void prepare(MimeMessage mimeMessage) throws Exception {
                String from = (String) model.get(FROM);
                String to = (String) model.get(TO);
                String subject = (String) model.get(SUBJECT);
                String key = (String) model.get(KEY);
                Assert.notNull(from);
                Assert.notNull(to);
                Assert.notNull(subject);
                List<String> ccList = (List<String>) model.get(CC_LIST);
                MimeMessageHelper message = new MimeMessageHelper(mimeMessage);
                message.setFrom(from);
                message.setTo(to);
                message.setSubject(subject);
                if (ccList != null) {
                    for (String cc : ccList) {
                        //message.addCc(cc);
                        message.addBcc(cc);
                    }
                }
                model.put("messageSource",messageSourceVM);
                model.put("noArgs", new Object[]{});
                model.put("key", key);

                model.put("locale",	Locale.ENGLISH);
                
                String text = VelocityEngineUtils.mergeTemplateIntoString(
                        velocityEngine, templateName,"utf-8", model);
                message.setText(text, true);
            }
        };

        mailSender.send(preparator); 
        r = true;
        }catch(Exception e) {
        	e.printStackTrace();        	
        }
    	return r;
    }
}
  
<body bgcolor="#FFFFFF" leftmargin="40" topmargin="0" marginwidth="0" marginheight="0">
<div>


<table style="width: 800px;" border="0" cellspacing="0" cellpadding="0">
  <tbody>
    <tr>
      <td colspan="2" width="600" height="28"><div style="text-align: left; float: left; font-size: 26px; font-weight: bold; color: #253400; font-family: arial, sans-serif;">
	  <a href="http://localhost:8080/kameroom/" style="text-decoration: none;color:black">kameroom.nl</a></div></td>  
    </tr>
    <tr>
      <td rowspan="2" width="334" height="18" bgcolor="#CEE3F6"></td>
      <td colspan="2" width="266" height="10" bgcolor="#CEE3F6"></td>
    </tr>
    <tr>
      <td colspan="2" style="color: #5d8400;border: 1px solid #FFFFFF;" rowspan="2" width="100" height="130" align="center" valign="top" bgcolor="#D5EAA2">
	  <table  border="0">
          <tbody>
            <tr >
              <td  style="color: #FFFFFF; font-size: 22px; font-family: arial, sans-serif;" align="right">
			  <a href="http://localhost:8080/kameroom/" style="text-decoration: none;color:black">
				<input type="image" src="http://localhost:8080/kameroom/display/images/main/logo.png" alt="kameroom.nl"  />
			  </a>
			  </td>
            </tr>
          </tbody>
        </table>
		</td>     
    </tr>
    <tr>
      <td rowspan="2" width="100%" height="497" align="left" valign="top">
	  <table style="width:100%" border="0" cellspacing="0" cellpadding="5">
          <tbody>
            <tr>
              <td style="font-size: 17px; font-family: arial, sans-serif; color: #5d8400; text-align: left;font-weight: bold;">NL New Message</td>
            </tr>
            <tr>
             <td style="font-size: 12px; font-family: arial, sans-serif; color: #000000; text-align: left;">
             Your messages are accessed by the following link <a href="$nurl">View Message</a>
             

			 <table border="1" style="width:100%;border:2px;font-size: 12px; font-family: arial, sans-serif; color: #000000;">
			 	<tr><td style="height:5px;border: 0px "></td></tr>
			 	<tr><td style="border: 0px"><b>Advert:</b></td><td style="border: 0px"><a href="$aurl">View Advert</a></td></tr>
			 	<tr><td style="border: 0px"><b>From:</b></td><td style="border: 0px">$frommessage</td></tr>
			 	<tr><td style="border: 0px"><b>To:</b></td><td style="border: 0px">$tomessage</td></tr>
				<tr><td style="border: 0px"><b>Subject:</b></td><td style="border: 0px">$subjectmessage</td></tr>
				<tr><td style="border: 0px"><b>Content:</b></td><td style="border: 0px">$content</td></tr>
				<tr><td style="height:5px;border: 0px "></td></tr>
				<tr><td colspan="2" style="border: 0px" >Or you copy and paste the following link in your browser's URL box for the message</td></tr>
				<tr><td colspan="2"style="border: 0px">$nurl</td></tr>
				<tr><td colspan="2" style="border: 0px" >Or you copy and paste the following link in your browser's URL box for the advert</td></tr>
				<tr><td colspan="2"style="border: 0px">http://localhost:8080/kameroom/profile/messages.k</td></tr>
			 </table></td>
            </tr>
          </tbody>
        </table></td>
      <td width="24" height="65"></td>
    </tr>
    <tr>
      <td colspan="2" width="266" align="left" valign="top">
      <table style="width: 245px; border: 3px solid #ffffff;" border="0" cellspacing="0" cellpadding="0">
          <tbody>
            <tr>
              <td style="font-size: 10px;"></td>
            </tr>
            <tr>
              <td style="border: 1px solid #D5EAA2; background: #F5FFDB; font-size: 12px; font-family: arial, sans-serif;"><div style="margin: 7px;"><a style="color: #5d8400;" href="http://localhost:8080/kameroom/contact.k">Contact</a></div></td>
            </tr>
            <tr>
              <td style="font-size: 10px;"></td>
            </tr>
            <tr>
              <td style="border: 1px solid #D5EAA2; background: #F5FFDB; font-size: 12px; font-family: arial, sans-serif;"><div style="margin: 7px;"><a style="color: #5d8400;" href="http://localhost:8080/kameroom/privacy.k">Privacy</a></div></td>
            </tr>
            <tr>
              <td style="font-size: 10px;"></td>
            </tr>
            <tr>
              <td style="border: 1px solid #D5EAA2; background: #F5FFDB; font-size: 12px; font-family: arial, sans-serif;"><div style="margin: 7px;">
			  <a style="color: #5d8400;" href="http://localhost:8080/kameroom/conditions.k">Terms&Conditions</a></div></td>
            </tr>
            <tr>
              <td style="font-size: 20px;"></td>
            </tr>
            <tr>
              <td style="font-size: 11px; font-family: arial, sans-serif; color: #a3a3a3;"><div style="margin: 0 7px;">kameroom 2012</div></td>
            </tr>
          </tbody>
        </table></td>
    </tr>
  </tbody>
</table>
</div>
</body>



Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s