Announcement Announcement Module
Collapse
No announcement yet.
Regarding velocity template Engine Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Regarding velocity template Engine

    hey;

    This could probably be my first post,I m using SpringFramework with ant and appfuse,and want to use the Mailing facility provided by velocity template engine,
    But its giving number format Exception, I have checked that all the fields of the SimpleMailMessage are set properly.and the recepient id also proper,

    Please help me out .if possible please send me complete steps of the same of any demo example.



    the steps which i have already gone through are:

    1.copied jars (velocity1.4,velocity tools view1.1) in to lib.
    2.created class MailEngine.java

    import java.util.Map;

    import javax.mail.MessagingException;
    import javax.mail.internet.MimeMessage;

    import org.apache.commons.logging.Log;
    import org.apache.commons.logging.LogFactory;
    import org.apache.velocity.app.VelocityEngine;
    import org.apache.velocity.exception.VelocityException;

    import org.springframework.core.io.ClassPathResource;
    import org.springframework.mail.MailException;
    import org.springframework.mail.MailSender;
    import org.springframework.mail.SimpleMailMessage;
    import org.springframework.mail.javamail.JavaMailSenderIm pl;
    import org.springframework.mail.javamail.MimeMessageHelpe r;
    import org.springframework.ui.velocity.VelocityEngineUtil s;

    /**
    * Class for sending e-mail messages based on Velocity templates
    * or with attachments.
    *
    * <p><a href="MailEngine.java.html"><i>View Source</i></a></p>
    *
    *
    */
    public class MailEngine {
    protected static final Log log = LogFactory.getLog(MailEngine.class);
    private MailSender mailSender;
    private VelocityEngine velocityEngine;

    public void setMailSender(MailSender mailSender) {
    this.mailSender = mailSender;
    }

    public void setVelocityEngine(VelocityEngine velocityEngine) {
    this.velocityEngine = velocityEngine;
    }

    /**
    * Send a simple message based on a Velocity template.
    * @param msg
    * @param templateName
    * @param model
    */
    public void sendMessage(SimpleMailMessage msg, String templateName,
    Map model) {
    String result = null;

    try {
    result =
    VelocityEngineUtils.mergeTemplateIntoString(veloci tyEngine,
    templateName, model);
    } catch (VelocityException e) {
    e.printStackTrace();
    }

    msg.setText(result);
    send(msg);
    }

    /**
    * Send a simple message with pre-populated values.
    * @param msg
    */
    public void send(SimpleMailMessage msg) {
    try {
    mailSender.send(msg);
    } catch (MailException ex) {
    //log it and go on
    log.error(ex.getMessage());
    }
    }

    /**
    * Convenience method for sending messages with attachments.
    *
    * @param emailAddresses
    * @param resource
    * @param bodyText
    * @param subject
    * @param attachmentName
    * @throws MessagingException
    *
    */
    public void sendMessage(String[] emailAddresses,
    ClassPathResource resource, String bodyText,
    String subject, String attachmentName)
    throws MessagingException {
    MimeMessage message =
    ((JavaMailSenderImpl) mailSender).createMimeMessage();

    // use the true flag to indicate you need a multipart message
    MimeMessageHelper helper = new MimeMessageHelper(message, true);

    helper.setTo(emailAddresses);
    helper.setText(bodyText);
    helper.setSubject(subject);

    helper.addAttachment(attachmentName, resource);

    ((JavaMailSenderImpl) mailSender).send(message);
    }
    }



    3.Enterd beans in application context.

    <bean id="mailEngine" class="com.nihilent.hrms.service.MailEngine">
    <property name="mailSender" ref="mailSender"/>
    <property name="velocityEngine" ref="velocityEngine"/>
    </bean>

    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailS enderImpl">
    <property name="host" value="${mail.host}"/>
    <property name="username" value="${mail.username}"/>
    <property name="password" value="${mail.password}"/>
    </bean>

    <!-- Configure Velocity for sending e-mail -->
    <bean id="velocityEngine" class="org.springframework.ui.velocity.VelocityEng ineFactoryBean">
    <property name="velocityProperties">
    <props>
    <prop key="resource.loader">class</prop>
    <prop key="class.resource.loader.class">
    org.apache.velocity.runtime.resource.loader.Classp athResourceLoader
    </prop>
    <prop key="velocimacro.library"></prop>
    </props>
    </property>
    </bean>

    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage" singleton="false">
    <property name="from" value="${mail.default.from}"/>
    </bean>

    4.using this class method (send) in my action class

    MailEngine mailEngine=new MailEngine();
    MailSender mailSender=new MailSender();
    VelocityEngine velocityEngine=new VelocityEngine();
    SimpleMailMessage smm=new SimpleMailMessage();
    mailEngine.setMailSender(mailSender);
    mailEngine.setVelocityEngine(velocityEngine);
    smm.setTo("Dev" +"<"+"[email protected]"+">");//here i m giving proper id
    StringBuffer sb=new StringBuffer();

    sb.append("dear just checking");
    smm.setText(sb.toString());
    smm.setSubject("check");

    try {
    mailEngine.send(smm);
    }
    catch (MailException exc)
    {
    log.error("The e-mail could not be send: " + exc.getMessage());
    throw exc;
    }
Working...
X